-- Клиентская часть смены скинов
local PLUGIN = PLUGIN

local function OpenSkinMenu(ply, ent)
    if not IsValid(ent) then return end

    local frame = vgui.Create("DFrame")
    frame:SetSize(250, 400)
    frame:SetTitle("Выбор скина")
    frame:Center()
    frame:MakePopup()

    local skinPanel = vgui.Create("DScrollPanel", frame)
    skinPanel:SetSize(230, 350)
    skinPanel:SetPos(10, 30)

    net.Start("RequestSkinData")
    net.WriteEntity(ent)
    net.SendToServer()

    net.Receive("ReceiveSkinData", function()
        local totalSkins = net.ReadInt(8)
        local skinNames = net.ReadTable()
        for i = 0, totalSkins - 1 do
            local skinName = skinNames[i] or "Скин " .. i 
            local button = vgui.Create("DButton", skinPanel)
            button:SetText(skinName)
            button:SetSize(210, 40)
            button:SetPos(10, 10 + i * 50)
            button.DoClick = function()
                net.Start("ChangeEntitySkin")
                net.WriteEntity(ent)
                net.WriteInt(i, 8)
                net.SendToServer()
            end
        end
    end)
end

hook.Add("PlayerBindPress", "OpenSkinMenuOnUse", function(ply, bind, pressed)
    local allowedGroups = {
        owner = true,
        admin = true,
        ["spec admin"] = true,
        prem = true,
        sponsor = true
    }

if allowedGroups[ply:GetUserGroup()] then
        if bind == "+use" and pressed then
            local trace = ply:GetEyeTrace()
            local ent = trace.Entity

            if IsValid(ent) and trace.HitPos:Distance(ply:GetPos()) < 1000 then
                local activeWeapon = ply:GetActiveWeapon()
                if IsValid(activeWeapon) and activeWeapon:GetClass() == "weapon_hands" then
                    net.Start("CheckAllowedEntity")
                    net.WriteEntity(ent)
                    net.SendToServer()
                    net.Receive("EntityAllowed", function()
                        OpenSkinMenu(ply, ent)
                    end)
                end
            end
        end
    end
end)

concommand.Add("get_bodygroup_info", function(ply)
    local trace = ply:GetEyeTrace()
    local ent = trace.Entity

    if not IsValid(ent) then
        print("[Ошибка] Вы не смотрите на объект!")
        return
    end

    local bodygroups = ent:GetBodyGroups()
    print("========[ Bodygroup Info ]========")
    print("Модель: " .. ent:GetModel())

    for _, bg in ipairs(bodygroups) do
        local currentValue = ent:GetBodygroup(bg.id)
        print(string.format("Bodygroup: %s (ID: %d) | Выбрано: %d", bg.name, bg.id, currentValue))
    end

    local materials = ent:GetMaterials()
    print("========[ Sub-Materials ]========")

    for i = 0, #materials - 1 do
        local mat = ent:GetSubMaterial(i)
        if mat == "" then mat = "По умолчанию" end
        print(string.format("SubMaterial ID: %d | Материал: %s", i, mat))
    end
end)

concommand.Add("get_skin_info", function(ply)
    local trace = ply:GetEyeTrace()
    local ent = trace.Entity

    if not IsValid(ent) then
        print("[Ошибка] Вы не смотрите на объект!")
        return
    end

    local totalSkins = ent:SkinCount() - 1
    print("========[ Skin Info ]========")
    print("Модель: " .. ent:GetModel())
    print("Доступно скинов: " .. totalSkins)

    for i = 0, totalSkins do
        print(string.format("Skin ID: %d", i))
    end
end)
