local PANEL = {}

    AccessorFunc(PANEL, "money", "Money", FORCE_NUMBER)
    
    function PANEL:CreateZeroStats()
        SmoothBullet = 0
        SmoothHealth = 0
        SmoothPsy = 0
        SmoothRun = 0
        SmoothBurn = 0
        SmoothRadiaton = 0
        SmoothAcid = 0
        SmoothElectra = 0
        ArmorStatus = 0
        HelmetStatus = 0
        BagStatus = 0
        SmoothMutant = 0
        SniperSlotStatus = 0
        KnifeSlotStatus = 0
        PistolSlotStatus = 0
        PrimarySlotStatus = 0
    end

    local SortResetbuttonColor = Color(255, 255, 255, 255)
local SortWeaponbuttonColor = Color(255, 255, 255, 255)
local SortAmmobuttonColor = Color(255, 255, 255, 255)
local SortArmorbuttonColor = Color(255, 255, 255, 255)
local SortArtefactbuttonColor = Color(255, 255, 255, 255)
local SortFoodbuttonColor = Color(255, 255, 255, 255)
local SortDetectorsbuttonColor = Color(255, 255, 255, 255)
local SortHlambuttonColor = Color(255, 255, 255, 255)

    function PANEL:RefrashMyInventory()
        self.scroller_inv_me = self:Add("DScrollPanel")
        self.scroller_inv_me:SetSize(ScrW() * .23, ScrH() * .77)
        self.scroller_inv_me:SetPos(ScrW() * .624, ScrH() * .17)
        self.scroller_inv_me:MakePopup()

        local vbar = self.scroller_inv_me:GetVBar()
        vbar.Paint = function(self_panel, width, height)
            draw.RoundedBox(0, 0, 0, width, height, Color(40,40,40))
        end
        vbar.btnUp.Paint = function(self_panel, width, height)
            draw.RoundedBox(0, 0, 0, width , height, Color(87, 87, 87))
            surface.SetDrawColor(255, 255, 255)
            surface.SetMaterial(Material("stalplay/ui/ui_pda2_noice.png"))
            surface.DrawPartialTexturedRect( 0, 0, width, height, 809.5, 692, 20, 18, 1024, 1024 )
        end
        vbar.btnGrip.Paint = function(self_panel, width, height)
            draw.RoundedBox(0, 0, 0, width, height, Color(87, 87, 87))
        end
        vbar.btnDown.Paint = function(self_panel, width, height)
            draw.RoundedBox(0, 0, 0, width , height, Color(87, 87, 87))
            surface.SetDrawColor(255, 255, 255)
            surface.SetMaterial(Material("stalplay/ui/ui_pda2_noice.png"))
            surface.DrawPartialTexturedRect( 0, 0, width, height, 809.5, 739.5, 20, 20, 1024, 1024 )
        end

        ui = self.scroller_inv_me:Add("ixInventory")

        local character = LocalPlayer():GetCharacter()
        local inventory = character:GetInventory()

        if (inventory) then
             ui:SetInventory(inventory)
        end

        ui:SetTitle()
        ui:SetDraggable(false)

        ix.gui.inv1 = ui
    end
    
    function PANEL:Init()
        self:SetSize(ScrW() * .145, ScrH() * .07)
        self:SetPos(ScrW() * .093, ScrH() * .025)
    
        self.amountEntry = self:Add("ixTextEntry")
        self.amountEntry:Dock(FILL)
        self.amountEntry:SetFont("ixGenericFont")
        self.amountEntry:SetNumeric(false)
        self.amountEntry:SetValue("1000")
    
        self.transferButton = self:Add("DButton")
        self.transferButton:SetFont("ixIconsMedium")
        self:SetLeft(false)
        self.transferButton.DoClick = function()
            local amount = math.max(0, math.Round(tonumber(self.amountEntry:GetValue()) or 0))
            self.amountEntry:SetValue("1000")
    
            if (amount != 0) then
                self:OnTransfer(amount)
            end
        end
        
        surface.PlaySound("anomalyzone/ui_sounds/item_use/anomaly_1_5_2/inv_open.ogg")
    end
    
    function PANEL:SetLeft(bValue)
        if (bValue) then
            self.transferButton:Dock(LEFT)
            self.transferButton:SetText("s")
        else
            self.transferButton:Dock(RIGHT)
            self.transferButton:SetText("t")
        end
    end
    
    function PANEL:SetMoney(money)
        local name = string.gsub(ix.util.ExpandCamelCase(ix.currency.plural), "%s", "")
    
        self.money = math.max(math.Round(tonumber(money) or 0), 0)
        --self.moneyLabel:SetText(string.format("%d", money))
    end
    
    function PANEL:OnTransfer(amount)
    end
    
    function PANEL:Paint(width, height)
        --derma.SkinFunc("PaintBaseFrame", self, width, height)
    end
    
    function PANEL:Paint(width, height)
        --derma.SkinFunc("PaintBaseFrame", self, width, height)
    end
    
    vgui.Register("ixStorageMoney", PANEL, "EditablePanel")
    
    DEFINE_BASECLASS("Panel")
    PANEL = {}
    
    AccessorFunc(PANEL, "fadeTime", "FadeTime", FORCE_NUMBER)
    AccessorFunc(PANEL, "frameMargin", "FrameMargin", FORCE_NUMBER)
    AccessorFunc(PANEL, "storageID", "StorageID", FORCE_NUMBER)
    
    local INV_DATA = {
        ["shlem"] = {
            position = {.459, .13},
            slotssize = 20
        },
        ["armor"] = {
            position = {.463, .30},
            slotssize = 20
        },
        ["sumka"] = {
            position = {.541, .13},
            slotssize = 20
        },
        ["flashlight"] = {
            position = {.383, .13},
            slotssize = 20
        },
        ["primary"] = {
            position = {.542, .27},
            slotssize = 20
        },
        ["primary2"] = {
            position = {.383, .27},
            slotssize = 20
        },
        ["pistol"] = {
            position = {.542, .650},
            slotssize = 20
        },
        ["melee"] = {
            position = {.383, .650},
            slotssize = 20
        }
    }
    
    function PANEL:DrawPlayerModel()
        self.IDModelPanel = self:Add("DModelPanel")
        self.IDModelPanel:SetSize(We(900), He(800))  -- Example size, adjust as needed
        self.IDModelPanel:SetPos(We(500), He(100))  -- Centered above the button
        self.IDModelPanel:SetModel(LocalPlayer():GetModel()) -- you can only change colors on playermodels
        self.IDModelPanel:SetCamPos(Vector(60, 0, 60))
    
        -- Sync bodygroups from player to model panel
        local ent = self.IDModelPanel:GetEntity()
        if IsValid(ent) and IsValid(LocalPlayer()) then
            for i = 0, LocalPlayer():GetNumBodyGroups() - 1 do
                ent:SetBodygroup(i, LocalPlayer():GetBodygroup(i))
            end
        end
    
        -- Disables default rotation
        function self.IDModelPanel:LayoutEntity(Entity)
            return
        end
    
        -- Make the panel non-clickable
        self.IDModelPanel:SetMouseInputEnabled(false)
        self.IDModelPanel:SetKeyboardInputEnabled(false)
    end
    
    local black_list_slots = {}
    
    --function PANEL:CreateInventorySlots()
    --    local character = LocalPlayer():GetCharacter()
    --    for slot_index, slot_data in pairs(INV_DATA) do
    --        if not black_list_slots[slot_index] then
    --            local slot_inv = character:GetInventorySlot(slot_index)
    --            local posX, posY = slot_data.position[1], slot_data.position[2]
    --            local slot_size = slot_data.slotssize or 40
    --
    --            if not IsValid(ix.gui["inv" .. slot_inv:GetID()]) then
    --                local inventory = self:Add("ixInventory")
    --                inventory:SetPos(ScrW() * posX, ScrH() * posY)
    --                inventory:SetTitle()
    --                inventory:SetIconSize(ScreenScale(slot_size))
    --                inventory.Paint = function(s, w, h)
    --                    draw.RoundedBox(0, 0, 0, w, h, Color(50,50,50, 100))
    --                end
    --
    --                if (slot_inv) then
    --                    inventory:SetInventory(slot_inv)
    --                    ix.gui["inv" .. slot_inv:GetID()] = inventory
    --                end
    --            end
    --        end
    --    end
    --end
    
function PANEL:CreateInventory()
    self.scroller_inv_me = self:Add("DScrollPanel")
    self.scroller_inv_me:SetSize(We(445), He(930))
    self.scroller_inv_me:SetPos(We(1450), He(100))
    self.scroller_inv_me:MakePopup()

    local vbar = self.scroller_inv_me:GetVBar()
    vbar.Paint = function(self_panel, width, height)
        draw.RoundedBox(0, 0, 0, width, height, Color(40,40,40))
    end
    vbar.btnUp.Paint = function(self_panel, width, height)
        draw.RoundedBox(0, 0, 0, width , height, Color(87, 87, 87))
        surface.SetDrawColor(255, 255, 255)
        surface.SetMaterial(Material("stalplay/ui/ui_pda2_noice.png"))
        surface.DrawPartialTexturedRect( 0, 0, width, height, 809.5, 692, 20, 18, 1024, 1024 )
    end
    vbar.btnGrip.Paint = function(self_panel, width, height)
        draw.RoundedBox(0, 0, 0, width, height, Color(87, 87, 87))
    end
    vbar.btnDown.Paint = function(self_panel, width, height)
        draw.RoundedBox(0, 0, 0, width , height, Color(87, 87, 87))
        surface.SetDrawColor(255, 255, 255)
        surface.SetMaterial(Material("stalplay/ui/ui_pda2_noice.png"))
        surface.DrawPartialTexturedRect( 0, 0, width, height, 809.5, 739.5, 20, 20, 1024, 1024 )
    end

    self.scroller_inv_storage = self:Add("DScrollPanel")
    self.scroller_inv_storage:SetSize(We(445), He(930))
    self.scroller_inv_storage:SetPos(We(50), He(100))
    self.scroller_inv_storage:MakePopup()

    local vbar = self.scroller_inv_storage:GetVBar()
    vbar.Paint = function(self_panel, width, height)
        draw.RoundedBox(0, 0, 0, width, height, Color(40,40,40))
    end
    vbar.btnUp.Paint = function(self_panel, width, height)
        draw.RoundedBox(0, 0, 0, width , height, Color(87, 87, 87))
        surface.SetDrawColor(255, 255, 255)
        surface.SetMaterial(Material("stalplay/ui/ui_pda2_noice.png"))
        surface.DrawPartialTexturedRect( 0, 0, width, height, 809.5, 692, 20, 18, 1024, 1024 )
    end
    vbar.btnGrip.Paint = function(self_panel, width, height)
        draw.RoundedBox(0, 0, 0, width, height, Color(87, 87, 87))
    end
    vbar.btnDown.Paint = function(self_panel, width, height)
        draw.RoundedBox(0, 0, 0, width , height, Color(87, 87, 87))
        surface.SetDrawColor(255, 255, 255)
        surface.SetMaterial(Material("stalplay/ui/ui_pda2_noice.png"))
        surface.DrawPartialTexturedRect( 0, 0, width, height, 809.5, 739.5, 20, 20, 1024, 1024 )
    end

    self.storageInventory = self.scroller_inv_storage:Add("ixInventory")
    self.storageInventory:SetTitle()
    self.storageInventory:SetDraggable(false)

    self.storageMoney = self:Add("ixStorageMoney")
    self.storageMoney:SetVisible(false)
    self.storageMoney:SetSize(ScrW() * .09, ScrH() * .033)
    self.storageMoney:SetPos(ScrW() * .134, ScrH() * .957)
    self.storageMoney.OnTransfer = function(_, amount)
        net.Start("ixStorageMoneyTake")
            net.WriteUInt(self.storageID, 32)
            net.WriteUInt(amount, 32)
        net.SendToServer()
    end
    ix.gui.inv1 = self.scroller_inv_me:Add("ixInventory")
    ix.gui.inv1:SetTitle()
    ix.gui.inv1:SetDraggable(false)
end
    
    
function PANEL:CreateExitButton()
	self.CloseButton = self:Add("DButton")
	self.CloseButton:SetFont("ixChatFont")
	self.CloseButton:SetSize(We(300), He(40))  -- Example size, adjust as needed
	self.CloseButton:SetPos((ScrW() - 300) / 2, ScrH() - 50)  -- Centered at the bottom
	self.CloseButton:SetText("Закрыть")
	self.CloseButton:SetTextColor(Color(255, 255, 255))
	self.CloseButton:MakePopup()
	self.CloseButton.Paint = function(s, w, h)
		-- RoundedBox(radius, x, y, width, height, color)
		draw.RoundedBox(0, 0, 0, w, h, Color(50, 50, 50, 200)) -- You can change the color and radius as needed
	end
	self.CloseButton.DoClick = function(this)
        surface.PlaySound("anomalyzone/ui_sounds/item_use/anomaly_1_5_2/inv_torch.ogg")
        net.Start("ixStorageClose")
        net.SendToServer()
        self:Remove()
	end
end
    
    
function PANEL:Init()
    if (IsValid(ix.gui.openedStorage)) then
        ix.gui.openedStorage:Remove()
    end

    ix.gui.openedStorage = self

    self:SetSize(ScrW(), ScrH())
    self:SetPos(0, 0)
    self:SetFadeTime(0.25)
    self:SetFrameMargin(4)
    self:CreateInventory()
    --self:CreateInventorySlots()
    self:CreateExitButton()
    self:DrawPlayerModel()

    --self.localMoney = self:Add("ixStorageMoney")
    --self.localMoney:SetVisible(false)
    --self.localMoney:SetSize(ScrW() * .09, ScrH() * .033)
    --self.localMoney:SetPos(ScrW() * .624, ScrH() * .957)
    --self.localMoney:SetLeft(true)
    --self.localMoney.OnTransfer = function(_, amount)
    --    net.Start("ixStorageMoneyGive")
    --        net.WriteUInt(self.storageID, 32)
    --        net.WriteUInt(amount, 32)
    --    net.SendToServer()
    --end
--
    --self:SetAlpha(255)
end
    
    
function PANEL:OnChildAdded(panel)
    panel:SetPaintedManually(true)
end
    
function PANEL:SetLocalInventory(inventory)
    if (IsValid(ix.gui.inv1) and !IsValid(ix.gui.menu)) then
        ix.gui.inv1:SetInventory(inventory)
    end
end
    
--function PANEL:SetLocalMoney(money)
--    if (!self.localMoney:IsVisible()) then
--        self.localMoney:SetVisible(true)
--        ix.gui.inv1:SetTall(ix.gui.inv1:GetTall() + self.localMoney:GetTall() + 2)
--    end
--
--    self.localMoney:SetMoney(money)
--end

function PANEL:SetStorageTitle(title)
    --
end

function PANEL:SetStorageInventory(inventory)
    self.storageInventory:SetInventory(inventory)
    ix.gui["inv" .. inventory:GetID()] = self.storageInventory
    self.Inventory = inventory
end

function PANEL:SetStorageMoney(money)
    if (!self.storageMoney:IsVisible()) then
        self.storageMoney:SetVisible(true)
    end

    self.storageMoney:SetMoney(money)
end
    

    
    
function PANEL:Paint(width, height)
    ix.util.DrawBlur(self, 5)

    draw.RoundedBox(0, 1450, 100, 447, 930, Color(50, 50, 50, 170))
    draw.RoundedBox(0, 1450, 50, 447, 50, Color(50, 50, 50, 200))
    draw.RoundedBox(0, 50, 100, 447, 930, Color(50, 50, 50, 170))
    draw.RoundedBox(0, 50, 50, 447, 50, Color(50, 50, 50, 200))
    draw.SimpleText("Инвентарь", "Trebuchet24", 1670, 60, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER)
    
    for _, v in ipairs(self:GetChildren()) do
        v:PaintManual()
    end
end

function PANEL:Remove()
    surface.PlaySound("anomalyzone/ui_sounds/item_use/anomaly_1_5_2/inv_close.ogg")
    gui.EnableScreenClicker(false)
    BaseClass.Remove(self)
end

function PANEL:OnRemove()
    if (!IsValid(ix.gui.menu)) then
        self.storageInventory:Remove()
        ix.gui.inv1:Remove()
        surface.PlaySound("anomalyzone/ui_sounds/item_use/anomaly_1_5_2/inv_close.ogg")
        gui.EnableScreenClicker(false)
    end
end

vgui.Register("ixStorageView", PANEL, "Panel")
