-- Клиентское меню строительства
local PLUGIN = PLUGIN

if CLIENT then
    function DrawItems(items, panel, mode, main)
        for i=1, #items do
            if items[i].cat == mode then
                local DButton = panel:Add( "DButton" )
                DButton:SetHeight(60)
                DButton:SetFont( "Font1" )
                DButton:SetText( items[i].name .. " | Запас: " .. items[i].count )
                DButton:SetIcon( items[i].icon )
                DButton:Dock( TOP )
                DButton:DockMargin( 0, 10, 0, 10 )
                DButton.Paint = function(self, w, h)
                    draw.RoundedBox(5, 0, 0, w, h, Color(0, 0, 0, 255))
                    draw.RoundedBox(5, 2, 2, w-4, h-4, Color(200, 200, 200, 255))
                end
                DButton.DoClick = function()
                    if items[i].model != nil then
                        net.Start("buildtrench")
                        net.WriteInt(i, 13)
                        net.SendToServer()
                        main:Close()
                    end
                    if items[i].entity != nil then  
                        net.Start("setentity")
                        net.WriteInt(i, 13)
                        net.SendToServer()
                        main:Close()
                    end
                end
            end
        end
    end
    surface.CreateFont("Font3", {
        font = "Arial",
        extended = true,
        weight = 900,
        size = 24
    })
    surface.CreateFont("Font2", {
        font = "Arial",
        extended = true,
        weight = 800,
        size = 20
    })
    surface.CreateFont("Font", {
        font = "Arial",
        extended = true,
        size = 20
    })
    surface.CreateFont("Font1", {
        font = "Arial",
        weight = 900,
        extended = true,
        size = 20
    })
    local faded_black = Color(100, 100, 100, 0)

    net.Receive("openmenu", function(len, ply)
        local alpha1 = 255
        local alpha2 = 0
        local alpha3 = 0
        local alpha4 = 0  
        local catlabel
        local DScrollPanel
        local items = net.ReadTable()
        local mode = 0
        local main = vgui.Create("DFrame")
        main:SetSize(500, 500) 
        main:Center()
        main:SetTitle(" ")
        main:SetDraggable(false)
        main:ShowCloseButton(false)
        main:MakePopup()
        main.Paint = function(self, w, h)
            draw.RoundedBox(5, 0, 0, w, h, faded_black)
        end

        local p = vgui.Create( "DPanel", main )
        p:Dock( TOP )
        p:DockMargin( 0, 0, 0, 0 )
        p:SetHeight(40)
        p.Paint = function(self, w, h)
            draw.RoundedBox(5, 0, 0, w, h, Color(0, 0, 0, 255))
            draw.RoundedBox(5, 2, 2, w-4, h-4, Color(150, 150, 150, 255))
        end
        local namelabel = vgui.Create( "DLabel", p )
        namelabel:Dock( LEFT )   
        namelabel:DockMargin( 20, 0, 20, 0 )  
        namelabel:SetSize(200, 20)  
        namelabel:SetFont( "Font" )
        namelabel:SetText( "Инженерный комплект" )

        local closebutton = vgui.Create( "DButton", p ) 
        closebutton:Dock( RIGHT )   
        closebutton:DockMargin( 20, 0, 20, 0 )   
        closebutton:SetText( " " )
        closebutton:SetImage("entities/closebutton.png")                
        closebutton:SetSize( 40, 40 )    
        closebutton.Paint = function(self, w, h)
            draw.RoundedBox(2, 0, 0, w, h, Color(150, 150, 150, 0))
        end 
        closebutton.DoClick = function()   
            main:Close()             
        end

        local ppp = vgui.Create( "DPanel", main )
        ppp:Dock( TOP )
        ppp:DockMargin( 0, 20, 0, 20 )
        ppp:SetHeight(60)
        ppp.Paint = function(self, w, h)
            draw.RoundedBox(5, 0, 0, w, h, Color(0, 0, 0, 255))
            draw.RoundedBox(5, 2, 2, w-4, h-4, Color(150, 150, 150, 255))
        end

        local entity = vgui.Create( "DButton", ppp ) 
        entity:Dock( LEFT )   
        entity:DockMargin( 70, 10, 0, 10 )   
        entity:SetText( " " )
        entity:SetImage("entities/entity.png")                
        entity:SetHeight(40)
        entity:SetWidth(40)
        entity.Paint = function(self, w, h)
            draw.RoundedBox(5, 0, 0, w, h, Color(120, 120, 120, alpha1))
        end 
        entity.DoClick = function()   
            DScrollPanel:GetCanvas():Clear()
            DrawItems(items, DScrollPanel, 0, main)  
            catlabel:SetText( "       Объекты" )     
            alpha1 = 255
            alpha2 = 0
            alpha3 = 0
            alpha4 = 0  
        end

        local tier1 = vgui.Create( "DButton", ppp ) 
        tier1:Dock( LEFT )   
        tier1:DockMargin( 70, 10, 0, 10 )     
        tier1:SetText( " " )
        tier1:SetImage("entities/tier1.png")                
        tier1:SetHeight(40)
        tier1:SetWidth(40)  
        tier1.Paint = function(self, w, h)
            draw.RoundedBox(5, 0, 0, w, h, Color(120, 120, 120, alpha2))
        end 
        tier1.DoClick = function()   
            DScrollPanel:GetCanvas():Clear()
            DrawItems(items, DScrollPanel, 1, main)   
            catlabel:SetText( "Легкие укрепления" ) 
            alpha1 = 0
            alpha2 = 255
            alpha3 = 0
            alpha4 = 0        
        end

        local tier2 = vgui.Create( "DButton", ppp ) 
        tier2:Dock( LEFT )   
        tier2:DockMargin( 70, 10, 0, 10 )  
        tier2:SetText( " " )
        tier2:SetImage("entities/tier2.png")                
        tier2:SetHeight(40)
        tier2:SetWidth(40)    
        tier2.Paint = function(self, w, h)
            draw.RoundedBox(5, 0, 0, w, h, Color(120, 120, 120, alpha3))
        end 
        tier2.DoClick = function()   
            DScrollPanel:GetCanvas():Clear()
            DrawItems(items, DScrollPanel, 2, main)   
            catlabel:SetText( "Средние укрепления" )    
            alpha1 = 0
            alpha2 = 0
            alpha3 = 255
            alpha4 = 0 
        end

        local tier3 = vgui.Create( "DButton", ppp ) 
        tier3:Dock( LEFT )   
        tier3:DockMargin( 70, 10, 0, 10 )   
        tier3:SetText( " " )
        tier3:SetImage("entities/tier3.png")                
        tier3:SetHeight(40)
        tier3:SetWidth(40) 
        tier3.Paint = function(self, w, h)
            draw.RoundedBox(5, 0, 0, w, h, Color(120, 120, 120, alpha4))
        end 
        tier3.DoClick = function()  
            DScrollPanel:GetCanvas():Clear()
            DrawItems(items, DScrollPanel, 3, main)
            catlabel:SetText( "Тяжелые укрепления" )
            alpha1 = 0
            alpha2 = 0
            alpha3 = 0
            alpha4 = 255 
        end

        local pp = vgui.Create( "DPanel", main )
        pp:Dock( TOP )
        pp:DockMargin( 0, 0, 0, 20  )
        pp:SetHeight(300)
        pp.Paint = function(self, w, h)
            draw.RoundedBox(5, 0, 0, w, h, Color(0, 0, 0, 255))
            draw.RoundedBox(5, 2, 2, w-4, h-4, Color(150, 150, 150, 255))
        end

        catlabel = vgui.Create( "DLabel", pp )
        catlabel:Dock( TOP )   
        catlabel:DockMargin( 170, 20, 0, 10 )  
        catlabel:SetSize(350, 20)  
        catlabel:SetFont( "Font" )
        catlabel:SetText( "       Объекты" )

        DScrollPanel = vgui.Create( "DScrollPanel", pp )
        DScrollPanel:Dock( TOP )
        DScrollPanel:SetHeight(240) 
        DScrollPanel:DockMargin( 6, 6, 6, 6  )
        DrawItems(items, DScrollPanel, 0, main)
    end)  
end
