local M = {}

local function isClothingItem(item)
    if not item then return false end

    if instanceof and instanceof(item, "Clothing") then
        return true
    end

    local cat = item.getCategory and item:getCategory() or nil
    if cat and string.lower(tostring(cat)) == "clothing" then
        return true
    end

    local dcat = item.getDisplayCategory and item:getDisplayCategory() or nil
    if dcat then
        local dc = string.lower(tostring(dcat))
        if dc:find("clothing", 1, true) then
            return true
        end
    end

    return false
end

local function getUsesLeft(item)
    if not item then return nil end
    if isClothingItem(item) then return nil end

    if item.getUsedDelta and item.getUseDelta then
        local used = item:getUsedDelta()
        local delta = item:getUseDelta()

        if delta and delta > 0 then
            local maxUses = math.floor(1 / delta + 0.5)
            local left = math.floor(used / delta + 0.0001)

            return left .. " / " .. maxUses
        end
    end

    if item.getAlreadyReadPages and item.getNumberOfPages then
        local total = item:getNumberOfPages()
        local read = item:getAlreadyReadPages()
        if total and total > 0 then
            return (total - read) .. " / " .. total
        end
    end

    if item.getUses and item.getUsesMax then
        local u = item:getUses()
        local m = item:getUsesMax()
        if m and m > 1 then
            return u .. " / " .. m
        end
    end

    return nil
end

local function getConditionStat(item)
    if not item then return nil end
    if not item.getCondition or not item.getConditionMax then return nil end

    local cur = item:getCondition()
    local max = item:getConditionMax()

    if not max or max <= 0 then
        max = 10
    end

    if cur == nil then
        cur = max
    end

    cur = math.max(0, math.min(cur, max))

    local pct = math.floor((cur / max) * 100 + 0.5)

    return pct .. "%"
end

local function getWeaponSkillStat(item)
    if not item or not instanceof or not instanceof(item, "HandWeapon") then
        return nil
    end

    -- Firearms
    if item.getAmmoType then
        local ammo = item:getAmmoType()
        if ammo and ammo ~= "" then
            return "Aiming, Reloading, Maintenance"
        end
    end

    -- Melee: use weapon type
    if item.getWeaponType then
        local wt = item:getWeaponType()
        if wt and wt ~= "" then
            wt = string.lower(tostring(wt))

            local map = {
                axe        = "Axe",
                longblunt  = "Long Blunt",
                smallblunt = "Short Blunt",
                longblade  = "Long Blade",
                smallblade = "Short Blade",
                spear      = "Spear",
            }

            local skill = map[wt]
            if skill then
                return skill .. ", Maintenance"
            end
        end
    end

    return "Improvised, Maintenance"
end

M.defs = {
    --------------------------------------------------
    -- BASE / GENERIC
    --------------------------------------------------
    {
        id       = "weight",
        labelKey = "IGUI_Inspect_Weight",
        fallback = "Weight",
        getter   = function(item)
            if item and item.getWeight then
                return InspectUI_fmtNumber(item:getWeight(), 2)
            end
        end,
    },
    {
        id       = "category",
        labelKey = "IGUI_Inspect_Category",
        fallback = "Category",
        getter   = function(item)
            if item and item.getCategory then
                return tostring(item:getCategory())
            end
        end,
    },
    {
        id       = "displayCategory",
        labelKey = "IGUI_Inspect_DisplayCategory",
        fallback = "Type",
        getter   = function(item)
            if item and item.getDisplayCategory then
                local ok, cat = pcall(function() return item:getDisplayCategory() end)
                if ok and cat and cat ~= "" then
                    return tostring(cat)
                end
            end
        end,
    },
    {
        id       = "weaponSubtype",
        labelKey = "IGUI_Inspect_WeaponSubtype",
        fallback = "Weapon subtype",
        getter   = function(item)
            if not item then return end

            local parent, child = InspectUI_resolveCategory(item)
            if parent ~= "Weapons" or not child or child == "" then
                return
            end

            -- Skip generic branch buckets
            if child == "Melee" or child == "Firearm" then
                return
            end

            local labels = {
                ["Melee/Axe"]        = "Axe",
                ["Melee/LongBlunt"]  = "Long Blunt",
                ["Melee/ShortBlunt"] = "Short Blunt",
                ["Melee/LongBlade"]  = "Long Blade",
                ["Melee/ShortBlade"] = "Short Blade",
                ["Melee/Spear"]      = "Spear",
                ["Melee/Improvised"] = "Improvised",
                ["Firearm/Pistol"]   = "Pistol",
                ["Firearm/Revolver"] = "Revolver",
                ["Firearm/Rifle"]    = "Rifle",
                ["Firearm/Shotgun"]  = "Shotgun",
                ["Firearm/SMG"]      = "SMG",
                ["Firearm/Other"]    = "Other",
            }

            return labels[child] or child
        end,
    },
    {
        id       = "weaponSkills",
        labelKey = "IGUI_Inspect_Stat_Skills",
        fallback = "Skills",
        getter   = getWeaponSkillStat,
    },
    {
        id       = "condition",
        labelKey = "IGUI_Inspect_Stat_Condition",
        fallback = "Estado",
        getter   = getConditionStat,
    },
    {
        id       = "usesLeft",
        labelKey = "IGUI_Inspect_Stat_Uses",
        fallback = "Uses",
        getter   = getUsesLeft,
    },
    {
        id       = "lightDistance",
        labelKey = "IGUI_Inspect_Stat_LightDistance",
        fallback = "Light distance",
        getter   = function(item)
            if not item or not item.getLightDistance then return end

            local dist = item:getLightDistance()
            if not dist or dist <= 0 then return end

            -- Distancia en tiles (entera)
            return tostring(math.floor(dist + 0.5))
        end,
    },
    {
        id       = "lightStrength",
        labelKey = "IGUI_Inspect_Stat_LightStrength",
        fallback = "Light strength",
        getter   = function(item)
            if not item or not item.getLightStrength then return end

            local s = item:getLightStrength()
            if not s or s <= 0 then return end

            -- Valor tal cual (normalmente 0.x - 2.x)
            return InspectUI_fmtNumber(s, 2)
        end,
    },
}

M.order = {
    "weight",
    "category",
    "displayCategory",
    "weaponSubtype",
    "weaponSkills",
    "condition",
    "usesLeft",
    "lightDistance",
    "lightStrength",
}

return M
