AddCSLuaFile()

ENT.Type = "anim"
ENT.Base = "base_gmodentity"
ENT.PrintName = "Арсенал"
ENT.Author = "Server"
ENT.Spawnable = true
ENT.AdminOnly = true
ENT.Category = "[FT] Система арсенала"
ENT.bNoPersist = true

if SERVER then
    function ENT:Initialize()
        self:SetModel("models/ft/shkaf.mdl")
        self:PhysicsInit(SOLID_VPHYSICS)
        self:SetMoveType(MOVETYPE_VPHYSICS)
        self:SetSolid(SOLID_VPHYSICS)
        local phys = self:GetPhysicsObject()
        if IsValid(phys) then phys:Wake() end
    end

    function ENT:Use(activator, caller)
        if not IsValid(activator) or not activator:IsPlayer() then return end
        local char = activator:GetCharacter()
        if not char then return end

        local plugin = ix.plugin.list and ix.plugin.list["arsenal"]
        if not plugin then return end

        local avail = plugin:GetAvailableWeapons(char)
        local weaponsData = {}
        local weaponHas = {}
        for class, data in pairs(avail) do
            weaponsData[class] = data
            weaponHas[class] = false
        end

        for _, wep in ipairs(activator:GetWeapons()) do
            if IsValid(wep) then
                local c = wep:GetClass()
                if weaponHas[c] ~= nil then weaponHas[c] = true end
            end
        end

        local supply = plugin:GetFactionSupply(char:GetFaction())
        supply = tonumber(supply) or 0

        local freeRemain = plugin:GetFreeWeaponCooldownForPlayer(activator)
        local factionID = char:GetFaction()
        net.Start("ixArsenalOpen")
            net.WriteUInt(supply, 32)
            net.WriteUInt(factionID or 0, 8)
            net.WriteTable(weaponsData)
            net.WriteTable(weaponHas)
            net.WriteTable(plugin.config.armor or {})
            net.WriteUInt(math.max(0, tonumber(freeRemain) or 0), 32)
        net.Send(activator)
    end
end

if CLIENT then
    function ENT:Draw()
        self:DrawModel()
        --local pos = self:GetPos() + Vector(0,0,10)
        --local ang = Angle(0, LocalPlayer():EyeAngles().y - 90, 90)
        --cam.Start3D2D(pos, ang, 0.1)
        --    draw.SimpleText("АРСЕНАЛ", "DermaDefaultBold", 0, 0, Color(200,200,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        --cam.End3D2D()
    end
end
