package com.tiedup.remake.items.base;

import java.util.Random;

/**
 * Standard colors for bondage items.
 * Colors are stored in NBT and used for texture selection.
 *
 * Based on original mod color variants:
 * - 16 standard colors (matching Minecraft dye colors)
 * - 2 special variants for tape (caution, clear)
 */
public enum ItemColor {
    // Standard 16 colors (modelId used for CustomModelData)
    BLACK("black", 0x1D1D21, 1),
    BLUE("blue", 0x3C44AA, 2),
    BROWN("brown", 0x835432, 3),
    CYAN("cyan", 0x169C9C, 4),
    GRAY("gray", 0x474F52, 5),
    GREEN("green", 0x5E7C16, 6),
    LIGHT_BLUE("light_blue", 0x3AB3DA, 7),
    LIME("lime", 0x80C71F, 8),
    MAGENTA("magenta", 0xC74EBD, 9),
    ORANGE("orange", 0xF9801D, 10),
    PINK("pink", 0xF38BAA, 11),
    PURPLE("purple", 0x8932B8, 12),
    RED("red", 0xB02E26, 13),
    SILVER("silver", 0x9D9D97, 14), // Also known as light_gray
    WHITE("white", 0xF9FFFE, 15),
    YELLOW("yellow", 0xFED83D, 16),

    // Special variants (for duct_tape/tape_gag)
    CAUTION("caution", 0xFFCC00, 17),
    CLEAR("clear", 0xCCCCCC, 18);

    private static final Random RANDOM = new Random();

    /** Standard colors (excludes special variants like caution/clear) */
    private static final ItemColor[] STANDARD_COLORS = {
        BLACK,
        BLUE,
        BROWN,
        CYAN,
        GRAY,
        GREEN,
        LIGHT_BLUE,
        LIME,
        MAGENTA,
        ORANGE,
        PINK,
        PURPLE,
        RED,
        SILVER,
        WHITE,
        YELLOW,
    };

    private final String name;
    private final int hexColor;
    private final int modelId;

    ItemColor(String name, int hexColor, int modelId) {
        this.name = name;
        this.hexColor = hexColor;
        this.modelId = modelId;
    }

    /**
     * Get the name used in NBT and texture paths.
     * Example: "red" -> textures/item/ropes_red.png
     */
    public String getName() {
        return name;
    }

    /**
     * Get the hex color value for tinting or display.
     */
    public int getHexColor() {
        return hexColor;
    }

    /**
     * Get the model ID used for CustomModelData.
     * This allows item models to use overrides for different colors.
     */
    public int getModelId() {
        return modelId;
    }

    /**
     * Get a random standard color (excludes caution/clear).
     */
    public static ItemColor getRandomStandard() {
        return STANDARD_COLORS[RANDOM.nextInt(STANDARD_COLORS.length)];
    }

    /**
     * Get a random standard color using a specific Random instance.
     */
    public static ItemColor getRandomStandard(Random random) {
        return STANDARD_COLORS[random.nextInt(STANDARD_COLORS.length)];
    }

    /**
     * Get a color by name (for NBT deserialization).
     * @return The color, or null if not found
     */
    public static ItemColor fromName(String name) {
        if (name == null || name.isEmpty()) {
            return null;
        }
        for (ItemColor color : values()) {
            if (color.name.equals(name)) {
                return color;
            }
        }
        return null;
    }

    /**
     * Check if this is a special color (caution/clear).
     * Special colors are only used for tape items.
     */
    public boolean isSpecial() {
        return this == CAUTION || this == CLEAR;
    }

    /**
     * Get the red component as a float (0-1).
     */
    public float getRed() {
        return ((hexColor >> 16) & 0xFF) / 255.0f;
    }

    /**
     * Get the green component as a float (0-1).
     */
    public float getGreen() {
        return ((hexColor >> 8) & 0xFF) / 255.0f;
    }

    /**
     * Get the blue component as a float (0-1).
     */
    public float getBlue() {
        return (hexColor & 0xFF) / 255.0f;
    }
}
