local PANEL = {}
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: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)
		ix.gui.invframe:OnRemove()
		surface.PlaySound("anomalyzone/ui_sounds/item_use/anomaly_1_5_2/inv_torch.ogg")
	end
end

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

timer.Create("CheckInventoryAnimation", 1, 0, function()
    local client = LocalPlayer()
    if client and IsValid(client) then
        local char = client:GetCharacter()
        if char then
            local inv = char:GetInventory()
            for k, v in pairs(inv:GetItems()) do
                if v.AnimStatus then
                    if IsValid(ix.gui.openedStorage) then
                        return
                    end
                    if IsValid(ix.gui.invframe) then
                        return 
                    end
                    if IsValid(ix.gui.vendorRemake) then
                        return 
                    end
                    v.AnimStatus = 0
                end
            end
        end
    end
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.IDScrollPanel = self:Add("DScrollPanel")
    self.IDScrollPanel:SetSize(We(445), He(930))
    self.IDScrollPanel:SetPos(We(1450), He(100))
    self.IDScrollPanel:MakePopup()

    local vbar = self.IDScrollPanel: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.IDScrollPanel: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()
	ix.gui.invframe = self
	self:SetTitle("")
	self:SetSize(ScrW(), ScrH())
	gui.EnableScreenClicker(true)
	self:SetPos(0, 0)
	self:ShowCloseButton(false)
    self:SetDraggable(false)
    self:SetBackgroundBlur(false)

    self:DrawPlayerModel()
	self:CreateInventory()
	self:CreateExitButton()
    self:CreateInventorySlots()

    surface.PlaySound("anomalyzone/ui_sounds/item_use/anomaly_1_5_2/inv_open.ogg")
end

function PANEL:PopulateInventoryTexts()
    if !LocalPlayer():GetCharacter() then return end

    draw.SimpleText(LocalPlayer():GetCharacter():GetMoney().." RU", "ixChatFont", ScrW() * .757, ScrH() * .11, color_white, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER)
	--draw.SimpleText( "Общий вес: "..carry .. "/"..max_weight.." кг", "ixChatFont", ScrW() * .85, ScrH() * .962, Color(99, 99, 99), TEXT_ALIGN_RIGHT, TEXT_ALIGN_RIGHT)
end

function PANEL:Paint(w,h)
    ix.util.DrawBlur(self, 5)

    draw.RoundedBox(0, We(1450), He(100), We(447), He(930), Color(50, 50, 50, 170))
    draw.RoundedBox(0, We(1450), He(50), We(447), He(50), Color(50, 50, 50, 200))
    draw.SimpleText("Инвентарь", "Trebuchet24", We(1670), He(60), Color(255, 255, 255, 255), TEXT_ALIGN_CENTER)
end

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

vgui.Register("NDInventory", PANEL, "DFrame")
