AddCSLuaFile()

if (SERVER) then
    return
end

require("mw_utils")

local openSound = "mw/user_interface/main_iw8/iw8_wheel_popup.wav"
local detachSound = "mw/user_interface/main_iw8/iw8_mainmenu_deny_v1.wav"
local attachSound = "mw/user_interface/main_iw8/iw8_general_purchase_confirm_v1.wav"
local selectAttachmentSound = "mw/user_interface/mp/iw8_radar_drone_marker2.wav"
local closeAttachmentsSound = "mw/user_interface/mp/iw8_outofbounds_popup.wav"
local closeSound = "mw/user_interface/mp/iw8_scoreboard_popup_extra.wav"
local scrollSound = "mw/user_interface/ui_text_beep1.wav"
local hoverSounds = {
    "mw/user_interface/main_iw8/iw8_general_updownmovement1_v1.wav",
    "mw/user_interface/main_iw8/iw8_general_updownmovement2_v1.wav",
    "mw/user_interface/main_iw8/iw8_general_updownmovement3_v1.wav",
    "mw/user_interface/main_iw8/iw8_general_updownmovement4_v1.wav",
    "mw/user_interface/main_iw8/iw8_general_updownmovement5_v1.wav",
    "mw/user_interface/main_iw8/iw8_general_updownmovement6_v1.wav"
}
local hoverAttachmentSounds = {
    "mw/user_interface/main_iw8/iw8_mpmenu_tabsmove1_v1.wav",
    "mw/user_interface/main_iw8/iw8_mpmenu_tabsmove2_v1.wav",
    "mw/user_interface/main_iw8/iw8_mpmenu_tabsmove3_v1.wav",
    "mw/user_interface/main_iw8/iw8_mpmenu_tabsmove4_v1.wav",
    "mw/user_interface/main_iw8/iw8_mpmenu_tabsmove5_v1.wav",
    "mw/user_interface/main_iw8/iw8_mpmenu_tabsmove6_v1.wav"
}
local selectCategorySound = "mw/user_interface/main_iw8/iw8_leavelobby_alert_v1.wav"
local presetSound = "mw/user_interface/aar/ui_aar_progress_circle_stop.wav"
local savePresetSound = "mw/user_interface/aar/ui_aar_operator_complete_reveal.wav"
local removePresetSound = "mw/user_interface/mp/iw8_restock_lethals_v1.wav"
local resetSound = "mw/user_interface/mp/iw8_restock_lethals_v1.wav"
local randomSound = "mw/user_interface/mp/mp_ui_splash_notify_01.wav"
local favoriteSound = "mw/user_interface/ui_motiontracker_pong1.wav"
local unfavoriteSound = "mw/user_interface/ui_motiontracker_pong2.wav"
local blurMaterial = Material("mg/blur")
local buttonGlowMaterial = Material("mg/buttonglow")
local removeButtonGlowMaterial = Material("mg/removeattachmentbutton")
local customizeMenuOpenMaterial = Material("mg/customizemenuopen")
local removeAttachmentMaterial = Material("mg/removeattachment")
local removePresetMaterial = Material("mg/removenew")
local closeAttachmentsMaterial = Material("mg/closeattachments")
local rightClickMaterial = Material("mg/rightclick")
local cursorGlowMaterial = Material("mg/cursorglow")
local presetsMaterial = Material("mg/presets")
local defaultPresetMaterial = Material("mg/defaultpreset")
local noMaterial = Material("mg/d")
local addPresetMaterial = Material("mg/addpreset")
local resetMaterial = Material("mg/clear")
local randomMaterial = Material("mg/random")
local favoriteMaterial = Material("mg/favorite")
local bookmarkMaterial = Material("mg/bookmark")

local blackColor = Color(0, 0, 0, 150)
local whiteColor = Color(255, 255, 255, 200)
local greyColor = Color(255, 255, 255, 150)
local shadowColor = Color(0, 0, 0, 20)
local backgroundErrorColor = Color(50, 0, 0, 150)
local errorColor = Color(150, 0, 0, 255)
local greenColor = Color(0, 210, 20, 255)
local redColor = Color(230, 0, 30, 255)
local yellowColor = Color(255, 170, 20, 255)

local MWBLTLXCoordinate = {
    Text = {
        ["de"] = 230,
        ["en"] = 220,
        ["es-ES"] = 340,
        ["fr"] = 360,
        ["pt-BR"] = 280,
        ["ru"] = 280,
        ["th"] = 290,
        ["zh-CN"] = 180,
        ["zh-TW"] = 180
    },
    Line = {
        ["de"] = 320,
        ["en"] = 310,
        ["es-ES"] = 430,
        ["fr"] = 450,
        ["pt-BR"] = 370,
        ["ru"] = 400,
        ["th"] = 420,
        ["zh-CN"] = 280,
        ["zh-TW"] = 280 
    }
}

--opening the menu if no mgbase_customize bind (context menu)

local function closeCustomizationMenu()
    if (!IsValid(MW_CUSTOMIZEMENU)) then
        return
    end

    gui.EnableScreenClicker(false)
    surface.PlaySound(closeSound)
    
    if (IsValid(MW_CUSTOMIZEMENU)) then
        MW_CUSTOMIZEMENU:Remove()
    end
end

local function validWeapon(weapon)
    return IsValid(weapon) && weapon:GetOwner() == LocalPlayer() && weapon:HasFlag("Customizing")
end

--removing panel if already there (reload code)
if (IsValid(MW_CUSTOMIZEMENU)) then
    MW_CUSTOMIZEMENU:Remove()
end

MW_CUSTOMIZEMENU = nil

local function makeCloseButton(panel, panelToClose)
    local closeButton = vgui.Create("DButton", panel)
    closeButton:Dock(RIGHT)
    closeButton:SetSize(40, 40)
    closeButton:SetText("")
    
    local closeButtonDownColor = Color(100, 100, 100, 200)
    function closeButton:Paint(w, h)
        surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, whiteColor.a)
        self:DrawOutlinedRect()
        
        surface.SetMaterial(closeAttachmentsMaterial)
        
        local color = self:IsDown() && closeButtonDownColor || whiteColor
        surface.SetDrawColor(color.r, color.g, color.b, color.a)
        surface.DrawTexturedRect(5, 5, w - 10, h - 10)
    end
    
    function closeButton:DoClick()
        surface.PlaySound(closeAttachmentsSound)
        panelToClose:Remove()
    end
    
    return closeButton
end

local function createButtonControls(panel, leftClickName, rightClickName, wi, he)
    if (IsValid(panel.controls)) then
        return
    end

    local ml, mt, mr, mb = panel:GetDockMargin()
    local posx, posy = panel:LocalToScreen(panel:GetX(), panel:GetY())
    posx = posx - wi - 5
    posy = posy - mt

    panel.controls = vgui.Create("DPanel")
    panel.controls:SetPos(posx - 100, posy)
    panel.controls:MoveTo(posx, posy, 0.1)
    panel.controls:SetSize(wi, he)
    panel.controls:NoClipping(true)
    panel.controls:SetMouseInputEnabled(false)
    panel.controls.parent = panel
    function panel.controls:Think()
        if (!IsValid(self.parent)) then
            self:Remove()
        end
    end
    function panel.controls:Paint(w, h)
        if (rightClickName != nil) then
            surface.SetDrawColor(blackColor.r, blackColor.g, blackColor.b, blackColor.a)
            surface.SetMaterial(buttonGlowMaterial)
            surface.DrawTexturedRectRotated(w * 0.5, h * 0.5, h, w, 90)

            surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, whiteColor.a)
            surface.SetMaterial(rightClickMaterial)
            surface.DrawTexturedRect(w - 32, h * 0.5 - 15, 20, 30)
            
            draw.SimpleText(rightClickName, "mgbase_control", w - 45, h * 0.5, whiteColor, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER)
            --draw.SimpleText(": FAVORITE", "mgbase_statPositive", 0, -20, whiteColor, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
        end
    end
end

local function removeButtonControls(panel)
    if (IsValid(panel.controls)) then
        panel.controls:Remove()
    end
end

local function makeScrollBar(panel, dock)
    local scroll = vgui.Create("DScrollPanel", panel)
    scroll:Dock(FILL)
    
    local sbar = scroll:GetVBar()
    sbar:Dock(dock)
    sbar.LastScrollValue = sbar:GetOffset()
    function sbar:Paint(w, h)
    end
    function sbar:Think()
        if (self:GetOffset() != self.LastScrollValue && math.abs(self:GetOffset() - self.LastScrollValue) > 10) then
            surface.PlaySound(scrollSound)
            self.LastScrollValue = self:GetOffset()
        end
    end
    function sbar.btnUp:Paint(w, h)
        surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, whiteColor.a)
        surface.DrawRect(w * 0.5 - 2, 0, 4, h)
    end
    function sbar.btnDown:Paint(w, h)
        surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, whiteColor.a)
        surface.DrawRect(w * 0.5 - 2, 0, 4, h)
    end
    function sbar.btnGrip:Paint(w, h)
        surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, whiteColor.a)
        surface.DrawRect(w * 0.5 - 1, 10, 2, h - 20)
    end
    
    return scroll
end

local function makePopupMenu()
    local background = vgui.Create("DButton", MW_CUSTOMIZEMENU)
    background:SetPos(0, 0)
    background:SetSize(ScrW(), ScrH())
    background:SetText("")
    background:Center()
    background:SetCursor("arrow")
    
    function background:Think()
        if (LocalPlayer():KeyDown(IN_USE)) then
            self:Remove()
        end
    end
    
    function background:Paint(width, height)
        Derma_DrawBackgroundBlur(self, self.m_fCreateTime)
        
        surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, 3)
        surface.SetMaterial(cursorGlowMaterial)
        surface.DrawTexturedRect(gui.MouseX() - 175, gui.MouseY() - 175, 350, 350)

        draw.SimpleText("im taking over", "DermaDefault", gui.MouseX(), gui.MouseY(), Color(whiteColor.r, whiteColor.g, whiteColor.b, 1), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
    end
    
    function background:DoClick()
        surface.PlaySound(closeAttachmentsSound)
        self:Remove()
    end
    
    return background
end

local function closePresetInfo(panel)
    if (!IsValid(panel.hover)) then
        return
    end
    
    panel.hover:Remove()
end

local function openPresetInfo(panel, preset)
    if (preset == nil || preset.Attachments == nil) then
        return
    end
    
    local atts = {}

    for _, attClass in pairs(preset.Attachments) do
        if (MW_ATTS[attClass] != nil) then
            table.insert(atts, attClass)
        end
    end

    if (!IsValid(panel.hover)) then
        panel.hover = vgui.Create("DPanel")
        panel.hover.parent = panel
        local x, y = panel:LocalToScreen(panel:GetPos())
        panel.hover:SetPos(x + panel:GetWide() + 20, y + 20)
        panel.hover:SetSize(400, 50 * (#atts + 1))
        panel.hover:SetMouseInputEnabled(false)
        panel.hover:NoClipping(true)
        function panel.hover:Think()
            if (!IsValid(self.parent)) then
                self:Remove()
                return
            end
            
            self:SetPos(math.Clamp(gui.MouseX() + 30, 0, ScrW() - self:GetWide()), math.Clamp(gui.MouseY() + 30, 0, ScrH() - self:GetTall()))
        end
        function panel.hover:Paint(w, h)
            --[[surface.SetMaterial(blurMaterial)
            surface.SetDrawColor(255, 255, 255, 255)
            
            for i = 1, 10, 1 do
                render.UpdateScreenEffectTexture()
                self:DrawTexturedRect()
            end
            
            surface.SetDrawColor(blackColor.r, blackColor.g, blackColor.b, blackColor.a)
            self:DrawFilledRect()
            
            --white fade
            surface.SetMaterial(buttonGlowMaterial)
            surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, whiteColor.a * 0.02)
            surface.DrawTexturedRect(0, h * 0.5, w, h * 0.5)]]
        end
        function panel.hover:PaintOver(w, h)
            surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, whiteColor.a)
            
            --long line on the right
            --surface.DrawLine(w + 5, 15, w + 5, h - 15)
            
            --corner lines
            surface.DrawLine(-5, -5, -5, 5)
            surface.DrawLine(-5, -5, 5, -5)
        end
        
        local attsGrid = vgui.Create("DGrid", panel.hover)
        attsGrid:SetPos(0, 0)
        attsGrid:SetCols(1)
        attsGrid:SetColWide(panel.hover:GetWide())
        attsGrid:SetRowHeight(50)
        attsGrid:Dock(FILL)
        
        for i, attachmentClass in pairs(atts) do
            local attPanel = vgui.Create("DPanel")
            attPanel:SetSize(attsGrid:GetColWide(), attsGrid:GetRowHeight())

            function attPanel:Paint(w, h)
                surface.SetDrawColor(blackColor.r, blackColor.g, blackColor.b, blackColor.a)
                surface.DrawRect(5, 5, 40, 40)

                --glow hover
                surface.SetDrawColor(blackColor.r, blackColor.g, blackColor.b, blackColor.a)
                surface.SetMaterial(buttonGlowMaterial)
                surface.DrawTexturedRectRotated(150, 25, 40, 200, 270)

                local attachment = MW_ATTS[attachmentClass]
                local attColor = attachment.UIColor || whiteColor

                surface.SetDrawColor(attColor.r, attColor.g, attColor.b, 255)
                surface.SetMaterial(attachment.Icon)
                surface.DrawTexturedRect(8, h * 0.5 - 17, 35, 35)
                
                draw.SimpleText(attachment.Name, "mgbase_stat", 60, h * 0.5, attColor, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
                local c = Color(attColor.r, attColor.g, attColor.b, attColor.a * 0.5)
                draw.SimpleText(attachment.Name, "mgbase_statPositive", 60, h * 0.5, c, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
            end
            
            attsGrid:AddItem(attPanel)
        end
    end
end

local function createPresetPanel(preset, wepclass)
    --panel to hold button
    local presetPanel = vgui.Create("DPanel")
    function presetPanel:Paint(w, h)
    end
    
    local but = vgui.Create("DButton", presetPanel)
    but:SetText("")
    but:Dock(FILL)
    but:DockMargin(0, 5, 0, 5)
    but.HoverDelta = 0
    but.ClickDelta = 0
    but.bWasHovered = false
    but.FavoriteDelta = 0
    but.bWasFavorite = preset != nil && mw_utils.IsAssetFavorite(wepclass, preset.ClassName) || false
    
    function but:IsAllowed()
        return true
    end

    function but:Paint(width, height)
        if (self:IsHovered()) then
            self.HoverDelta = math.Approach(self.HoverDelta, 1, math.min(10 * RealFrameTime(), 0.1))
            
            if (!self.bWasHovered) then
                surface.PlaySound(hoverAttachmentSounds[math.random(1, #hoverAttachmentSounds)])
            end
            
            self.bWasHovered = true
        else
            self.HoverDelta = math.Approach(self.HoverDelta, 0, math.min(10 * RealFrameTime(), 0.1))
            self.bWasHovered = false
        end
        
        if (self:IsDown()) then
            self.ClickDelta = math.Approach(self.ClickDelta, 1, math.min(10 * RealFrameTime(), 0.1))
        else
            self.ClickDelta = math.Approach(self.ClickDelta, 0, math.min(10 * RealFrameTime(), 0.1))
        end

        self.FavoriteDelta = math.Approach(self.FavoriteDelta, 0, math.min(3 * RealFrameTime(), 0.3))
        
        local bAllowed = self:IsAllowed()
        local currentColor = bAllowed && blackColor || backgroundErrorColor
        
        --background
        surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a)
        self:DrawFilledRect()
        
        currentColor = bAllowed && whiteColor || errorColor
        
        --click hold
        surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a * 0.15 * self.ClickDelta)
        self:DrawFilledRect()
        
        --border hover
        surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a * self.HoverDelta)
        self:DrawOutlinedRect()
        
        --glow hover
        surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a * 0.15 * (self.HoverDelta - self.ClickDelta))
        surface.SetMaterial(buttonGlowMaterial)
        surface.DrawTexturedRect(0, height * 0.5, width, height * 0.5)
        
        --preset name
            
        if (self.FavoriteDelta > 0) then
            surface.SetDrawColor(yellowColor.r, yellowColor.g, yellowColor.b, yellowColor.a * self.FavoriteDelta)
            self:DrawFilledRect()
        end

        if (preset == nil) then
            return
        end

        local hoverColor = Color(currentColor.r, currentColor.g, currentColor.b, currentColor.a * self.HoverDelta)
        draw.SimpleText(preset.Name, "mgbase_attSlotAttachmentInUse:hover", width * 0.5 - 2, height * 0.5, hoverColor, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        draw.SimpleText(preset.Name, "mgbase_attSlotAttachmentInUse", width * 0.5 - 2, height * 0.5, currentColor, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, shadowColor)

        if (!preset._bUserGenerated) then
            surface.SetDrawColor(255, 255, 255, Lerp(self.HoverDelta, 5, 50))
            surface.SetMaterial(defaultPresetMaterial)
            surface.DrawTexturedRect(width * 0.5 - 25, height * 0.5 - 20, 50, 40)
        end

        local bFav = mw_utils.IsAssetFavorite(wepclass, preset.ClassName)

        if (bFav != self.bWasFavorite) then
            self.FavoriteDelta = bFav && 1 || 0
            self.bWasFavorite = bFav
        end

        if (bFav) then
            surface.SetDrawColor(yellowColor.r, yellowColor.g, yellowColor.b, yellowColor.a)
            surface.SetMaterial(favoriteMaterial)
            surface.DrawTexturedRect(-1, -1, 16, 16)
        end
    end

    function but:PaintOver(width, height)
    end

    function but:Think()
        if (self:IsHovered()) then
            openPresetInfo(self, preset)
            if (preset != nil) then
                createButtonControls(self, nil, MWBLTL.Get("CuzMenu_Nom_Text1"), 200, self:GetTall())
            end
        else
            closePresetInfo(self)
            removeButtonControls(self)
        end
    end

    return presetPanel, but
end

local function openPresetsMenu(weapon)
    
    local background = makePopupMenu()
    local presets = mw_utils.GetPresetsForSWEP(weapon:GetClass())

    table.sort(presets, function(a, b)
        local aFav = mw_utils.IsAssetFavorite(weapon:GetClass(), a.ClassName)
        local bFav = mw_utils.IsAssetFavorite(weapon:GetClass(), b.ClassName)

        if (!aFav && bFav) then
            return false
        elseif (aFav && !bFav) then
            return true
        elseif ((aFav && bFav) || (!aFav && !bFav)) then
            if (!a._bUserGenerated && b._bUserGenerated) then
                return true
            elseif (a._bUserGenerated && !b._bUserGenerated) then
                return false
            elseif ((a._bUserGenerated && b._bUserGenerated) || (!a._bUserGenerated && !b._bUserGenerated)) then
                return a.Name < b.Name
            end
        end
    end)
    
    local menu = vgui.Create("DPanel", background)
    menu:SetSize(400, ScrH() * 0.6)
    menu:Center() 
    
    local x,y = menu:GetPos()
    menu:SetPos(x, ScrH())
    menu:MoveTo(x, y, 0.1, 0, -1)
    
    function menu:Paint(width, height)
    end
    
    local headerPanel = vgui.Create("DPanel", menu)
    headerPanel:SetText("")
    headerPanel:Dock(TOP)
    headerPanel:SetSize(0, 40)
    headerPanel:DockMargin(20, 0, 20, 10)
    function headerPanel:Paint(w, h)
        draw.SimpleText(MWBLTL.Get("CuzMenu_Nom_Text2"), "mgbase_attSlotMenu", 0, h * 0.5, whiteColor, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
    end 
    
    makeCloseButton(headerPanel, background)
    
    --ADD PRESET
    local addPresetPanel, addBut = createPresetPanel(nil, nil)
    addPresetPanel:SetParent(menu)
    addPresetPanel:SetSize(0, 80)
    addPresetPanel:SetPos(20, 40)
    addPresetPanel:DockMargin(20, 0, 20, 0)
    addPresetPanel:Dock(TOP)
    
    function addBut:IsAllowed()
        if (!validWeapon(weapon)) then
            return
        end
        
        for _, att in pairs(weapon:GetAllAttachmentsInUse()) do
            if (att.Index > 1) then
                return true
            end
        end
        
        return false
    end
    
    local presetInputRequirement = vgui.Create("EditablePanel", addBut)
    presetInputRequirement:Dock(FILL)
    presetInputRequirement:SetVisible(false)
    function presetInputRequirement:Paint()
        --surface.SetDrawColor(255, 0, 0, 255)
        --self:DrawFilledRect()
    end
    
    function presetInputRequirement:Think()
        local x, y = addBut:LocalToScreen(addBut:GetX(), addBut:GetY())
        self:SetPos(x, y - 5) --stupid fucking workaround because of makepopup
    end
    
    local presetInput = vgui.Create("DTextEntry", presetInputRequirement)
    presetInput:Dock(FILL)
    presetInput:DockMargin(10, 0, 10, 0)
    presetInput:SetTabbingDisabled(true)
    presetInput:SetPaintBackground(false)
    presetInput:SetMultiline(false)
    presetInput:SetFont("mgbase_attSlotAttachmentInUse")
    presetInput:SetCursorColor(Color(255, 255, 255, 255))
    presetInput:SetPlaceholderText(MWBLTL.Get("CuzMenu_Nom_Text3"))
    presetInput:SetEditable(true)
    presetInput:SetTextColor(whiteColor)
    
    function presetInput:AllowInput(char)
        return #self:GetText() >= 32
    end
    
    function presetInput:OnLoseFocus()
        self:GetParent():SetVisible(false)
        self:SetText("")
    end
    
    function presetInput:OnEnter()
        local atts = {}
        
        for slot, att in pairs(weapon:GetAllAttachmentsInUse()) do
            if (att.Index > 1) then
                atts[#atts + 1] = att.ClassName
            end
        end
        
        mw_utils.SavePreset(weapon:GetClass(), self:GetText(), atts)
        surface.PlaySound(savePresetSound)
        background:Remove()
        openPresetsMenu(weapon)
    end

    local oldPaint = addBut.Paint
    function addBut:Paint(width, height)
        oldPaint(self, width, height)
        
        local bAllowed = self:IsAllowed()
        local currentColor = bAllowed && whiteColor || errorColor
        
        --icon
        if (!presetInputRequirement:IsVisible()) then
            surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, Lerp(self.HoverDelta, 150, 255))
            surface.SetMaterial(addPresetMaterial)
            surface.DrawTexturedRect(width * 0.5 - 20, 15, 40, 40)
        end
    end
    
    function addBut:DoClick()
        if (!self:IsAllowed()) then
            surface.PlaySound(presetSound)
            return
        end
        
        presetInputRequirement:SetVisible(true)
        presetInputRequirement:MakePopup()
        presetInput:RequestFocus()
    end
    
    --DIVIDER BETWEEN ADD PRESET AND PRESETS
    local divider = vgui.Create("DPanel", menu)
    divider:SetSize(0, 1)
    divider:Dock(TOP)
    divider:DockMargin(20, 5, 20, 5)
    function divider:Paint(w, h)
        surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b)
        self:DrawFilledRect()
    end
    
    local scroll = makeScrollBar(menu, RIGHT)
    
    local presetsGrid = vgui.Create("DGrid", scroll)
    presetsGrid:SetPos(0, 0)
    presetsGrid:SetCols(1)
    presetsGrid:SetColWide(menu:GetWide())
    presetsGrid:SetRowHeight(80)
    presetsGrid:Dock(FILL)
    presetsGrid:DockMargin(20, 0, 0, 0)
     
    for _, preset in pairs(presets) do
        local presetPanel, but = createPresetPanel(preset, weapon:GetClass())
        presetPanel:SetSize(presetsGrid:GetColWide() * 0.9, presetsGrid:GetRowHeight())

        function but:DoRightClick()
            if (!mw_utils.IsAssetFavorite(weapon:GetClass(), preset.ClassName)) then
                mw_utils.FavoriteAsset(weapon:GetClass(), preset.ClassName)
                surface.PlaySound(favoriteSound)
            else
                mw_utils.UnfavoriteAsset(weapon:GetClass(), preset.ClassName)
                surface.PlaySound(unfavoriteSound)
            end
        end
            
        function but:DoClick()
            if (!self:IsAllowed()) then
                surface.PlaySound(selectAttachmentSound)
                return
            end
            
            mw_utils.LoadPreset(weapon, preset)
            
            surface.PlaySound(presetSound)

            if (preset.UISound != nil) then
                surface.PlaySound(preset.UISound)
            end
            
            background:Remove()
        end

        if (preset._bUserGenerated) then
            local removeButton = vgui.Create("DButton", but)
            removeButton:SetSize(24, 24)
            removeButton:SetPos(330, 5)
            removeButton:SetText("")
            removeButton.HoverDelta = 0
            removeButton.ClickDelta = 0
            removeButton.bWasHovered = false

            function removeButton:DoClick()
                mw_utils.RemovePreset(preset.ClassName)
                background:Remove()
                surface.PlaySound(removePresetSound)
                openPresetsMenu(weapon)
            end

            function removeButton:Paint(w, h)
                if (self:IsHovered()) then
                    self.HoverDelta = math.Approach(self.HoverDelta, 1, math.min(10 * RealFrameTime(), 0.1))
                    
                    if (!self.bWasHovered) then
                        surface.PlaySound(hoverAttachmentSounds[math.random(1, #hoverAttachmentSounds)])
                    end
                    
                    self.bWasHovered = true
                else
                    self.HoverDelta = math.Approach(self.HoverDelta, 0, math.min(10 * RealFrameTime(), 0.1))
                    self.bWasHovered = false
                end
                
                if (self:IsDown()) then
                    self.ClickDelta = math.Approach(self.ClickDelta, 1, math.min(10 * RealFrameTime(), 0.1))
                else
                    self.ClickDelta = math.Approach(self.ClickDelta, 0, math.min(10 * RealFrameTime(), 0.1))
                end

                surface.SetMaterial(removePresetMaterial)
                surface.SetDrawColor(redColor.r, redColor.g, redColor.b, Lerp(self.HoverDelta * 0.25 + self.ClickDelta * 0.75, redColor.a * 0.1, redColor.a))
                self:DrawTexturedRect()
            end
        end
        
        presetsGrid:AddItem(presetPanel)
    end

    --SPAWN METHOD
    local presetSpawnPanel = vgui.Create("DPanel", menu)
    presetSpawnPanel:Dock(BOTTOM)
    presetSpawnPanel:SetSize(0, 50)
    presetSpawnPanel:DockMargin(0, 10, 0, 0)
    function presetSpawnPanel:Paint(w, h)
    end

    local options = {[0] = MWBLTL.Get("CuzMenu_Method_Text1"), [1] = MWBLTL.Get("CuzMenu_Method_Text2"), [2] = MWBLTL.Get("CuzMenu_Method_Text3"), [3] = MWBLTL.Get("CuzMenu_Method_Text4")}
    local method = math.Clamp(GetConVar("mgbase_presetspawnmethod"):GetInt(), 0, 3)

    local presetSpawnLabel = vgui.Create("DLabel", presetSpawnPanel)
    presetSpawnLabel:Dock(LEFT)
    presetSpawnLabel:DockMargin(20, 5, 0, 5)
    presetSpawnLabel:SetFont("mgbase_presetSpawnMethod")
    presetSpawnLabel:SetTextColor(whiteColor)
    presetSpawnLabel:SetText(MWBLTL.Get("CuzMenu_Nom_Text4"))
    presetSpawnLabel:SizeToContents()

    local presetSpawnCombo = vgui.Create("DComboBox", presetSpawnPanel)
    presetSpawnCombo:SetSize(200, 10)
    presetSpawnCombo:SetValue(options[method])
    presetSpawnCombo:SetFont("mgbase_presetSpawnMethod")
    presetSpawnCombo:SetTextColor(whiteColor)
    presetSpawnCombo:Dock(RIGHT)
    presetSpawnCombo:DockMargin(0, 5, 20, 5)
    for i, op in pairs(options) do
        presetSpawnCombo:AddChoice(op)
    end

    function presetSpawnCombo:OnSelect(index, value)
        GetConVar("mgbase_presetspawnmethod"):SetInt(index - 1)
    end

    function presetSpawnCombo:Paint(w, h)
        surface.SetDrawColor(blackColor.r, blackColor.g, blackColor.b, blackColor.a)
        self:DrawFilledRect()

        surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, whiteColor.a * 0.25)
        self:DrawOutlinedRect()
    end

    function presetSpawnCombo:OnMenuOpened(menu)
        function menu:Paint(w, h)
            surface.SetDrawColor(blackColor.r, blackColor.g, blackColor.b, blackColor.a)
            self:DrawFilledRect()
        end

        for i = 1, #options + 1, 1 do
            local child = menu:GetChild(i)
            child:SetFont("mgbase_presetSpawnMethod_child")
            child:SetTextColor(whiteColor)
            child.HoverDelta = 0
            function child:Paint(w, h)
                if (self:IsHovered()) then
                    self.HoverDelta = math.Approach(self.HoverDelta, 1, 8 * FrameTime())
                else
                    self.HoverDelta = math.Approach(self.HoverDelta, 0, 8 * FrameTime())
                end

                surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, self.HoverDelta * 15)
                self:DrawFilledRect()
            end
        end
    end
end

local function openStatsInfo(panel, attachment, weapon)
    if (attachment.Breadcrumbs == nil) then --from attachments selection list
        weapon:MakeBreadcrumbsForAttachment(attachment)
    end
    
    local count = table.Count(attachment.Breadcrumbs)
    
    if (attachment.Breadcrumbs == nil || count <= 0) then
        return
    end

    if (!IsValid(panel.hover)) then
        panel.hover = vgui.Create("DPanel")
        panel.hover.parent = panel
        local x, y = panel:LocalToScreen(panel:GetPos())
        panel.hover:SetPos(x + panel:GetWide() + 20, y + 20)
        panel.hover:SetSize(400, 50 * math.max(count, 1))
        panel.hover:SetMouseInputEnabled(false)
        panel.hover:NoClipping(true)
        function panel.hover:Think()
            if (!IsValid(self.parent)) then
                self:Remove()
                return
            end
            
            self:SetPos(math.Clamp(gui.MouseX() + 30, 0, ScrW() - self:GetWide()), math.Clamp(gui.MouseY() + 30, 0, ScrH() - self:GetTall()))
        end
        function panel.hover:Paint(w, h)
            if (count <= 0) then
                return
            end
            
            surface.SetMaterial(blurMaterial)
            surface.SetDrawColor(255, 255, 255, 255)
            
            for i = 1, 10, 1 do
                render.UpdateScreenEffectTexture()
                self:DrawTexturedRect()
            end
            
            surface.SetDrawColor(blackColor.r, blackColor.g, blackColor.b, blackColor.a)
            self:DrawFilledRect()
            
            --white fade
            surface.SetMaterial(buttonGlowMaterial)
            surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, whiteColor.a * 0.02)
            surface.DrawTexturedRect(0, h * 0.5, w, h * 0.5)
        end
        function panel.hover:PaintOver(w, h)
            surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, whiteColor.a)

            --long line on the right
            surface.DrawLine(w + 5, 15, w + 5, h - 15)
            
            --corner lines
            surface.DrawLine(-5, -5, -5, 5)
            surface.DrawLine(-5, -5, 5, -5)
        end

        local statsGrid = vgui.Create("DGrid", panel.hover)
        statsGrid:SetPos(0, 0)
        statsGrid:SetCols(1)
        statsGrid:SetColWide(panel.hover:GetWide())
        statsGrid:SetRowHeight(50) 
        statsGrid:Dock(FILL)
        
        for i, crumb in pairs(attachment.Breadcrumbs) do
            local statPanel = vgui.Create("DPanel")
            statPanel:SetSize(statsGrid:GetColWide(), statsGrid:GetRowHeight())
            function statPanel:Paint(w, h)
                if (!validWeapon(weapon)) then
                    return
                end
                
                local statInfo = weapon.StatInfo[crumb.statInfo]
                draw.SimpleText(statInfo.Name, "mgbase_statName", 10, h * 0.5, whiteColor, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
                
                local bPositive = crumb.Current < crumb.Original
                
                if (statInfo.ProIfMore) then
                    bPositive = !bPositive
                end
                
                local statColor = bPositive && greenColor || redColor
                local statText = math.Round(crumb.Current - crumb.Original, 1)
                if (statInfo.ShowPercentage) then
                    statText = math.Round((crumb.Current - crumb.Original) / crumb.Original * 100, 1).."%"
                end
                
                statText = (bPositive && "⮝ " || "⮟ ")..statText
                
                draw.SimpleText(statText, "mgbase_stat", w - 10, h * 0.5, statColor, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER)
                
                local c = Color(statColor.r, statColor.g, statColor.b, statColor.a * 0.5)
                draw.SimpleText(statText, "mgbase_statPositive", w - 10, h * 0.5, c, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER)
            end
            
            statsGrid:AddItem(statPanel)
        end

        if attachment.CustomText then

            local panelWidth = statsGrid:GetColWide()
            local text = attachment.CustomText
            local splitText = string.Split(text, " ")

            local textPanel = vgui.Create("DPanel")
            textPanel:SetSize(statsGrid:GetColWide(), statsGrid:GetRowHeight())
            textPanel.WrappedText = {}

            local curString = ""
            local prevString = ""
            local count = 1

            for k, v in pairs(splitText) do 

                curString = curString..v.." "
                local w, h = surface.GetTextSize(curString)
                local w2, h2 = surface.GetTextSize(v)

                if w > panelWidth - 10 then 
                    textPanel.WrappedText[count] = prevString
                    curString = v.." "
                    count = count + 1
                    panel.hover:SetTall(panel.hover:GetTall() + 50)
                end 
                prevString = curString

            end
            textPanel.WrappedText[count] = curString

            function textPanel:Paint(w, h)

                if !self.WrappedText then return end

                for k, v in pairs(self.WrappedText) do
                    local old = DisableClipping( true )
                    draw.SimpleText(v, "mgbase_statName", 10 , (h * 0.5) + ((k - 1) * 50), attachment.CustomTextColor || whiteColor, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
                    DisableClipping( old )
                end
                        
                local bPositive = true
            end

            panel.hover:SetTall(panel.hover:GetTall() + 50)
            statsGrid:AddItem(textPanel)
        end

    end
end

local function closeStatsInfo(panel)
    if (!IsValid(panel.hover)) then
        return
    end
    
    panel.hover:Remove()
end

local categoryScrollBarValue = {}

local function openCategoryList(categoryName, weapon, buttonFrom)
    local background = makePopupMenu()
    local atts = {}
    
    for slot, attachmentClasses in pairs(weapon.Customization) do
        for i, attachmentClass in pairs(attachmentClasses) do
            if (MW_ATTS[attachmentClass] == nil) then
                continue
            end
            
            local att = table.Copy(weapon:GetStoredAttachment(attachmentClass))
            if (att.Category == categoryName && i > 1 && MW_ATTS[attachmentClass] != nil && weapon:GetAttachmentInUseForSlot(slot).ClassName != attachmentClass) then
                atts[#atts + 1] = att
            end
        end
    end
    
    if (#atts <= 0) then
        --failsafe somehow
        background:Remove()
        return
    end
    
    if (categoryScrollBarValue[categoryName] == nil) then
        categoryScrollBarValue[categoryName] = 0
    end

    local menu = vgui.Create("DPanel", background)
    menu:SetSize(400, ScrH() * 0.6)
    menu:Center()
    
    local x,y = menu:GetPos()
    menu:SetPos(x, ScrH())
    menu:MoveTo(x, y, 0.1, 0, -1)
    
    function menu:Paint(width, height)
    end
    
    local headerPanel = vgui.Create("DPanel", menu)
    headerPanel:SetText("")
    headerPanel:Dock(TOP)
    headerPanel:SetSize(0, 40)
    headerPanel:DockMargin(20, 0, 20, 10)
    function headerPanel:Paint(w, h)
        local text = string.upper(categoryName)
        local ltl = MWBLTL.Get("Atts_Category_"..string.gsub(categoryName, " ", "_"))
        draw.SimpleText(ltl or text, "mgbase_attSlotMenu", 0, h * 0.5, whiteColor, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
    end 
    
    makeCloseButton(headerPanel, background)

    local scroll = makeScrollBar(menu, RIGHT)
    local vbar = scroll:GetVBar()
    
    vbar:AnimateTo(categoryScrollBarValue[categoryName], 0.25, 0, -1)

    function vbar:OnRemove()
        categoryScrollBarValue[categoryName] = self:GetScroll()
    end

    local attachmentsGrid = vgui.Create("DGrid", scroll)
    attachmentsGrid:SetPos(0, 0)
    attachmentsGrid:SetCols(1)
    attachmentsGrid:SetColWide(menu:GetWide())
    attachmentsGrid:SetRowHeight(80)
    attachmentsGrid:Dock(FILL)
    attachmentsGrid:DockMargin(20, 0, 0, 0)
    
    table.sort(atts, function(a, b)
        local aFav = mw_utils.IsAssetFavorite(weapon:GetClass(), a.ClassName)
        local bFav = mw_utils.IsAssetFavorite(weapon:GetClass(), b.ClassName)

        if (!aFav && bFav) then
            return false
        elseif (aFav && !bFav) then
            return true
        elseif ((aFav && bFav) || (!aFav && !bFav)) then
            return a.Name < b.Name
        end
    end)

    for _, attachment in pairs(atts) do
        --panel to hold button
        local attachmentPanel = vgui.Create("DPanel")
        attachmentPanel:SetSize(attachmentsGrid:GetColWide() * 0.9, attachmentsGrid:GetRowHeight())
        function attachmentPanel:Paint(w, h)
        end
        
        local but = vgui.Create("DButton", attachmentPanel)
        but:SetText("")
        but:Dock(FILL)
        but:DockMargin(0, 5, 0, 5)
        but.HoverDelta = 0
        but.ClickDelta = 0
        but.bWasHovered = false
        but.IsAllowed = true
        but.FavoriteDelta = 0
        
        function but:Think()
            if (!validWeapon(weapon)) then
                return
            end
            
            self.IsAllowed = weapon:IsAttachmentAllowed(attachment)

            if (self:IsHovered()) then
                openStatsInfo(self, attachment, weapon)
                createButtonControls(self, nil, MWBLTL.Get("CuzMenu_Nom_Text1"), 200, self:GetTall())
            else
                closeStatsInfo(self)
                removeButtonControls(self)
            end
        end
        
        function but:Paint(width, height)
            if (self:IsHovered()) then
                self.HoverDelta = math.Approach(self.HoverDelta, 1, math.min(10 * RealFrameTime(), 0.1))
                
                if (!self.bWasHovered) then
                    surface.PlaySound(hoverAttachmentSounds[math.random(1, #hoverAttachmentSounds)])
                end
                self.bWasHovered = true
            else
                self.HoverDelta = math.Approach(self.HoverDelta, 0, math.min(10 * RealFrameTime(), 0.1))
                self.bWasHovered = false
            end
            
            if (self:IsDown()) then
                self.ClickDelta = math.Approach(self.ClickDelta, 1, math.min(10 * RealFrameTime(), 0.1))
            else
                self.ClickDelta = math.Approach(self.ClickDelta, 0, math.min(10 * RealFrameTime(), 0.1))
            end
            
            local currentColor = self.IsAllowed && blackColor || backgroundErrorColor
            
            --background
            surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a)
            self:DrawFilledRect()
            
            currentColor = self.IsAllowed && (attachment.UIColor || whiteColor) || errorColor
            
            --click hold
            surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a * 0.15 * self.ClickDelta)
            self:DrawFilledRect()
            
            --border hover
            surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a * self.HoverDelta)
            self:DrawOutlinedRect()
            
            --glow hover
            surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a * 0.15 * (self.HoverDelta - self.ClickDelta))
            surface.SetMaterial(buttonGlowMaterial)
            surface.DrawTexturedRect(0, height * 0.5, width, height * 0.5)

            --favorite bg
            self.FavoriteDelta = math.Approach(self.FavoriteDelta, 0, math.min(3 * RealFrameTime(), 0.3))

            if (self.FavoriteDelta > 0) then
                surface.SetDrawColor(yellowColor.r, yellowColor.g, yellowColor.b, yellowColor.a * self.FavoriteDelta)
                self:DrawFilledRect()
            end

            if (mw_utils.IsAssetFavorite(weapon:GetClass(), attachment.ClassName)) then
                surface.SetDrawColor(yellowColor.r, yellowColor.g, yellowColor.b, yellowColor.a)
                surface.SetMaterial(favoriteMaterial)
                surface.DrawTexturedRect(-1, -1, 16, 16)
            end
            
            --att icon
            surface.SetMaterial(attachment.Icon)
            surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, 255)
            surface.DrawTexturedRect(15, 11, 50, 50) --???
            
            --att name
            local bDeveloper = GetConVar("developer"):GetInt() > 0
            local blocker = weapon:GetBlockerAttachment(attachment)
            local bCanShowExtra = attachment.CosmeticChange || blocker != nil || bDeveloper
            local hoverColor = Color(currentColor.r, currentColor.g, currentColor.b, currentColor.a * self.HoverDelta)
            local yOffset = bCanShowExtra && self.HoverDelta * 10 || 0

            local name = bDeveloper && attachment.ClassName || attachment.Name
            draw.SimpleText(name, "mgbase_attSlotAttachmentInUse:hover", 75, height * 0.5 - yOffset, hoverColor, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
            draw.SimpleText(name, "mgbase_attSlotAttachmentInUse", 75, height * 0.5 - yOffset, currentColor, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, 2, shadowColor)
            
            if (bCanShowExtra) then
                if (bDeveloper) then
                    draw.SimpleText(attachment.Base, "mgbase_attSlotAttachmentInUse_IsCosmetic", 75, height * 0.5 + yOffset, hoverColor, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
                elseif (blocker != nil) then
                    draw.SimpleText(MWBLTL.Get("CuzMenu_Nom_Text5")..blocker.Name, "mgbase_attSlotAttachmentInUse_IsCosmetic", 75, height * 0.5 + yOffset, hoverColor, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
                else
                    draw.SimpleText(MWBLTL.Get("CuzMenu_Nom_Text6"), "mgbase_attSlotAttachmentInUse_IsCosmetic", 75, height * 0.5 + yOffset, hoverColor, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
                end
            end
        end
        
        function but:PaintOver(width, height)
        end
        
        function but:DoClick()
            if (!self.IsAllowed) then
                surface.PlaySound(selectAttachmentSound)
                return
            end
            
            local slotInUse = 1
            local indexInUse = 1
            
            for slot, atts in pairs(weapon.Customization) do
                for i, att in pairs(atts) do
                    if (att == attachment.ClassName) then
                        slotInUse = slot
                        indexInUse = i
                        break
                    end
                end
            end
            
            if (attachment.ClassName == weapon:GetAttachmentInUseForSlot(slotInUse).ClassName) then
                surface.PlaySound(detachSound)
            else
                surface.PlaySound(attachSound)
            end
            
            surface.PlaySound(selectAttachmentSound)
            mw_utils.SendAttachmentToServer(weapon, slotInUse, indexInUse)

            background:Remove()
        end

        function but:DoRightClick()
            if (!mw_utils.IsAssetFavorite(weapon:GetClass(), attachment.ClassName)) then
                mw_utils.FavoriteAsset(weapon:GetClass(), attachment.ClassName)
                surface.PlaySound(favoriteSound)
                self.FavoriteDelta = 1
            else
                mw_utils.UnfavoriteAsset(weapon:GetClass(), attachment.ClassName)
                surface.PlaySound(unfavoriteSound)
            end
        end
        
        attachmentsGrid:AddItem(attachmentPanel)
    end
end

local function createUtilityButton(parent, icon, text)
    local button = vgui.Create("DButton", parent)
    button:SetText("")
    button.HoverDelta = 0
    button.ClickDelta = 0
    button.bWasHovered = false
    function button:Paint(width, height)
        if (self:IsHovered()) then
            self.HoverDelta = math.Approach(self.HoverDelta, 1, math.min(10 * RealFrameTime(), 0.1))
            
            if (!self.bWasHovered) then
                surface.PlaySound(hoverAttachmentSounds[math.random(1, #hoverAttachmentSounds)])
            end
            
            self.bWasHovered = true
        else
            self.HoverDelta = math.Approach(self.HoverDelta, 0, math.min(10 * RealFrameTime(), 0.1))
            self.bWasHovered = false
        end
        
        if (self:IsDown()) then
            self.ClickDelta = math.Approach(self.ClickDelta, 1, math.min(10 * RealFrameTime(), 0.1))
        else
            self.ClickDelta = math.Approach(self.ClickDelta, 0, math.min(10 * RealFrameTime(), 0.1))
        end
        
        local currentColor = blackColor
        
        --background
        surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a)
        self:DrawFilledRect()
        
        currentColor = whiteColor
        
        --click hold
        surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a * 0.15 * self.ClickDelta)
        self:DrawFilledRect()
        
        --border hover
        surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, Lerp(self.HoverDelta, 45, 255))
        self:DrawOutlinedRect()
        
        --glow hover
        surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a * Lerp(self.ClickDelta, 0.1, 0))
        surface.SetMaterial(cursorGlowMaterial)
        local x, y = self:ScreenToLocal(gui.MouseX(), gui.MouseY())
        surface.DrawTexturedRect(x - 70, y - 70, 140, 140)
        
        --icon
        if (icon != nil) then
            surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, Lerp(self.HoverDelta, 150, 255))
            surface.SetMaterial(icon)
            surface.DrawTexturedRect(10, 10, width - 20, height - 20)
        end
        
        if (text != nil && text != "") then
            local hoverColor = Color(currentColor.r, currentColor.g, currentColor.b, currentColor.a * self.HoverDelta)
            draw.SimpleText(text, "mgbase_utilityButton:hover", width * 0.5, height * 0.5, hoverColor, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
            draw.SimpleText(text, "mgbase_utilityButton", width * 0.5, height * 0.5, currentColor, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, shadowColor)
        end
    end
    
    return button
end

local lastScroll = 0
local function openCustomizationMenu(weapon)
    if (IsValid(MW_CUSTOMIZEMENU)) then
        return
    end

    surface.PlaySound(openSound)
    gui.EnableScreenClicker(true)
    
    MW_CUSTOMIZEMENU = vgui.Create("DFrame") 
    MW_CUSTOMIZEMENU:SetPos(0, 0)
    MW_CUSTOMIZEMENU:SetSize(ScrW(), ScrH())
    MW_CUSTOMIZEMENU:ShowCloseButton(false) 
    MW_CUSTOMIZEMENU:SetDraggable(false)
    MW_CUSTOMIZEMENU:Center()
    MW_CUSTOMIZEMENU:SetTitle("")
    MW_CUSTOMIZEMENU.AlphaDelta = 0
    function MW_CUSTOMIZEMENU:Paint(width, height)
        if (!validWeapon(weapon)) then
            return
        end
        
        weapon:DrawStats(self)

        if (!LocalPlayer():KeyDown(IN_USE)) then
            self.AlphaDelta = Lerp(math.min(10 * RealFrameTime(), 1), self.AlphaDelta, 1)
        else
            self.AlphaDelta = Lerp(math.min(10 * RealFrameTime(), 1), self.AlphaDelta, 0)
        end
        
        --open blur thing
        local size = 1500 * self.AlphaDelta
        surface.SetMaterial(customizeMenuOpenMaterial)
        surface.SetDrawColor(0, 0, 0, math.sin(self.AlphaDelta * math.pi) * 200)
        surface.DrawTexturedRect(width * 0.5 - size * 0.5, height * 0.5 - size * 0.5, size, size)

        surface.SetMaterial(noMaterial)
        surface.SetDrawColor(255, 255, 255, math.sin(self.AlphaDelta * math.pi) * 3)
        surface.DrawTexturedRect(width * 0.5 - size * 0.5, height * 0.5 - size * 0.5, size, size)
        
        --title
        local animHeight = 20
        draw.SimpleTextOutlined(MWBLTL.Get("CuzMenu_Title_Text"), "mgbase_attTitle_blur", ScrW() * 0.5, ScrH() * 0.1 - animHeight + (1 - self.AlphaDelta) * animHeight, Color(255, 255, 255, self.AlphaDelta * math.abs(math.sin(CurTime() * 3) * 255)), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 2, Color(0, 0, 0, self.AlphaDelta * 20))
        draw.SimpleTextOutlined(MWBLTL.Get("CuzMenu_Title_Text"), "mgbase_attTitle", ScrW() * 0.5, ScrH() * 0.1 - animHeight + (1 - self.AlphaDelta) * animHeight, Color(255, 255, 255, self.AlphaDelta * 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 2, Color(0, 0, 0, self.AlphaDelta * 20))
        
        surface.SetDrawColor(255, 255, 255, self.AlphaDelta * 255)
        surface.DrawLine(ScrW() * 0.5 - self.AlphaDelta * 200, ScrH() * 0.1, ScrW() * 0.5 + self.AlphaDelta * 200, ScrH() * 0.1)
        
        --gun name
        draw.SimpleTextOutlined(weapon.PrintName, "mgbase_attWeaponName", ScrW() * 0.5, ScrH() * 0.1 + animHeight - (1 - self.AlphaDelta) * animHeight, Color(255, 255, 255, self.AlphaDelta * 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP, 2, Color(0, 0, 0, self.AlphaDelta * 20))
    end
    
    function MW_CUSTOMIZEMENU:Think()
        if (!validWeapon(weapon)) then
            closeCustomizationMenu()
            self:Remove() --why do i need this
        end
    end
    
    local categories = {}
    
    for slot, atts in pairs(weapon.Customization) do
        for ind, attClass in pairs(atts) do
            if (ind <= 1) then
                continue
            end

            if (MW_ATTS[attClass] == nil) then
                continue
            end
            
            local att = weapon:GetStoredAttachment(attClass)
            local category = att.Category

            if (category == nil) then
                continue
            end
            
            categories[category] = (categories[category] || 0) + (weapon:IsAttachmentAllowed(att) && 1 || 0)
        end
    end
    
    if (table.IsEmpty(categories)) then
        return
    end
    
    local leftSidePanel = vgui.Create("DPanel", MW_CUSTOMIZEMENU)
    leftSidePanel:SetPos(100, ScrH() * 0.2)
    leftSidePanel:SetSize(600, ScrH() * 0.7)
    function leftSidePanel:Paint(w, h)
    end
    function leftSidePanel:Think()
        local x, y = self:GetPos()
        
        if (!LocalPlayer():KeyDown(IN_USE)) then
            x = math.Approach(x, 100, 5000 * RealFrameTime())
        else
            x = math.Approach(x, -600, 5000 * RealFrameTime())
        end
        
        self:SetPos(x, y)
    end
    
    --holder for categories with scroll bar
    local categoriesMenu = vgui.Create("DScrollPanel", leftSidePanel)
    categoriesMenu:SetPos(0, 0)
    categoriesMenu:SetSize(leftSidePanel:GetWide(), leftSidePanel:GetTall() - 100)
    
    --categoriesMenu:MoveTo(100, ScrH() * 0.2, 0.3, 0, 10)
    function categoriesMenu:Paint(w, h)
        --    surface.SetDrawColor(255, 0, 0, 255)
        --    self:DrawFilledRect()
    end
    
    local sbar = categoriesMenu:GetVBar()
    sbar:Dock(LEFT)
    sbar.LastScrollValue = sbar:GetOffset()
    sbar:AnimateTo(lastScroll, 0.25, 0, -1)

    function sbar:Paint(w, h)
    end
    function sbar:Think()
        if (self:GetOffset() != self.LastScrollValue && math.abs(self:GetOffset() - self.LastScrollValue) > 10) then
            surface.PlaySound(scrollSound)
            self.LastScrollValue = self:GetOffset()
        end
    end
    function sbar.btnUp:Paint(w, h)
        surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, whiteColor.a)
        surface.DrawRect(w * 0.5 - 2, 0, 4, h)
    end
    function sbar.btnDown:Paint(w, h)
        surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, whiteColor.a)
        surface.DrawRect(w * 0.5 - 2, 0, 4, h)
    end
    function sbar.btnGrip:Paint(w, h)
        surface.SetDrawColor(whiteColor.r, whiteColor.g, whiteColor.b, whiteColor.a)
        surface.DrawRect(w * 0.5 - 1, 10, 2, h - 20)
    end
    function sbar:OnRemove()
        lastScroll = self:GetScroll()
    end
    --grid for categories
    local categoriesGrid = vgui.Create("DGrid", categoriesMenu)
    categoriesGrid:SetPos(0, 0)
    categoriesGrid:SetCols(1)
    categoriesGrid:SetColWide(categoriesMenu:GetWide())
    categoriesGrid:SetRowHeight(80)
    categoriesGrid:Dock(FILL)
    categoriesGrid:DockMargin(20, 0, 0, 0)
    
    for categoryName, count in SortedPairs(categories) do
        --panel to hold buitton + attachment in use panel
        local categoryPanel = vgui.Create("DPanel")
        categoryPanel:SetSize(categoriesGrid:GetColWide(), categoriesGrid:GetRowHeight())
        function categoryPanel:Paint(w, h)
            --    surface.SetDrawColor(255, 0, 0, 255)
            --    self:DrawFilledRect()
        end
        
        --button to select category
        local but = vgui.Create("DButton", categoryPanel)
        but:SetText("")
        but:SetSize(200, categoriesGrid:GetRowHeight())
        but:Dock(LEFT)
        but:DockMargin(0, 5, 0, 5)
        but.HoverDelta = 0
        but.ClickDelta = 0
        but.AttachmentDelta = 0
        but.bWasHovered = false
        but.AvailableAttachments = count
        
        function but:Think()
            if (!validWeapon(weapon)) then
                return
            end
            
            self.AvailableAttachments = 0
            
            for slot, atts in pairs(weapon.Customization) do
                for ind, attClass in pairs(atts) do
                    if (ind <= 1) then
                        continue
                    end

                    if (MW_ATTS[attClass] == nil) then
                        continue
                    end
                    
                    local att = weapon:GetStoredAttachment(attClass)
                    
                    if (att.Category != categoryName || att.ClassName == weapon:GetAttachmentInUseForSlot(slot).ClassName) then
                        continue
                    end
                    
                    self.AvailableAttachments = self.AvailableAttachments + (weapon:IsAttachmentAllowed(att) && 1 || 0)
                end
            end
        end
        
        function but:Paint(width, height)
            if (self:IsHovered()) then
                self.HoverDelta = math.Approach(self.HoverDelta, 1, math.min(10 * RealFrameTime(), 0.1))
                
                if (!self.bWasHovered) then
                    surface.PlaySound(hoverSounds[math.random(1, #hoverSounds)])
                end
                self.bWasHovered = true
            else
                self.HoverDelta = math.Approach(self.HoverDelta, 0, math.min(10 * RealFrameTime(), 0.1))
                self.bWasHovered = false
            end
            
            if (!self.inUsePanel:IsHovered()) then
                self.inUsePanel.HoverDelta = self.HoverDelta
            end
            
            if (self:IsDown()) then
                self.ClickDelta = math.Approach(self.ClickDelta, 1, math.min(10 * RealFrameTime(), 0.1))
            else
                self.ClickDelta = math.Approach(self.ClickDelta, 0, math.min(10 * RealFrameTime(), 0.1))
            end
            
            local currentColor = self.AvailableAttachments <= 0 && backgroundErrorColor || blackColor
            
            --background
            surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a)
            self:DrawFilledRect()
            
            currentColor = self.AvailableAttachments <= 0 && errorColor || whiteColor
            
            --click hold 
            surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a * 0.15 * self.ClickDelta)
            self:DrawFilledRect()
            
            --border hover
            surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a * self.HoverDelta)
            self:DrawOutlinedRect()
            
            --glow hover
            surface.SetDrawColor(currentColor.r, currentColor.g, currentColor.b, currentColor.a * 0.15 * (self.HoverDelta - self.ClickDelta))
            surface.SetMaterial(buttonGlowMaterial)
            surface.DrawTexturedRect(0, height * 0.75, width, height * 0.25)
            
            --category
            local text = categoryName
            local ltl = MWBLTL.Get("Atts_Category_"..string.gsub(categoryName, " ", "_"))
            draw.SimpleText(string.upper(ltl or text), "mgbase_attSlotMenu", width * 0.5, height * 0.5, currentColor, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
            
            local hoverColor = Color(currentColor.r, currentColor.g, currentColor.b, currentColor.a * self.HoverDelta)
            draw.SimpleText(string.upper(ltl or text), "mgbase_attSlotMenu:hover", width * 0.5, height * 0.5, hoverColor, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
        
        --panel to show what attachment we have in use (empty if default)
        local removeAttachmentButton = vgui.Create("DButton", categoryPanel)
        removeAttachmentButton:SetSize(400, categoriesGrid:GetRowHeight())
        removeAttachmentButton:SetText("")
        removeAttachmentButton:Dock(LEFT)
        removeAttachmentButton:DockMargin(10, 0, 0, 0)
        removeAttachmentButton.HoverDelta = 0
        removeAttachmentButton.HoverDelta2 = 0 --alternative so att button doesn't higlight other elements
        removeAttachmentButton.ClickDelta = 0
        removeAttachmentButton.FavoriteDelta = 0
        removeAttachmentButton.AttachmentDelta = 0
        removeAttachmentButton.LastAttachmentIndex = 1
        removeAttachmentButton.bShow = true--this is for latency (hide before actual customization update)
        
        --link to button so we have consistent hover delta anim
        but.inUsePanel = removeAttachmentButton
        
        function removeAttachmentButton:DoRightClick()
            local attachmentInUse = nil
            
            for slot, att in pairs(weapon:GetAllAttachmentsInUse()) do
                if (att.Category == categoryName) then
                    attachmentInUse = att
                    break
                end
            end
            
            if (attachmentInUse == nil) then
                return
            end

            if (!mw_utils.IsAssetFavorite(weapon:GetClass(), attachmentInUse.ClassName)) then
                mw_utils.FavoriteAsset(weapon:GetClass(), attachmentInUse.ClassName)
                surface.PlaySound(favoriteSound)
                self.FavoriteDelta = 1
            else
                mw_utils.UnfavoriteAsset(weapon:GetClass(), attachmentInUse.ClassName)
                surface.PlaySound(unfavoriteSound)
            end
        end

        function removeAttachmentButton:TestHover(x, y)
            local x, y = self:ScreenToLocal(x, y) -- Convert to local coordinates
            return x >= 0 && y >= 0 && x <= self:GetWide() * 0.65 && y <= self:GetTall() 
        end
        
        function removeAttachmentButton:Think()
            if (!validWeapon(weapon)) then
                return
            end
            
            self:SetEnabled(false)
            self:SetCursor("none")
            
            --attachment in use next to button
            local attachmentInUse = nil
            
            for slot, att in pairs(weapon:GetAllAttachmentsInUse()) do
                if (att.Category == categoryName) then
                    attachmentInUse = att
                    break
                end
            end
            
            if (attachmentInUse == nil) then
                return
            end
            
            if (attachmentInUse.Index > 1 && self.bShow) then
                self:SetEnabled(true)
                self:SetCursor("hand")
            end
            
            if (self.LastAttachmentIndex != attachmentInUse.Index) then
                self.AttachmentDelta = 0
                self.ClickDelta = 0
                self.HoverDelta = 0
                self.HoverDelta2 = 0
                self.LastAttachmentIndex = attachmentInUse.Index
                self.bShow = true
            end

            if (self:IsHovered()) then
                openStatsInfo(self, attachmentInUse, weapon)
            else
                closeStatsInfo(self)
            end
        end
        
        function removeAttachmentButton:Paint(width, height)
            if (!self.bShow) then
                closeStatsInfo(self)
                return
            end
            
            if (!validWeapon(weapon)) then
                return
            end
            
            local attachmentInUse = nil
            
            for slot, att in pairs(weapon:GetAllAttachmentsInUse()) do
                if (att.Category == categoryName && att.Index > 1) then
                    attachmentInUse = att
                    break
                end
            end
            
            if (attachmentInUse == nil) then
                closeStatsInfo(self)
                return
            end
            
            if (self:IsHovered()) then
                self.HoverDelta = math.Approach(self.HoverDelta, 1, math.min(10 * RealFrameTime(), 0.1))
                self.HoverDelta2 = math.Approach(self.HoverDelta2, 1, math.min(10 * RealFrameTime(), 0.1))
                
                if (!self.bWasHovered) then
                    surface.PlaySound(hoverAttachmentSounds[math.random(1, #hoverAttachmentSounds)])
                end
                
                self.bWasHovered = true
            else
                self.HoverDelta = math.Approach(self.HoverDelta, 0, math.min(10 * RealFrameTime(), 0.1))
                self.HoverDelta2 = math.Approach(self.HoverDelta2, 0, math.min(10 * RealFrameTime(), 0.1))
                self.bWasHovered = false
            end
            
            if (self:IsDown() && !input.IsMouseDown(MOUSE_RIGHT)) then
                self.ClickDelta = math.Approach(self.ClickDelta, 1, math.min(10 * RealFrameTime(), 0.1))
            else
                self.ClickDelta = math.Approach(self.ClickDelta, 0, math.min(10 * RealFrameTime(), 0.1))
            end
            
            self.AttachmentDelta = math.Approach(self.AttachmentDelta, 1, math.min(10 * RealFrameTime(), 0.1))
            
            surface.SetAlphaMultiplier(self.AttachmentDelta)
            
            local rightOffset = (1 - self.AttachmentDelta) * 20
            
            --background stylish black blur
            surface.SetMaterial(removeButtonGlowMaterial)
            local color = LerpVector(self.ClickDelta, Vector(0, 0, 0), Vector(100, 0, 0))
            local alpha = Lerp(self.ClickDelta, Lerp(self.HoverDelta, 150, 250), 250)
            surface.SetDrawColor(color.x, color.y, color.z, alpha)
            surface.DrawTexturedRect(100 + rightOffset - self:GetWide() * 0.5, height * 0.5 - 30, self:GetWide(), 60)
            
            rightOffset = rightOffset + 10
            
            --fav
            self.FavoriteDelta = math.Approach(self.FavoriteDelta, 0, math.min(3 * RealFrameTime(), 0.3))
            
            if (self.FavoriteDelta > 0) then
                surface.SetDrawColor(yellowColor.r, yellowColor.g, yellowColor.b, yellowColor.a * self.FavoriteDelta)
                surface.SetMaterial(removeButtonGlowMaterial)
                surface.DrawTexturedRect(100 + rightOffset - self:GetWide() * 0.5, height * 0.5 - 30, self:GetWide(), 60)
            end

            --att icon
            surface.SetDrawColor(255, 255, 255, Lerp(self.HoverDelta - self.ClickDelta, 100, 255))
            surface.SetMaterial(attachmentInUse.Icon)
            surface.DrawTexturedRect(rightOffset, height * 0.5 - 20, 40, 40)
            
            surface.SetDrawColor(200, 0, 0, (self.HoverDelta2 * 0.5 + self.ClickDelta - self.FavoriteDelta) * 255)
            surface.SetMaterial(removeAttachmentMaterial)
            surface.DrawTexturedRect(rightOffset + 6, height * 0.5 - 16, 28, 32)
            
            rightOffset = rightOffset + 50
            
            --att name
            
            local colorForAttachment = attachmentInUse.UIColor || whiteColor
            draw.SimpleText(attachmentInUse.Name, "mgbase_attSlotAttachmentInUse:hover", rightOffset, height * 0.5, Color(colorForAttachment.r, colorForAttachment.g, colorForAttachment.b, colorForAttachment.a * self.HoverDelta), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
            draw.SimpleText(attachmentInUse.Name, "mgbase_attSlotAttachmentInUse", rightOffset, height * 0.5, colorForAttachment, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, 2, shadowColor)
            
            if (mw_utils.IsAssetFavorite(weapon:GetClass(), attachmentInUse.ClassName)) then
                surface.SetDrawColor(yellowColor.r, yellowColor.g, yellowColor.b, yellowColor.a)
                surface.SetMaterial(favoriteMaterial)
                surface.DrawTexturedRect(-1, 10, 16, 16)
            end

            surface.SetAlphaMultiplier(1)
        end
        
        function removeAttachmentButton:DoClick()
            local attachmentInUse = nil
            
            for slot, att in pairs(weapon:GetAllAttachmentsInUse()) do
                if (att.Category == categoryName && att.Index > 1) then
                    attachmentInUse = att
                    break
                end
            end
            
            if (attachmentInUse == nil) then
                return
            end
            
            surface.PlaySound(detachSound)
            surface.PlaySound(selectAttachmentSound)
            
            mw_utils.SendAttachmentToServer(weapon, attachmentInUse.Slot, attachmentInUse.Index)
            
            self.bShow = false
        end
        
        function but:DoClick()
            if (self.AvailableAttachments > 0) then
                surface.PlaySound(selectCategorySound)
                openCategoryList(categoryName, weapon, removeAttachmentButton)
                self.inUsePanel.LastAttachmentIndex = 1
            else
                surface.PlaySound(closeAttachmentsSound)
            end
        end
        
        categoriesGrid:AddItem(categoryPanel)
    end
    
    local bottomPanel = vgui.Create("DPanel", MW_CUSTOMIZEMENU)
    bottomPanel:SetSize(ScrW(), ScrH() * 0.2)
    bottomPanel:SetPos(0, ScrH() * 0.9 - 50)
    function bottomPanel:Paint()
    end
    function bottomPanel:Think()
        local x, y = self:GetPos()
        
        if (!LocalPlayer():KeyDown(IN_USE)) then
            y = math.Approach(y, ScrH() * 0.9 - 50, 5000 * RealFrameTime())
        else
            y = math.Approach(y, ScrH() * 0.9 + 450, 5000 * RealFrameTime())
        end
        
        self:SetPos(x, y)
    end
    
    -- exit button
    --[[local exitButton = createUtilityButton(bottomPanel, nil, MWBLTL.Get("CuzMenu_Nom_Text7"))
    exitButton:SetSize(150, 74)
    exitButton:SetPos(ScrW() * 0.5 - 75, 0)
    function exitButton:DoClick()
        closeCustomizationMenu()
    end]]
    
    --PRESETS BUTTON
    local presetsButton = createUtilityButton(bottomPanel, presetsMaterial, nil)
    presetsButton:SetSize(66, 66)
    presetsButton:SetPos(ScrW() * 0.5 - 100 - 33, 4)
    function presetsButton:DoClick()
        surface.PlaySound(selectCategorySound)
        openPresetsMenu(weapon)
    end

    --RESET BUTTON
    local resetButton = createUtilityButton(bottomPanel, resetMaterial, nil)
    resetButton:SetSize(66, 66)
    resetButton:SetPos(ScrW() * 0.5 - 33, 4)
    function resetButton:DoClick()
        surface.PlaySound(resetSound)

        for slot, att in pairs(weapon:GetAllAttachmentsInUse()) do
            if (att.Index > 1) then --just sending what we need
                mw_utils.SendAttachmentToServer(weapon, slot, 1)
            end
        end
    end

    --RANDOM BUTTON
    local randomButton = createUtilityButton(bottomPanel, randomMaterial, nil)
    randomButton:SetSize(66, 66)
    randomButton:SetPos(ScrW() * 0.5 + 100 - 33, 4)
    function randomButton:DoClick()
        surface.PlaySound(randomSound)

        for slot, atts in pairs(weapon.Customization) do
            mw_utils.SendAttachmentToServer(weapon, slot, math.random(#atts))
        end
    end
end

function SWEP:CustomizationMenu()
    if (self:HasFlag("Customizing")) then
        openCustomizationMenu(self)
    else
        closeCustomizationMenu(self)
    end
end

function SWEP:GetStatColor(originalStat, currentStat, statInfoIndex)
    local statInfo = self.StatInfo[statInfoIndex]
    
    if (statInfo != nil) then
        if (originalStat != currentStat) then
            local isPro = currentStat > originalStat
            
            if (!statInfo.ProIfMore) then
                isPro = currentStat < originalStat
            end
            
            return isPro && Color(0, 200, 0, 255) || Color(200, 0, 0, 255)
        end
    end
    
    return Color(255, 255, 255, 255)
end

local maxStats = 10

function SWEP:DrawStat(statInfoIndex, append, index, originalStat, currentStat)
    local scale = ScrH() / 1080
    local spacing = (index * 30) - (maxStats * 0.5 * 30) * scale
    local x,y = ScrW() * 0.05, ScrH() * 0.5

    --surface.SetMaterial(blurMaterial)
    --surface.SetDrawColor(255, 255, 255, 255)
        
    --render.UpdateScreenEffectTexture()
    --surface.DrawTexturedRect(x - 23 * scale, y + spacing - 14, 350, 28)
        
    surface.SetDrawColor(blackColor.r, blackColor.g, blackColor.b, blackColor.a)
    surface.DrawRect(x - 23 * scale, y + spacing - 14, MWBLTLXCoordinate.Line[GetConVar("gmod_language"):GetString()], 28)

    local info = self.StatInfo[statInfoIndex].Name
    local ltl = MWBLTL.Get("StatInfo_"..statInfoIndex)
    draw.SimpleTextOutlined(ltl..":" or info..":", "mgbase_firemode", x - 20 * scale, y + spacing, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, 2, Color(0, 0, 0, 20))
    draw.SimpleTextOutlined(math.Round(currentStat, 2)..append, "mgbase_firemode", x + MWBLTLXCoordinate.Text[GetConVar("gmod_language"):GetString()] * scale, y + spacing, self:GetStatColor(originalStat, currentStat, statInfoIndex), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, 2, Color(0, 0, 0, 20))
    
    return x - 20 * scale, y + spacing
end

function SWEP:DrawStats(panel)
    if (!self:GetOwner():KeyDown(IN_USE)) then
        return
    end
    
    surface.SetAlphaMultiplier(1 - panel.AlphaDelta)

    local scale = ScrH() / 1080
    local spacing = 30 * scale
    local x,y = ScrW() * 0.05, ScrH() * 0.5
    
    draw.SimpleTextOutlined(MWBLTL.Get("CuzMenu_Nom_Text8"), "mgbase_firemode", x + 150 * scale, y - (maxStats * 0.5 * 35) * scale, Color(255, 255, 255, 200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, Color(0, 0, 0, 20))
    
    local original = weapons.Get(self:GetClass()) --inheritance gotta copy
    local c = 1
    self:DrawStat("DamageClose", "", c, original.Bullet.Damage[1], self.Bullet.Damage[1])
    c = c + 1
    
    self:DrawStat("DamageRange", "", c, original.Bullet.Damage[2], self.Bullet.Damage[2])
    c = c + 1
    
    self:DrawStat("RPM", "", c, original.Primary.RPM, self.Primary.RPM)
    c = c + 1
    
    self:DrawStat("EffectiveRange", MWBLTL.Get("CuzMenu_Nom_Text9"), c, original.Bullet.EffectiveRange, self.Bullet.EffectiveRange)
    c = c + 1
    
    if (self.Projectile == nil) then
        self:DrawStat("MaxRange", MWBLTL.Get("CuzMenu_Nom_Text9"), c, original.Bullet.Range, self.Bullet.Range)
        c = c + 1
    end
    
    local statBeforeLineX, statBeforeLineY = self:DrawStat("Accuracy", "", c, original.Cone.Hip * 100, self.Cone.Hip * 100)
    c = c + 1
    
    surface.SetDrawColor(255, 255, 255, 200)
    surface.DrawLine(statBeforeLineX, statBeforeLineY + spacing, statBeforeLineX + MWBLTLXCoordinate.Line[GetConVar("gmod_language"):GetString()] - 5, statBeforeLineY + spacing)
    c = c + 1
    
    self:DrawStat("AimLength", MWBLTL.Get("CuzMenu_Nom_Text10"), c, original.Animations.Ads_In.Length / (original.Animations.Ads_In.Fps / 30), self:GetAnimLength("Ads_In"))
    c = c + 1
    
    local reloadAnimIndex = self:ChooseReloadAnim()

    if (reloadAnimIndex != nil) then
        self:DrawStat("ReloadLength", MWBLTL.Get("CuzMenu_Nom_Text10"), c, original.Animations[reloadAnimIndex].Length / (original.Animations[reloadAnimIndex].Fps / 30), self:GetAnimLength(reloadAnimIndex))
        c = c + 1
    end
    
    self:DrawStat("DrawLength", MWBLTL.Get("CuzMenu_Nom_Text10"), c, original.Animations.Draw.Length / (original.Animations.Draw.Fps / 30), self:GetAnimLength("Draw"))
    c = c + 1
    
    self:DrawStat("HolsterLength", MWBLTL.Get("CuzMenu_Nom_Text10"), c, original.Animations.Holster.Length / (original.Animations.Holster.Fps / 30), self:GetAnimLength("Holster"))
    c = c + 1
    
    statBeforeLineX, statBeforeLineY = self:DrawStat("SprintLength", MWBLTL.Get("CuzMenu_Nom_Text10"), c, original.Animations.Sprint_Out.Length / (original.Animations.Sprint_Out.Fps / 30), self:GetAnimLength("Sprint_Out"))
    c = c + 1

    surface.SetAlphaMultiplier(1)
end