/******************************************************************************
 .d88888b.  888     888 888888b.   888    888        d8888  .d8888b.  888    d8P  
d88P" "Y88b 888     888 888  "88b  888    888       d88888 d88P  Y88b 888   d8P   
888     888 888     888 888  .88P  888    888      d88P888 888    888 888  d8P    
888     888 888     888 8888888K.  8888888888     d88P 888 888        888d88K     
888     888 888     888 888  "Y88b 888    888    d88P  888 888        8888888b    
888     888 888     888 888    888 888    888   d88P   888 888    888 888  Y88b   
Y88b. .d88P Y88b. .d88P 888   d88P 888    888  d8888888888 Y88b  d88P 888   Y88b  
 "Y88888P"   "Y88888P"  8888888P"  888    888 d88P     888  "Y8888P"  888    Y88b 
 By Oubliete and UnknownAlias
*******************************************************************************/
-- The derma behind the settings menu.

local PANEL = {}

function PANEL:Init()
	self.Sheets = {}
	self.PanelQue = {}
	self.RemovePanelQue = {}
	self.OpenPanels = {}
	self.Padding = 5;
	self.TPanel = {}
end

function PANEL:AddSheet(name,panel,icon,subtext)
	self.TPanel[name] = panel;
	self.Sheets[#self.Sheets+1] = {Name=name,Panel=panel,Icon=icon,Subtext=subtext,tabpanel=nil}
	self:Update()
end

function PANEL:RemoveSheet(name)
	for k,v in pairs(self.Sheets) do
		if v.Name == name then 
			if v.tabpanel then v.tabpanel:Remove() end
			v.tabpanel = nil;
			table.remove(self.Sheets,k)
		end
	end
	self:Update()
end
		

function PANEL:Update() -- Call this AFTER you've added the tabs
	local div =  ((self:GetWide()-(self.Padding))/ (#self.Sheets)) 
	local x = self.Padding;
	local y = self.Padding;
	table.SortByMember(self.Sheets, "Name",function(a,b) return a>b end)
	for k,v in pairs(self.Sheets) do
		if v.tabpanel then v.tabpanel:Remove() end
		v.tabpanel = nil;
		v.tabpanel = oubhack:CreateVgui("oh_settings_tab_button",self)
		v.tabpanel:SetPos(x,y)
		v.tabpanel:SetSize(div-self.Padding-oubhack.cp_offset,self:GetTall()-10-oubhack.cp_offset)
		v.tabpanel:SetTitle(v.Name)
		v.tabpanel:SetSubtext(v.Subtext)
		v.tabpanel:SetPanel(v.Panel)
		v.tabpanel.index = k;
		x = x + div;
	end

end

function PANEL:RemovePanels()
	for k,v in pairs(self.Sheets) do
		v.tabpanel:Remove()
		v.tabpanel = nil;
	end
	for k,v in pairs(self.PanelQue) do
		print(v.Panel)
		v.Panel:SetVisible(false)
		v.Panel:Remove()
		v.Panel = nil
		table.remove(self.PanelQue,k)
	end
	for k,v in pairs(self.RemovePanelQue) do
		v.Panel:SetVisible(false)
		v.Panel:Remove()
		v.Panel = nil
		table.remove(self.RemovePanelQue,k)
	end
	self:CloseAllPanels()
	self.Sheets = {};
end

function PANEL:Think() -- Using this becuase it's called repeataly
	local endx = ScrW()/2;
	local endy = ScrH()/2;
	
	local endw = ScrW()/2;
	local endh = ScrH()/2;
	
	local endx = ScrW()/2 - (endw/2);
	local endy = ScrH()/2 - (endh/2);
	for k,v in pairs(self.PanelQue) do
		if v.x != endx then
			v.x = math.Approach(v.x,endx,30);
		end
		if v.y != endy then
			v.y = math.Approach(v.y,endy,30);
		end
		if v.width != endw then
			v.width = math.Approach(v.width,endw,50);
		end
		if v.height != endh then
			v.height = math.Approach(v.height,endh,50);
		end
		v.Panel:SetPos(v.x,v.y);
		v.Panel:SetSize(v.width,v.height);
		if (v.x == endx) and (v.y == endy) and (v.width == endw) and (v.height == endh) then self.PanelQue[k] = nil end;
	end
	
	for k,v in pairs(self.RemovePanelQue) do
		if v.x != v.Panel.OldX then
			v.x = math.Approach(v.x,v.Panel.OldX,30);
		end
		if v.y != v.Panel.OldY then
			v.y = math.Approach(v.y,v.Panel.OldY,30);
		end
		if v.width != v.Panel.OldW then
			v.width = math.Approach(v.width,v.Panel.OldW,50);
		end
		if v.height != v.Panel.OldH then
			v.height = math.Approach(v.height,v.Panel.OldH,50);
		end
		v.Panel:SetPos(v.x,v.y);
		v.Panel:SetSize(v.width,v.height);
		if (v.x == v.Panel.OldX) and (v.y == v.Panel.OldY) and (v.width == v.Panel.OldW) and (v.height == v.Panel.OldH) then 
			self:RemoveOpenPanelLog(v.Panel)
			self.RemovePanelQue[k] = nil; 
			v.Panel:SetVisible(false);
			oubhack.Frame:AddSheet(v.Panel.SD.Name,v.Panel.SD.Panel,v.Panel.SD.Icon,v.Panel.SD.Subtext)
			oubhack.Frame:Update()
		end
	end
end

function PANEL:AddOpenPanelLog(panel)
	self.OpenPanels[#self.OpenPanels+1] = panel;
end

function PANEL:RemoveOpenPanelLog(panel)
	for k,v in pairs(self.OpenPanels) do
		if v == panel then
			table.remove(self.OpenPanels,k);
		end
	end
end

function PANEL:CloseAllPanels()
	for k,v in pairs(self.OpenPanels) do
		if v and ValidPanel(v) then
			v:SetVisible(false)
		else
			v = nil;
		end
	end
end

function PANEL:OpenPanel(panel,X,Y,w,h,sheet_data)
	panel.OldX = X;
	panel.OldY = Y;
	-- This is used when it's minimized
	panel.OldW = w;
	panel.OldH = h;
	panel.SD = sheet_data;
	self.PanelQue[#self.PanelQue+1] = {Panel=panel,x=X,y=Y,width=w,height=h}
	panel:SetPos(X,Y)
	panel:SetSize(w,h)
	panel:SetVisible(true)
	
	self:AddOpenPanelLog(panel)
end

function PANEL:ClosePanel(panel)
	self.RemovePanelQue[#self.RemovePanelQue+1] = {Panel=panel,x=panel.x,y=panel.y,width=panel:GetWide(),height=panel:GetTall()}
end

function PANEL:OnMousePressed()
	print("you're clicking on the settings tab panel")
end


oubhack:RegisterVgui("oh_settings_tab",PANEL)