ViolLua.Menu = { 
    Resolution = ViolLua.Resolution,
    Colors     = ViolLua.Colors,
    Config     = ViolLua.Config,
    Items      = ViolLua.Items,

    Gradients = {
        Up     = Material( 'vgui/gradient_up' ),
        Down   = Material( 'vgui/gradient_down' ),
        Left   = Material( 'vgui/gradient-l' ),
        Right  = Material( 'vgui/gradient-r' ),
        Uni    = Material( 'vgui/gradient-u' ),
        Center = Material( 'gui/center_gradient' ),
        Grid   = Material( 'gui/alpha_grid.png', 'nocull' )
    },

    Plain = Material( 'vgui/white' ),

    Color  = ViolLua.Colors[ 'Main' ],
    Copied = ViolLua.Colors[ 'Main' ],

    Background = NULL,
    Bottom     = NULL,
    Active     = NULL,
    Last       = NULL,
    
    Tabs    = { },
    Toggles = { },
    Held    = { },
    Dots    = { },

    Held = false
}

ViolLua:LoadFile( 'lua/ViolLua/modules/menu/constellation.lua' )

ViolLua:LoadFile( 'lua/ViolLua/modules/menu/elements/tabs.lua' )
ViolLua:LoadFile( 'lua/ViolLua/modules/menu/elements/list.lua' )
ViolLua:LoadFile( 'lua/ViolLua/modules/menu/elements/label.lua' )
ViolLua:LoadFile( 'lua/ViolLua/modules/menu/elements/input.lua' )
ViolLua:LoadFile( 'lua/ViolLua/modules/menu/elements/slider.lua' )
ViolLua:LoadFile( 'lua/ViolLua/modules/menu/elements/button.lua' )
ViolLua:LoadFile( 'lua/ViolLua/modules/menu/elements/binder.lua' )
ViolLua:LoadFile( 'lua/ViolLua/modules/menu/elements/checkbox.lua' )
ViolLua:LoadFile( 'lua/ViolLua/modules/menu/elements/dropdown.lua' )
ViolLua:LoadFile( 'lua/ViolLua/modules/menu/elements/draggable.lua' )
ViolLua:LoadFile( 'lua/ViolLua/modules/menu/elements/colorpicker.lua' )
ViolLua:LoadFile( 'lua/ViolLua/modules/menu/elements/minicheckbox.lua' )

function ViolLua.Menu:Scale( Size )
    return math.max( Size * ( self.Resolution.Height / 1080 ), 1 )
end

function ViolLua.Menu:Init( Tabs )
    if ( not istable( Tabs ) ) then 
        return ViolLua:Print( true, 'Invalid tab layout passed to menu initializer "%s"!', type( Tabs ) )
    end

    local Offset = self:Scale( 50 )

    -- Generate the constellation.
    self:SetupConstellation( )

    -- Generate the background.
    local Background = vgui.Create( 'DPanel' )
    Background:SetSize( self.Resolution.Width, self.Resolution.Height )
    Background:SetMouseInputEnabled( true )
    Background:SetKeyboardInputEnabled( true )
    Background.Paint = function( self, W, H )
        surface.SetDrawColor( 0, 0, 0, 180 )
        surface.DrawRect( 0, 0, W, H )

        ViolLua.Menu:RenderConstellation( )
    end

    self.Background = Background

    -- Generate the bottom tab selector.
    local Bottom = vgui.Create( 'DFrame', Background )
	Bottom:SetSize( self.Resolution.Width, Offset )
	Bottom:SetPos( 0, self.Resolution.Height - ( Offset / 2 ) )
	Bottom:SetTitle( '' )
	Bottom:SetDraggable( false )
    Bottom:ShowCloseButton( false )
    Bottom:MakePopup( )

    Bottom.Paint = function( self, W, H )
        surface.SetMaterial( ViolLua.Menu.Gradients.Left )
        surface.SetDrawColor( 17, 17, 17 )
        surface.DrawTexturedRect( 0, 0, W, H )

        surface.SetMaterial( ViolLua.Menu.Gradients.Right )
        surface.SetDrawColor( 17, 17, 17 )
        surface.DrawTexturedRect( 0, 0, W, H )

        surface.SetMaterial( ViolLua.Menu.Gradients.Center )
        surface.SetDrawColor( ViolLua.Menu.Color )
        surface.DrawTexturedRect( 0, 0, W, 2 )
    end

    self.Bottom = Bottom
    
    -- Generate our tabs.
    for i = 0, #Tabs - 1 do 
        local Index = Tabs[ i + 1 ]

        self:GenerateTab( Index.Title, 750, Index.Height, Index.Single )

        local TabButton = vgui.Create( 'DButton', Bottom )
        TabButton:SetText( Index.Title )
        TabButton:SetTextColor( self.Colors[ 'White' ] )
        TabButton:SetFont( 'DefaultSmall' )
        TabButton:SizeToContents( )
        TabButton:Dock( LEFT )
        TabButton:DockMargin( 0, -self:Scale( 50 ), self:Scale( 5 ), 0 )
        
        TabButton.Paint = function( self, W, H ) 
            if ( ViolLua.Config[ 'miscellaneous_menu_labels' ] ) then 
                self:SetColor( ViolLua.Menu.Color )
            else
                self:SetColor( ViolLua.Colors[ 'White' ] )
            end
        end

        TabButton.DoClick = function( self, W, H )
            local Frame = ViolLua.Menu.Tabs[ Index.Title ].Parent

            Frame:MakePopup( )

            local Speed = ViolLua.Config[ 'miscellaneous_menu_fade' ]

            if ( Frame:IsVisible( ) ) then 
                Frame:AlphaTo( 0, Speed, 0, function( Data, Panel ) 
                    Panel:ToggleVisible( )
                end )
            else
                Frame:ToggleVisible( )
                Frame:SetAlpha( 0 )
                Frame:AlphaTo( 255, Speed, 0 )
            end
        end
    end

    -- Generate the timestamp.
    local Timestamp = vgui.Create( 'DLabel', Bottom )
    Timestamp:Dock( RIGHT )
    Timestamp:DockMargin( 0, -self:Scale( 50 ), -self:Scale( 15 ), 0 )
    Timestamp:SetText( '' )
    Timestamp:SetMouseInputEnabled( true )
    Timestamp:SetCursor( 'hand' )

    Timestamp.DoClick = function( self )
        ViolLua.Menu:Input( true )
    end

    Timestamp.Paint = function( self, W, H ) 
        if ( ViolLua.Config[ 'miscellaneous_menu_labels' ] ) then 
            self:SetColor( ViolLua.Menu.Color )
        else
            self:SetColor( ViolLua.Colors[ 'White' ] )
        end
    end
    
    Timestamp.Think = function( self )
        self:SetText( os.date( '%X' ) )
    end
end

function ViolLua.Menu:Handle( Title, Callback, Side )
    local Tab = self.Tabs[ Title ]

    if ( not Tab ) then 
        return
    end

    Callback( self, Side and Tab.RightS or Tab.LeftS )
end

function ViolLua.Menu:Input( Override )
    if ( Override or input.IsKeyDown( KEY_INSERT ) ) then 
        if ( not self.Toggle ) then 
            self.Background:ToggleVisible( )

            for k, Tab in pairs( self.Tabs ) do 
                Tab.Parent:SetVisible( false )
            end
        end
        
        self.Toggle = true
    else 
        self.Toggle = false
    end
end

ViolLua.Menu:Init( {
    { Title = 'Aimbot', Height = 500, Single = false },
    { Title = 'Anti-Aim', Height = 500, Single = false },
    { Title = 'Players', Height = 500, Single = false },
    { Title = 'World', Height = 500, Single = false },
    { Title = 'Items', Height = 500, Single = false },
    { Title = 'Miscellaneous', Height = 500, Single = false },
} )

ViolLua.Hooks:New( 'Think', ViolLua.Menu.Input, ViolLua.Menu )