local Inventory = nil
local InventoryOpen = nil

local function GetItemAmount( item )
	return LocalPlayer().Inventory[ item ];
end;

local PANEL = {};

function PANEL:Init()
	self.Label = vgui.Create( "DLabel", self );
	self.Label:SetTextColor( Color( 255, 255, 255, 255 ) );
	self.Label:SizeToContents();
	
	self.UpdateLabelText = "";
	self.DrainW = 0;
end;

function PANEL:SetDrainW( num )
	self.DrainW = num;
end;

function PANEL:SetLabelText( text )
	self.Label:SetText( text );
	self.UpdateLabelText = text;
end;

function PANEL:PerformLayout()
	self.Label:SetPos( ( self:GetWide() / 2 ) - ( self.Label:GetWide() / 2 ), ( self:GetTall() / 2 ) - ( self.Label:GetTall() / 2 ) );
	self.Label:SetText( self.UpdateLabelText );
	self.Label:SizeToContents();
end;

function PANEL:Paint()
	draw.DrawDoubleOutlined( 0, 0, self:GetWide(), self:GetTall(), Color( 0, 0, 0, 255 ) );
	
	surface.SetDrawColor( 45, 152, 186, 150 );
	surface.DrawRect( 2, 2, self:GetWide() - 4, self:GetTall() - 4 );
	
	surface.SetDrawColor( 43, 152, 191, 255 );
	surface.DrawRect( 2, 2, math.Clamp( ( self:GetWide() - 4 ) * self.DrainW, 0, ( self:GetWide() - 4 ) ), self:GetTall() - 4 );
end;
vgui.Register( "eps_drainbar", PANEL, "DPanel" );

local PANEL = {};

function PANEL:Init()
	self.Item = {};
	self.Item.Selected = nil;
	self.Categories = {};
	
	self:SetSize( 470, 565 );
	self:SetPos( ScrW() / 2 - 235, ScrH() / 2 - 282.5 );
	
	for k, v in pairs( epsilonScript.Items.Stored ) do
		local category = epsilonScript.Items.GetItem( k ).Category;
		
		if ( !table.HasValue( self.Categories, category ) ) then
			self.Categories[ #self.Categories + 1 ] = category
		end;
	end;
	
	self.Inventory = vgui.Create( "DPanelList" );
	self.Inventory:SetPadding( 2 )
	self.Inventory:SetSpacing( 2 )
	
	self.Business = vgui.Create( "DPanelList" )
	self.Business:SetPadding( 3 );
	self.Business:SetSpacing( 2 );

	for k, v in pairs( self.Categories ) do
		self.BusinessCollapsibleCategory = vgui.Create( "DCollapsibleCategory" );
		self.BusinessCollapsibleCategory:SetLabel( v );
		self.BusinessCollapsibleCategory:SetPadding( 4 );
		
		self.BusinessCollapsibleCategoryList = vgui.Create( "DPanelList" )
		self.BusinessCollapsibleCategoryList:SetAutoSize( true )
		self.BusinessCollapsibleCategoryList:EnableVerticalScrollbar( true )
		self.BusinessCollapsibleCategoryList:SetPadding( 4 )
		self.BusinessCollapsibleCategoryList:SetSpacing( 4 )
		self.BusinessCollapsibleCategory:SetContents( self.BusinessCollapsibleCategoryList );
		
		self.Business:AddItem( self.BusinessCollapsibleCategory );
		
		for k2, v2 in pairs( epsilonScript.Items.Stored ) do
			if ( v2.Category == v ) then
				local DPanel = vgui.Create( "DPanel" )
				DPanel:SetSize( 0, 36 )
				
				local SpawnIcon = vgui.Create( "SpawnIcon", DPanel )
				SpawnIcon:SetIconSize( 32 )
				SpawnIcon:SetPos ( 2, 2 )
				SpawnIcon:SetModel( v2.Model )
				SpawnIcon:SetToolTip( v2.Description )
				
				local NameLabel = SimpleLabel( DPanel, v2.Name, Colour_White, "Default", 38, 3 )
				local CostLabel = SimpleLabel( DPanel, v2.Cost .. epsilonScript.Config:Get( "Currency" ), Colour_White, "Default", 38, 18 )
				local BuyButton = SimpleButton( DPanel, 375, 10, 60, 20, "Purchase", function() 
					LocalPlayer():ConCommand( "eps_buyitem " .. k2 )
					
					InventoryOpen = !InventoryOpen;
					gui.EnableScreenClicker( InventoryOpen );
					self:SetVisible( InventoryOpen );
				end );
				
				self.BusinessCollapsibleCategoryList:AddItem( DPanel )
			end
		end
	end;
	
	self.Bank = vgui.Create( "DPanelList" )
	
	local WIPLabel = SimpleLabel( self.Bank, "WIP - Not done yet!", Colour_White, "MenuFont2", 140, 150 )
	
	self.Crafting = vgui.Create( "DPanel" )
	self.Crafting.Paint = function() draw.DrawSimpleRect( 0, 0, self.Crafting:GetWide(), self.Crafting:GetTall(), Color( 60, 60, 60, 255 ) ) end;
	self:BuildCraftTab()
	
	self:AddSheet( "Inventory", self.Inventory, "gui/silkicons/box", false, false, "Inventory with your items!" );
	self:AddSheet( "Business", self.Business, "gui/silkicons/brick_add", false, false, "Buy items!" );
	self:AddSheet( "Bank", self.Bank, "gui/silkicons/star", false, false, "Get loans!" );
	self:AddSheet( "Crafting", self.Crafting, "gui/silkicons/picture_edit", false, false, "Crafting items!" );
end;

function PANEL:RebuildInventory()
	self.Inventory:Clear()
	
	self.WeightAmount = vgui.Create( "eps_drainbar" );
	self.WeightAmount:SetSize( 0, 25 );
	self.WeightAmount:SetLabelText( math.Round( LocalPlayer().CurrentInventoryWeight * 10 ) / 10 .. " / " .. LocalPlayer().MaxInventoryWeight .. " kg" );
	self.WeightAmount:SetDrainW( LocalPlayer().CurrentInventoryWeight / LocalPlayer().MaxInventoryWeight );
	self.Inventory:AddItem( self.WeightAmount );
	
	local categories = {};
	local items = {};
	local k, v;
	
	for k, v in pairs( LocalPlayer().Inventory ) do
		local itemTable = epsilonScript.Items.GetItem( k );
		
		if ( itemTable ) then
			local category = itemTable.Category;
			
			items[ category ] = items[ category ] or {};
			items[ category ][ #items[ category ] + 1 ] = k;
		end;
	end;
	
	for k, v in pairs( items ) do
		categories[ #categories + 1 ] = { category = k, items = v };
	end;
	
	table.sort( categories, function( a, b )
		return a.category < b.category;
	end );
	
	if ( table.Count( categories ) > 0 ) then
		for k, v in pairs( categories ) do
			local collapsibleCategory = vgui.Create( "DCollapsibleCategory", self );
			local panelList = vgui.Create( "DPanelList", self );
			
			table.sort( v.items, function( a, b )
				return epsilonScript.Items.Stored[ a ].Name > epsilonScript.Items.Stored[ b ].Name;
			end );
			
			for k2, v2 in pairs( v.items ) do
				local itemTable = epsilonScript.Items.GetItem( v2 );
				
				local DPanel = vgui.Create( "DPanel" );
				DPanel:SetSize( 0, 36 );
				
				local SpawnIcon = vgui.Create( "SpawnIcon", DPanel );
				SpawnIcon:SetIconSize( 32 );
				SpawnIcon:SetPos ( 2, 2 );
				SpawnIcon:SetModel( itemTable.Model );
				SpawnIcon:SetToolTip( itemTable.Description );
				
				local NameLabel = SimpleLabel( DPanel, itemTable.Name, Colour_White, "Default", 38, 3 );
				local WeightLabel = SimpleLabel( DPanel, "(x" .. GetItemAmount( v2 ) .. ") " .. ( itemTable.Weight * GetItemAmount( v2 ) ) .. " kg", Colour_White, "Default", 38, 18 );
				
				local DropButton = SimpleButton( DPanel, 375, 10, 60, 20, "Drop", function()
					LocalPlayer():ConCommand( "eps_dropitem " .. v2 )
					InventoryOpen = !InventoryOpen;
					gui.EnableScreenClicker( InventoryOpen );
					self:SetVisible( InventoryOpen );
				end );
				
				if ( !itemTable.Drop ) then
					DropButton:SetDisabled( true );
				end;
				
				local UseButton = SimpleButton( DPanel, 310, 10, 60, 20, "Use", function()
					LocalPlayer():ConCommand( "eps_useitem " .. v2 )
					InventoryOpen = !InventoryOpen;
					gui.EnableScreenClicker( InventoryOpen );
					self:SetVisible( InventoryOpen );
				end );
				
				if ( !itemTable.Use ) then
					UseButton:SetDisabled( true );
				end;
				
				panelList:AddItem( DPanel );
			end;
			
			panelList:SetAutoSize( true );
			panelList:SetPadding( 4 );
			panelList:SetSpacing( 4 );
			
			collapsibleCategory:SetLabel( v.category );
			collapsibleCategory:SetPadding( 4 );
			collapsibleCategory:SetContents( panelList );
			
			self.Inventory:AddItem( collapsibleCategory );
		end;
	end;
end;

function PANEL:BuildCraftTab()
	local CraftingForm = vgui.Create( "DForm", self.Crafting );
	CraftingForm:SetSize( self:GetWide() - 20, 150 );
	CraftingForm:SetPos( 5, 0 );
	CraftingForm:SetPadding( 4 );
	CraftingForm:SetSpacing( 4 );
	CraftingForm:SetName( "Crafting Recipes" );
	
	for k, v in pairs( epsilonScript.Crafting.Stored ) do
		local collapsibleCategory = vgui.Create( "DCollapsibleCategory" );
		collapsibleCategory:SetLabel( v.Name );
		collapsibleCategory:SetExpanded( false );
		collapsibleCategory:SetPadding( 4 );
		
		local DPanel = vgui.Create( "DPanel" );
		DPanel:SetSize( 0, 80 );
		DPanel.Paint = function() draw.DrawSimpleRect( 0, 0, DPanel:GetWide(), DPanel:GetTall(), Color( 90, 90, 90, 255 ) ) draw.DrawSimpleOutlined( 0, 0, DPanel:GetWide(), DPanel:GetTall(), Colour_Black ) end; 
		
		local spawnIcon = vgui.Create( "SpawnIcon", DPanel );
		spawnIcon:SetIconSize( 48 );
		spawnIcon:SetPos( 3, 3 );
		spawnIcon:SetModel( v.Model );
		spawnIcon:SetToolTip()
		
		local Label = SimpleLabel( DPanel, v.Information, Colour_White, "Default", 3, 64 );
		local requiredItemsLabel = SimpleLabel( DPanel, "Required Items:", Colour_White, "DefaultBold", 56, 3 );
		local makeButton = SimpleButton( DPanel, 55, 30, 60, 20, "Make", function() LocalPlayer():ConCommand( "eps_craftitem " .. k ) end );
		
		local requiredItemsList = vgui.Create( "DPanelList", DPanel );
		requiredItemsList:SetSize( 280, 55 );
		requiredItemsList:SetPos( 150, 4 );
		requiredItemsList:SetPadding( 4 );
		requiredItemsList:SetSpacing( 2 );
		requiredItemsList:EnableVerticalScrollbar( true );
		
		local requiredItems = epsilonScript.Crafting.GetRequiredItems( k );
		
		for k2, v2 in pairs( requiredItems ) do
			local itemName = epsilonScript.Items.GetItem( k2 ).Name;
			
			local requiredItemLabel = SimpleLabel( nil, itemName .. "(x" .. v2 .. ")", Colour_Green, "Default", nil, nil );
			
			requiredItemLabel.Think = function()
				if ( LocalPlayer().Inventory[ k2 ] == nil or LocalPlayer().Inventory[ k2 ] < v2 ) then
					requiredItemLabel:SetColor( Colour_Red );
				else
					requiredItemLabel:SetColor( Colour_Green );
				end;
			end;
			
			requiredItemsList:AddItem( requiredItemLabel );
		end;
		
		collapsibleCategory:SetContents( DPanel );
		CraftingForm:AddItem( collapsibleCategory )
	end;
end;

function PANEL:Think()
	self.WeightAmount:SetLabelText( math.Round( LocalPlayer().CurrentInventoryWeight * 10 ) / 10 .. " / " .. LocalPlayer().MaxInventoryWeight .. " kg" );
	self.WeightAmount:SetDrainW( LocalPlayer().CurrentInventoryWeight / LocalPlayer().MaxInventoryWeight );
	
	DPropertySheet.Think( self );
end;
vgui.Register( "eps_Inventory", PANEL, "DPropertySheet" );

concommand.Add( "eps_inventory", function()
	InventoryOpen = !InventoryOpen;

	gui.EnableScreenClicker( InventoryOpen );
	
	if ( Inventory ) then
		Inventory:SetVisible( InventoryOpen );
		
		if ( InventoryOpen ) then
			Inventory:RebuildInventory();
		end;
	else
		Inventory = vgui.Create( "eps_Inventory" );
		Inventory:RebuildInventory();
	end;
end );