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/props_wasteland/controlroom_storagecloset001a.mdl")
        self:PhysicsInit(SOLID_VPHYSICS)
        self:SetMoveType(MOVETYPE_VPHYSICS)
        self:SetSolid(SOLID_VPHYSICS)
        local phys = self:GetPhysicsObject()
        if IsValid(phys) then phys:Wake() end
        self.playerCooldowns = {}
    end

    function ENT:GetModelBodygroups(model)
        local tempEnt = ents.Create("prop_dynamic")
        if not IsValid(tempEnt) then return {} end
        tempEnt:SetModel(model)
        tempEnt:Spawn()
        local bodygroups = {}
        for i = 0, tempEnt:GetNumBodyGroups() - 1 do
            local name = tempEnt:GetBodygroupName(i)
            local count = tempEnt:GetBodygroupCount(i)
            if count > 1 then
                bodygroups[i] = {name = name, count = count, index = i}
            end
        end
        tempEnt:Remove()
        return bodygroups
    end

    function ENT:GetModelSkinCount(model)
        local tempEnt = ents.Create("prop_dynamic")
        if not IsValid(tempEnt) then return 0 end
        tempEnt:SetModel(model)
        tempEnt:Spawn()
        local skinCount = tempEnt:SkinCount() or 0
        tempEnt:Remove()
        return skinCount
    end

    function ENT:IsBlacklisted(model, type, index)
        local plugin = ix.plugin.list and ix.plugin.list["arsenal"]
        if not plugin or not plugin.config.wardrobeBlacklist then return false end
        local blacklist = plugin.config.wardrobeBlacklist[model]
        if not blacklist then return false end
        if type == "bodygroup" and blacklist.bodygroups then
            return table.HasValue(blacklist.bodygroups, index)
        elseif type == "skin" and blacklist.skins then
            return table.HasValue(blacklist.skins, index)
        end
        return false
    end

    function ENT:Use(activator)
        if not IsValid(activator) or not activator:IsPlayer() then return end
        if not self.playerCooldowns then self.playerCooldowns = {} end
        local steamID = activator:SteamID()
        local now = CurTime()
        if self.playerCooldowns[steamID] and (now - self.playerCooldowns[steamID]) < 2 then return end
        local char = activator:GetCharacter()
        if not char then return end
        local model = activator:GetModel()
        if not model then return end

        local bodygroups = self:GetModelBodygroups(model)
        local skinCount = self:GetModelSkinCount(model)
        local availableBodygroups = {}
        for idx, data in pairs(bodygroups) do
            if not self:IsBlacklisted(model, "bodygroup", idx) then
                availableBodygroups[idx] = data
            end
        end
        local availableSkins = {}
        for i = 0, skinCount - 1 do
            if not self:IsBlacklisted(model, "skin", i) then
                table.insert(availableSkins, i)
            end
        end

        local currentBodygroups = {}
        for idx in pairs(availableBodygroups) do
            currentBodygroups[idx] = activator:GetBodygroup(idx)
        end
        local currentSkin = activator:GetSkin()
        local currentPatch = char:GetData("patchIndex", 1)

        self.playerCooldowns[steamID] = now
        net.Start("ixWardrobeOpen")
        net.WriteString(model)
        net.WriteTable(availableBodygroups)
        net.WriteTable(availableSkins)
        net.WriteTable(currentBodygroups)
        net.WriteUInt(currentSkin, 8)
        net.WriteUInt(currentPatch, 8)
        net.Send(activator)
    end

    net.Receive("ixWardrobeApply", function(len, client)
        if not IsValid(client) then return end
        local char = client:GetCharacter()
        if not char then return end
        
        local isWipe = net.ReadBool()
        local plugin = ix.plugin.list and ix.plugin.list["arsenal"]

        if isWipe then
            client:SetSkin(0)
            char:SetData("skin", 0)
            char:SetData("patchIndex", 1)
            char:SetData("bodygroups", nil)

            local bgs = client:GetBodyGroups()
            for _, bg in ipairs(bgs) do
                client:SetBodygroup(bg.id, 0)
            end

            if plugin and plugin.config.patchIndex then
                client:SetSubMaterial(plugin.config.patchIndex, "")
            end
            
            return
        end

        local bodygroups = net.ReadTable()
        local skin = net.ReadUInt(8)
        local patch = net.ReadUInt(8)
        local model = client:GetModel()

        local faction = char:GetFaction()
        for idx, value in pairs(bodygroups) do
            if plugin and plugin.config.wardrobeAccess and plugin.config.wardrobeAccess[model] then
                local bAccess = plugin.config.wardrobeAccess[model][idx]
                if bAccess and bAccess[value] then
                    if not table.HasValue(bAccess[value], faction) then
                        value = 0
                    end
                end
            end
            client:SetBodygroup(idx, value)
        end

        client:SetSkin(skin)
        char:SetData("bodygroups", bodygroups)
        char:SetData("skin", skin)
        char:SetData("patchIndex", patch)

        if plugin and plugin.config.patchIndex and plugin.config.patchMaterials then
            local pMat = plugin.config.patchMaterials[patch]
            if pMat then
                client:SetSubMaterial(plugin.config.patchIndex, pMat)
            else
                client:SetSubMaterial(plugin.config.patchIndex, "")
            end
        end
    end)
end

if CLIENT then
    local C_BG_D = Color(3, 5, 4)
    local C_BG_M = Color(8, 12, 10)
    local C_BG_L = Color(12, 18, 14)
    local C_PRI = Color(27, 94, 32)
    local C_PRI_H = Color(33, 110, 38)
    local C_ACC = Color(56, 102, 35)
    local C_ACC_H = Color(66, 120, 45)
    local C_BDR = Color(46, 125, 50, 80)
    local C_TXT_P = Color(165, 214, 167)
    local C_TXT_S = Color(129, 199, 132)
    local C_GRAD = Color(27, 94, 32, 15)
    local C_WARN = Color(150, 40, 40)
    local C_WARN_H = Color(180, 50, 50)

    function ENT:Draw()
        self:DrawModel()
    end

    net.Receive("ixWardrobeOpen", function()
        local model = net.ReadString()
        local bodygroups = net.ReadTable()
        local availableSkins = net.ReadTable()
        local currentBodygroups = net.ReadTable()
        local currentSkin = net.ReadUInt(8)
        local currentPatch = net.ReadUInt(8)
        if IsRecruit(ply) then
            ply:Notify("Новоприбывший не имеет доступа к гардеробу.")
            return
        end

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

        local frame = vgui.Create("DFrame")
        frame:SetSize(900, 600)
        frame:Center()
        frame:SetTitle("")
        frame:ShowCloseButton(false)
        frame:MakePopup()
        frame:SetBackgroundBlur(true)
        frame.Paint = function(s, w, h)
            draw.RoundedBox(8, 0, 0, w, h, C_BG_D)
            surface.SetDrawColor(C_GRAD)
            surface.DrawRect(0, 0, w, 3)
            draw.RoundedBoxEx(8, 0, 0, w, 50, C_BG_M, true, true, false, false)
            surface.SetDrawColor(C_BDR)
            surface.DrawLine(0, 50, w, 50)
            draw.SimpleText("ГАРДЕРОБ", "DermaLarge", w/2, 25, C_TXT_P, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
            surface.SetDrawColor(C_ACC)
            surface.DrawRect(20, 48, 30, 2)
            surface.DrawRect(w-50, 48, 30, 2)
            surface.SetDrawColor(C_BDR)
            surface.DrawOutlinedRect(0, 0, w, h, 1)
        end
        
        local closeBtn = vgui.Create("DButton", frame)
        closeBtn:SetPos(frame:GetWide() - 40, 10)
        closeBtn:SetSize(30, 30)
        closeBtn:SetText("")
        closeBtn.Paint = function(s, w, h)
            local col = s:IsHovered() and C_ACC_H or C_ACC
            draw.RoundedBox(4, 0, 0, w, h, col)
            draw.SimpleText("X", "DermaLarge", w/2, h/2-2, C_TXT_P, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
        closeBtn.DoClick = function() frame:Close() end

        local modelPanel = vgui.Create("DModelPanel", frame)
        modelPanel:SetPos(20, 60)
        modelPanel:SetSize(400, 460)
        modelPanel:SetModel(model)
        
        local oldPaint = modelPanel.Paint
        modelPanel.Paint = function(s, w, h)
            draw.RoundedBox(6, 0, 0, w, h, C_BG_M)
            surface.SetDrawColor(C_BDR)
            surface.DrawOutlinedRect(0, 0, w, h, 1)
            oldPaint(s, w, h)
        end
        
        modelPanel.LayoutEntity = function(s, ent)
            if not s.bAnimSet then
                local seq = ent:LookupSequence("idle_subtle")
                if seq <= 0 then seq = ent:LookupSequence("idle_all_01") end
                if seq > 0 then
                    ent:SetSequence(seq)
                end
                s.bAnimSet = true
            end

            if (s.bDragging) then
                local x, y = gui.MousePos()
                s.camAnims.rot.yaw = s.camAnims.rot.yaw + (x - s.lastX) * 0.8
                s.camAnims.rot.pitch = math.Clamp(s.camAnims.rot.pitch + (y - s.lastY) * 0.8, -45, 45)
                s.lastX, s.lastY = x, y
            end

            local speed = FrameTime() * 10
            s.curZoom = Lerp(speed, s.curZoom, s.camAnims.zoom)
            s.curRot = LerpAngle(speed, s.curRot, s.camAnims.rot)
            
            local eyePos = ent:GetPos() + Vector(0, 0, 40)
            s:SetCamPos(eyePos - s.curRot:Forward() * s.curZoom)
            s:SetLookAng(s.curRot)

            ent:FrameAdvance((RealTime() - (s.lastTick or RealTime())) * 1)
            s.lastTick = RealTime()
        end
        
        modelPanel.camAnims = { zoom = 60, rot = Angle(0, 180, 0) }
        modelPanel.curZoom = 60
        modelPanel.curRot = Angle(0, 180, 0)

        local function SetCameraAngle(angle)
            if angle == "front" then
                modelPanel.camAnims.rot = Angle(0, 180, 0)
            elseif angle == "side" then
                modelPanel.camAnims.rot = Angle(0, 90, 0)
            elseif angle == "back" then
                modelPanel.camAnims.rot = Angle(0, 0, 0)
            end
        end
        
        modelPanel.OnMousePressed = function(s, code)
            if code == MOUSE_LEFT then
                s.bDragging = true
                s.lastX, s.lastY = gui.MousePos()
            end
        end
        
        modelPanel.OnMouseReleased = function(s, code)
            if code == MOUSE_LEFT then
                s.bDragging = false
            end
        end
        
        modelPanel.OnCursorExited = function(s)
            s.bDragging = false
        end
        
        modelPanel.OnMouseWheeled = function(s, delta)
            s.camAnims.zoom = math.Clamp(s.camAnims.zoom - delta * 15, 20, 120)
        end
        
        timer.Simple(0.05, function()
            if IsValid(modelPanel) then SetCameraAngle("front") end
        end)
        
        modelPanel.Think = function(s)
            local entity = s:GetEntity()
            if IsValid(entity) then
                for idx, value in pairs(currentBodygroups) do
                    if entity:GetBodygroup(idx) ~= value then
                        entity:SetBodygroup(idx, value)
                    end
                end
                if entity:GetSkin() ~= currentSkin then
                    entity:SetSkin(currentSkin)
                end
                
                if plugin and plugin.config.patchIndex and plugin.config.patchMaterials then
                    local pMat = plugin.config.patchMaterials[currentPatch]
                    if pMat then
                        entity:SetSubMaterial(plugin.config.patchIndex, pMat)
                    else
                        entity:SetSubMaterial(plugin.config.patchIndex, "")
                    end
                end
            end
        end

        local viewButtons = vgui.Create("DPanel", frame)
        viewButtons:SetPos(20, 530)
        viewButtons:SetSize(400, 50)
        viewButtons.Paint = function(s, w, h)
            draw.RoundedBox(6, 0, 0, w, h, C_BG_M)
            surface.SetDrawColor(C_BDR)
            surface.DrawOutlinedRect(0, 0, w, h, 1)
        end
        
        local bw = 125
        local bs = 12
        
        local frontBtn = vgui.Create("DButton", viewButtons)
        frontBtn:SetPos(10, 10)
        frontBtn:SetSize(bw, 30)
        frontBtn:SetText("")
        frontBtn.Paint = function(s, w, h)
            local col = s:IsHovered() and C_PRI_H or C_PRI
            draw.RoundedBox(4, 0, 0, w, h, col)
            surface.SetDrawColor(C_BDR)
            surface.DrawOutlinedRect(0, 0, w, h, 1)
            draw.SimpleText("СПЕРЕДИ", "DermaDefault", w/2, h/2, C_TXT_P, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
        frontBtn.DoClick = function() SetCameraAngle("front") end
        
        local sideBtn = vgui.Create("DButton", viewButtons)
        sideBtn:SetPos(10 + bw + bs, 10)
        sideBtn:SetSize(bw, 30)
        sideBtn:SetText("")
        sideBtn.Paint = function(s, w, h)
            local col = s:IsHovered() and C_PRI_H or C_PRI
            draw.RoundedBox(4, 0, 0, w, h, col)
            surface.SetDrawColor(C_BDR)
            surface.DrawOutlinedRect(0, 0, w, h, 1)
            draw.SimpleText("СБОКУ", "DermaDefault", w/2, h/2, C_TXT_P, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
        sideBtn.DoClick = function() SetCameraAngle("side") end
        
        local backBtn = vgui.Create("DButton", viewButtons)
        backBtn:SetPos(10 + (bw + bs) * 2, 10)
        backBtn:SetSize(bw, 30)
        backBtn:SetText("")
        backBtn.Paint = function(s, w, h)
            local col = s:IsHovered() and C_PRI_H or C_PRI
            draw.RoundedBox(4, 0, 0, w, h, col)
            surface.SetDrawColor(C_BDR)
            surface.DrawOutlinedRect(0, 0, w, h, 1)
            draw.SimpleText("СЗАДИ", "DermaDefault", w/2, h/2, C_TXT_P, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
        backBtn.DoClick = function() SetCameraAngle("back") end

        local settingsPanel = vgui.Create("DPanel", frame)
        settingsPanel:SetPos(440, 60)
        settingsPanel:SetSize(440, 520)
        settingsPanel.Paint = function(s, w, h)
            draw.RoundedBox(6, 0, 0, w, h, C_BG_M)
            surface.SetDrawColor(C_BDR)
            surface.DrawOutlinedRect(0, 0, w, h, 1)
        end

        local settingsScroll = vgui.Create("DScrollPanel", settingsPanel)
        settingsScroll:Dock(FILL)
        settingsScroll:DockMargin(10, 10, 10, 140)
        
        local sbar = settingsScroll:GetVBar()
        sbar:SetWide(8)
        sbar.Paint = function(s, w, h) draw.RoundedBox(4, 0, 0, w, h, C_BG_D) end
        sbar.btnGrip.Paint = function(s, w, h) draw.RoundedBox(4, 0, 0, w, h, C_ACC) end
        sbar.btnUp.Paint = function() end
        sbar.btnDown.Paint = function() end

        local yPos = 5

        if plugin and plugin.config.patchMaterials and #plugin.config.patchMaterials > 0 then
            local patchLabel = vgui.Create("DPanel", settingsScroll)
            patchLabel:SetPos(10, yPos)
            patchLabel:SetSize(400, 35)
            patchLabel.Paint = function(s, w, h)
                draw.RoundedBox(4, 0, 0, w, h, C_BG_L)
                surface.SetDrawColor(C_ACC)
                surface.DrawRect(0, 0, 3, h)
                draw.SimpleText("ПАТЧИ", "DermaLarge", 10, h/2, C_TXT_P, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
            end
            yPos = yPos + 40

            local pH = vgui.Create("DPanel", settingsScroll)
            pH:SetPos(10, yPos)
            pH:SetSize(400, 65)
            pH.Paint = function(s, w, h)
                local col = s:IsHovered() and C_BG_L or C_BG_D
                draw.RoundedBox(6, 0, 0, w, h, col)
                surface.SetDrawColor(C_BDR)
                surface.DrawOutlinedRect(0, 0, w, h, 1)
            end

            local pS = vgui.Create("DNumSlider", pH)
            pS:SetPos(5, 15)
            pS:SetSize(390, 40)
            pS:SetMin(1)
            pS:SetMax(#plugin.config.patchMaterials)
            pS:SetDecimals(0)
            pS:SetValue(currentPatch)
            pS:SetText("Тип патча")
            pS.Slider.Paint = function(s, w, h)
                draw.RoundedBox(4, 0, h/2-2, w, 4, C_BG_D)
                local maxV = math.max(1, pS:GetMax() - pS:GetMin())
                local frac = (pS:GetValue() - pS:GetMin()) / maxV
                draw.RoundedBox(4, 0, h/2-2, w * frac, 4, C_ACC)
            end
            pS.Slider.Knob.Paint = function(s, w, h)
                draw.RoundedBox(w/2, 0, 0, w, h, C_PRI)
                if s:IsHovered() then draw.RoundedBox(w/2, 2, 2, w-4, h-4, C_ACC_H) end
            end
            pS.OnValueChanged = function(s, value) currentPatch = math.floor(value) end

            yPos = yPos + 70
        end

        if table.Count(bodygroups) > 0 then
            local bgLabel = vgui.Create("DPanel", settingsScroll)
            bgLabel:SetPos(10, yPos)
            bgLabel:SetSize(400, 35)
            bgLabel.Paint = function(s, w, h)
                draw.RoundedBox(4, 0, 0, w, h, C_BG_L)
                surface.SetDrawColor(C_ACC)
                surface.DrawRect(0, 0, 3, h)
                draw.SimpleText("ЧАСТИ ТЕЛА", "DermaLarge", 10, h/2, C_TXT_P, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
            end
            yPos = yPos + 40

            for idx, data in SortedPairsByMemberValue(bodygroups, "index") do
                local bgPanel = vgui.Create("DPanel", settingsScroll)
                bgPanel:SetPos(10, yPos)
                bgPanel:SetSize(400, 65)
                bgPanel.Paint = function(s, w, h)
                    local col = s:IsHovered() and C_BG_L or C_BG_D
                    draw.RoundedBox(6, 0, 0, w, h, col)
                    surface.SetDrawColor(C_BDR)
                    surface.DrawOutlinedRect(0, 0, w, h, 1)
                end

                local label = vgui.Create("DLabel", bgPanel)
                label:SetPos(10, 8)
                label:SetSize(380, 20)
                label:SetFont("DermaDefault")
                label:SetText(data.name)
                label:SetTextColor(C_TXT_P)

                local slider = vgui.Create("DNumSlider", bgPanel)
                slider:SetPos(5, 28)
                slider:SetSize(390, 30)
                slider:SetMin(0)
                slider:SetMax(data.count - 1)
                slider:SetDecimals(0)
                slider:SetValue(currentBodygroups[idx] or 0)
                slider:SetText("")
                slider.Slider.Paint = function(s, w, h)
                    draw.RoundedBox(4, 0, h/2-2, w, 4, C_BG_D)
                    local maxV = math.max(1, slider:GetMax() - slider:GetMin())
                    local frac = (slider:GetValue() - slider:GetMin()) / maxV
                    draw.RoundedBox(4, 0, h/2-2, w * frac, 4, C_ACC)
                end
                slider.Slider.Knob.Paint = function(s, w, h)
                    draw.RoundedBox(w/2, 0, 0, w, h, C_PRI)
                    if s:IsHovered() then draw.RoundedBox(w/2, 2, 2, w-4, h-4, C_ACC_H) end
                end
                slider.OnValueChanged = function(s, value) currentBodygroups[idx] = math.floor(value) end

                yPos = yPos + 70
            end
        end

        if #availableSkins > 1 then
            local skinLabel = vgui.Create("DPanel", settingsScroll)
            skinLabel:SetPos(10, yPos)
            skinLabel:SetSize(400, 35)
            skinLabel.Paint = function(s, w, h)
                draw.RoundedBox(4, 0, 0, w, h, C_BG_L)
                surface.SetDrawColor(C_ACC)
                surface.DrawRect(0, 0, 3, h)
                draw.SimpleText("СКИНЫ", "DermaLarge", 10, h/2, C_TXT_P, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
            end
            yPos = yPos + 40

            local skinPanel = vgui.Create("DPanel", settingsScroll)
            skinPanel:SetPos(10, yPos)
            skinPanel:SetSize(400, 65)
            skinPanel.Paint = function(s, w, h)
                local col = s:IsHovered() and C_BG_L or C_BG_D
                draw.RoundedBox(6, 0, 0, w, h, col)
                surface.SetDrawColor(C_BDR)
                surface.DrawOutlinedRect(0, 0, w, h, 1)
            end

            local skinSlider = vgui.Create("DNumSlider", skinPanel)
            skinSlider:SetPos(5, 15)
            skinSlider:SetSize(390, 40)
            skinSlider:SetMin(0)
            skinSlider:SetMax(#availableSkins)
            skinSlider:SetDecimals(0)
            skinSlider:SetValue(currentSkin)
            skinSlider:SetText("Скин")
            skinSlider.Slider.Paint = function(s, w, h)
                draw.RoundedBox(4, 0, h/2-2, w, 4, C_BG_D)
                local maxV = math.max(1, skinSlider:GetMax() - skinSlider:GetMin())
                local frac = (skinSlider:GetValue() - skinSlider:GetMin()) / maxV
                draw.RoundedBox(4, 0, h/2-2, w * frac, 4, C_ACC)
            end
            skinSlider.Slider.Knob.Paint = function(s, w, h)
                draw.RoundedBox(w/2, 0, 0, w, h, C_PRI)
                if s:IsHovered() then draw.RoundedBox(w/2, 2, 2, w-4, h-4, C_ACC_H) end
            end
            skinSlider.OnValueChanged = function(s, value) currentSkin = math.floor(value) end

            yPos = yPos + 70
        end

        local wipeBtn = vgui.Create("DButton", settingsPanel)
        wipeBtn:SetPos(10, 390)
        wipeBtn:SetSize(420, 50)
        wipeBtn:SetText("")
        wipeBtn.Paint = function(s, w, h)
            local col = s:IsHovered() and C_WARN_H or C_WARN
            draw.RoundedBox(6, 0, 0, w, h, col)
            surface.SetDrawColor(C_BDR)
            surface.DrawOutlinedRect(0, 0, w, h, 2)
            draw.SimpleText("СБРОСИТЬ", "DermaLarge", w/2, h/2, C_TXT_P, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
        wipeBtn.DoClick = function()
            net.Start("ixWardrobeApply")
            net.WriteBool(true)
            net.SendToServer()
            frame:Close()
        end

        local applyBtn = vgui.Create("DButton", settingsPanel)
        applyBtn:SetPos(10, 450)
        applyBtn:SetSize(420, 60)
        applyBtn:SetText("")
        applyBtn.Paint = function(s, w, h)
            local col = s:IsHovered() and C_PRI_H or C_PRI
            draw.RoundedBox(6, 0, 0, w, h, col)
            surface.SetDrawColor(C_GRAD)
            surface.DrawRect(0, 0, w, h/2)
            surface.SetDrawColor(C_BDR)
            surface.DrawOutlinedRect(0, 0, w, h, 2)
            draw.SimpleText("ПРИМЕНИТЬ", "DermaLarge", w/2, h/2, C_TXT_P, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
        applyBtn.DoClick = function()
            net.Start("ixWardrobeApply")
            net.WriteBool(false)
            net.WriteTable(currentBodygroups)
            net.WriteUInt(currentSkin, 8)
            net.WriteUInt(currentPatch, 8)
            net.SendToServer()
            frame:Close()
        end
    end)
end
