local bg = Material('surrounded/vgui_bg.png')
local lang = SuR.Language

function SuR:OpenBuyMenu()
    local df = vgui.Create("DFrame")
    df:SetSize(We(600), He(425))
    df:Center()
    df:SetTitle("")
    df:MakePopup()
    df:ShowCloseButton(false)
    df.Paint = function(self, w, h)
        surface.SetMaterial(bg)
        surface.SetDrawColor(0,0,0,230)
        surface.DrawTexturedRect(0, 0, w, h)

        draw.SimpleText(lang["buymenu_main"], "SuR_SmallFont2", We(30), He(15), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
    end

    local db = vgui.Create("DButton", df)
    db:SetSize(We(32), He(32))
    db:SetPos(We(540),He(15))
    db:SetText("")
    function db:Paint(w, h)
        if self:IsHovered() then
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, Color(200,255,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        else
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
    end
    function db:DoClick()
        surface.PlaySound("surrounded/click.mp3")
        df:Remove()
    end
    function db:OnCursorEntered()
        surface.PlaySound("surrounded/tick.wav")
    end

    local frame = vgui.Create("DPanel", df)
    frame:SetPos(We(30), He(50))
    frame:SetSize(We(530), He(350))
    function frame:Paint(w, h) end

    local propertySheet = vgui.Create("DPropertySheet", frame)
    propertySheet:Dock(FILL)

    for categoryName, items in SortedPairs(SuR.Config.TierWeapons) do
        if categoryName == "Melee" or categoryName == "Other" or categoryName == "Tier1" then continue end

        local panel = vgui.Create("DPanel", frame)
        panel:Dock(FILL)
        panel.Paint = function(self, w, h)
            surface.SetDrawColor(0,0,0)
            surface.DrawRect(0, 0, w, h)
        end

        local scrollPanel = vgui.Create("DScrollPanel", panel)
        scrollPanel:Dock(FILL)

        local list = vgui.Create("DIconLayout", scrollPanel)
        list:Dock(FILL)
        list:SetSpaceY(5)
        list:SetSpaceX(5)    

        for _, item in ipairs(items) do
            local wep = weapons.Get(item)
            if !wep then continue end

            local price = 50
            if categoryName == "Tier2" then price = SuR.Config.Tier2_Price end
            if categoryName == "Tier3" then price = SuR.Config.Tier3_Price end
            if categoryName == "Tier4" then price = SuR.Config.Tier4_Price end
            if categoryName == "Tier5" then price = SuR.Config.Tier5_Price end

            local itemPanel = list:Add("DPanel")
            itemPanel:SetSize(530, 100)
            itemPanel.Paint = function(self, w, h)
                surface.SetDrawColor(50,50,50)
                surface.DrawRect(0, 0, w, h)
            end

            local icon = vgui.Create("DImage", itemPanel)
            icon:SetImage("entities/"..item..".png")
            icon:SetSize(We(88), He(88))
            icon:SetPos(We(5), He(5))

            local nameLabel = vgui.Create("DLabel", itemPanel)
            nameLabel:SetText(wep.PrintName)
            nameLabel:SetFont("SuR_SmallFont2")
            nameLabel:SizeToContents()
            nameLabel:SetPos(We(100), He(10))

            local priceLabel = vgui.Create("DLabel", itemPanel)
            priceLabel:SetText("Price: $" .. price)
            priceLabel:SetFont("SuR_SmallFont1")
            priceLabel:SizeToContents()
            priceLabel:SetPos(We(100), He(35))

            local buyButton = vgui.Create("DButton", itemPanel)
            buyButton:SetText(lang["buymenu_buy"])
            buyButton:SetFont("SuR_SmallFont1")
            buyButton:SetPos(We(100), He(60))
            buyButton:SetSize(100, 30)
            buyButton.DoClick = function()
                net.Start("SuR.BuyMenu")
                net.WriteString(item)
                net.SendToServer()

                if LocalPlayer():GetNWInt('Money') < price then
                    surface.PlaySound("buttons/combine_button_locked.wav")
                else
                    surface.PlaySound("buttons/lightswitch2.wav")
                end
            end
        end

        propertySheet:AddSheet(categoryName, panel, "icon16/folder.png")
    end
end

