--[[
============================================================================
ETB - ArmorTypes.lua
============================================================================
Defines armor types (Cloth, Leather, Mail, Plate) and class restrictions.

USAGE:
    local at = ETB.ArmorTypes
    local name = at:GetName(4)  -- "Plate"
    local canWear = at:CanClassWear("WARRIOR", 4)  -- true
    local primary = at:GetPrimaryForClass("MAGE")  -- 1 (Cloth)
============================================================================
]]--

local ADDON_NAME, NS = ...
local ETB = NS.ETB

ETB.ArmorTypes = {}
local ArmorTypes = ETB.ArmorTypes

-- =============================================================================
-- ARMOR TYPE DEFINITIONS
-- =============================================================================

local ARMOR_TYPE_DATA = {
    [1] = {
        name = "Cloth",
        color = "|cffff7d0a",  -- Orange-ish
        icon = "Interface\\Icons\\INV_Fabric_Silk_02",
    },
    [2] = {
        name = "Leather",
        color = "|cffabd473",  -- Green
        icon = "Interface\\Icons\\INV_Misc_LeatherScrap_08",
    },
    [3] = {
        name = "Mail",
        color = "|cff69ccf0",  -- Blue
        icon = "Interface\\Icons\\INV_Chest_Chain_05",
    },
    [4] = {
        name = "Plate",
        color = "|cffc79c6e",  -- Tan/Brown
        icon = "Interface\\Icons\\INV_Chest_Plate01",
    },
}

-- =============================================================================
-- CLASS TO PRIMARY ARMOR MAPPING
-- =============================================================================
-- Each class has a "primary" armor type for transmog purposes
-- This is the highest armor type they can wear

local CLASS_PRIMARY_ARMOR = {
    -- Cloth wearers
    MAGE = 1,
    PRIEST = 1,
    WARLOCK = 1,
    
    -- Leather wearers
    ROGUE = 2,
    DRUID = 2,
    MONK = 2,        -- MoP+
    DEMONHUNTER = 2, -- Legion+
    
    -- Mail wearers
    HUNTER = 3,
    SHAMAN = 3,
    EVOKER = 3,      -- Dragonflight+
    
    -- Plate wearers
    WARRIOR = 4,
    PALADIN = 4,
    DEATHKNIGHT = 4, -- Wrath+
}

-- What armor types each class CAN wear (not just primary)
local CLASS_WEARABLE_ARMOR = {
    -- Cloth only
    MAGE = { [1] = true },
    PRIEST = { [1] = true },
    WARLOCK = { [1] = true },
    
    -- Leather and below
    ROGUE = { [1] = true, [2] = true },
    DRUID = { [1] = true, [2] = true },
    MONK = { [1] = true, [2] = true },
    DEMONHUNTER = { [1] = true, [2] = true },
    
    -- Mail and below
    HUNTER = { [1] = true, [2] = true, [3] = true },
    SHAMAN = { [1] = true, [2] = true, [3] = true },
    EVOKER = { [1] = true, [2] = true, [3] = true },
    
    -- Plate and below (all armor)
    WARRIOR = { [1] = true, [2] = true, [3] = true, [4] = true },
    PALADIN = { [1] = true, [2] = true, [3] = true, [4] = true },
    DEATHKNIGHT = { [1] = true, [2] = true, [3] = true, [4] = true },
}

-- =============================================================================
-- API FUNCTIONS
-- =============================================================================

function ArmorTypes:Get(armorTypeID)
    return ARMOR_TYPE_DATA[armorTypeID]
end

function ArmorTypes:GetName(armorTypeID)
    local data = ARMOR_TYPE_DATA[armorTypeID]
    return data and data.name or "Unknown"
end

function ArmorTypes:GetColor(armorTypeID)
    local data = ARMOR_TYPE_DATA[armorTypeID]
    return data and data.color or "|cffffffff"
end

function ArmorTypes:GetColoredName(armorTypeID)
    local data = ARMOR_TYPE_DATA[armorTypeID]
    if not data then return "|cffffffffUnknown|r" end
    return data.color .. data.name .. "|r"
end

function ArmorTypes:GetIcon(armorTypeID)
    local data = ARMOR_TYPE_DATA[armorTypeID]
    return data and data.icon or "Interface\\Icons\\INV_Misc_QuestionMark"
end

function ArmorTypes:GetPrimaryForClass(classToken)
    return CLASS_PRIMARY_ARMOR[classToken]
end

function ArmorTypes:CanClassWear(classToken, armorTypeID)
    local wearable = CLASS_WEARABLE_ARMOR[classToken]
    if not wearable then return false end
    return wearable[armorTypeID] == true
end

function ArmorTypes:GetCurrentPlayerPrimary()
    local _, classToken = UnitClass("player")
    return self:GetPrimaryForClass(classToken)
end

function ArmorTypes:CanCurrentPlayerWear(armorTypeID)
    local _, classToken = UnitClass("player")
    return self:CanClassWear(classToken, armorTypeID)
end

function ArmorTypes:GetAll()
    local all = {}
    for id, data in pairs(ARMOR_TYPE_DATA) do
        all[#all + 1] = { id = id, name = data.name, color = data.color }
    end
    table.sort(all, function(a, b) return a.id < b.id end)
    return all
end

-- =============================================================================
-- CONVERSION HELPERS
-- =============================================================================

-- Convert item subclass ID to armor type ID
-- Based on GetItemInfoInstant subclassID for armor
local SUBCLASS_TO_ARMOR_TYPE = {
    [1] = 1,  -- Cloth
    [2] = 2,  -- Leather
    [3] = 3,  -- Mail
    [4] = 4,  -- Plate
}

function ArmorTypes:FromSubclassID(subclassID)
    return SUBCLASS_TO_ARMOR_TYPE[subclassID]
end

-- Convert armor type name to ID
local NAME_TO_ARMOR_TYPE = {
    ["Cloth"] = 1,
    ["Leather"] = 2,
    ["Mail"] = 3,
    ["Plate"] = 4,
}

function ArmorTypes:FromName(name)
    return NAME_TO_ARMOR_TYPE[name]
end

-- =============================================================================
-- DEBUG
-- =============================================================================

function ArmorTypes:Print()
    ETB:Print("|cff00ffff[ArmorTypes]|r Registered armor types:")
    for _, at in ipairs(self:GetAll()) do
        ETB:Print(string.format("  %d = %s%s|r", at.id, at.color, at.name))
    end
    
    local _, classToken = UnitClass("player")
    local primary = self:GetPrimaryForClass(classToken)
    ETB:Print(string.format("  Current player (%s) primary: %s", classToken, self:GetName(primary)))
end

ETB:Debug("ArmorTypes", "ArmorTypes.lua loaded")
