PLUGIN.name = "Подразделения и Специализации"
PLUGIN.author = "Refosel"
PLUGIN.description = ""

ix.util.Include("cl_plugin.lua")
--ix.util.Include("sv_plugin.lua")

-- Регистрация переменных персонажа
ix.char.RegisterVar("rank", { 
    field = "rank",
    fieldType = ix.type.number,
    default = 1
})

ix.char.RegisterVar("podr", { 
    field = "podr",
    fieldType = ix.type.number,
    default = 1
})

ix.char.RegisterVar("spec", { 
    field = "spec",
    fieldType = ix.type.number,
    default = 1
})

local PMETA = FindMetaTable("Player")

function PMETA:GetRankName()
    local client = self
    local char = client:GetCharacter() 
    if not char then return false end
    
    local rank = char:GetRank()
    local podr = char:GetPodr()
    local factiontable = ix.faction.Get(client:Team())
    if not factiontable then return false end

    -- Check for subdivision specific ranks
    if (podr) and factiontable.Podr and factiontable.Podr[podr] and factiontable.Podr[podr].ranks then
        local podrRanks = factiontable.Podr[podr].ranks
        if podrRanks[rank] then
            return podrRanks[rank][1]
        elseif rank > 0 then
            -- Find the highest rank index in the subdivision table
            local maxPodrRank = 0
            for k, _ in pairs(podrRanks) do
                if k > maxPodrRank then maxPodrRank = k end
            end
            
            -- Clamp if rank is higher than the max available in subdivision
            if rank >= maxPodrRank and maxPodrRank > 0 then
                return podrRanks[maxPodrRank][1]
            end
        end
    end

    if (rank) and factiontable.Ranks then
        if factiontable.Ranks[rank] then
            return factiontable.Ranks[rank][1]
        elseif rank > 0 then
            -- Find the highest rank index in the global table
            local maxGlobalRank = 0
            for k, _ in pairs(factiontable.Ranks) do
                if k > maxGlobalRank then maxGlobalRank = k end
            end
            
            -- Clamp if rank is higher than the max available globally
            if rank >= maxGlobalRank and maxGlobalRank > 0 then
                return factiontable.Ranks[maxGlobalRank][1]
            end
        end
    end
    
    return false
end

function PMETA:GetPodrName()
    local client = self
    local char = client:GetCharacter()
    if not char then return false end
    
    local podr = char:GetPodr()
    local factiontable = ix.faction.Get(client:Team())
    if (podr) and factiontable and factiontable.Podr and factiontable.Podr[podr] then
        return factiontable.Podr[podr].name 
    else
        return false
    end
end

function PMETA:GetSpecName()
    local client = self
    local char = client:GetCharacter()
    if not char then return false end
    
    local spec = char:GetSpec()
    local factiontable = ix.faction.Get(client:Team())
    if (spec) and factiontable and factiontable.Spec and factiontable.Spec[spec] then
        return factiontable.Spec[spec].name
    else
        return false
    end
end

-- ix.plugin.SetUnloaded("stamina", true)
-- ix.plugin.SetUnloaded("strength", true)
ix.plugin.SetUnloaded("doors", true)
ix.plugin.SetUnloaded("recognition", true)

ix.char.vars["description"].bNoDisplay = true
ix.char.vars["description"].OnValidate = function() return true	end
