--[[
	lua/autorun/client/OubHack/vgui/oh_DSettingsSwitch.lua
	Oubliette | (STEAM_0:1:34029133)
	===DStream===
]]

/******************************************************************************
 .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
*******************************************************************************/

-- Tab used at the top of the screen.

local PANEL = {}

PANEL.Colour = Color( 20, 20, 20, 200 )
PANEL.OutlineColour = Color( 0,255,0,255 )
PANEL.AColour = Color(255,255,255,255)
PANEL.DAColour = Color(0,0,0,255)

local Gradient = surface.GetTextureID( "gui/gradient_down" )
local Gradient_UP = surface.GetTextureID( "gui/gradient_up" )


function PANEL:Init()
	self.Active = false;
	self.DeactiveTitle = "Deactivated"
	self.ActiveTitle = "Settings menu"
	self.Color = Color(0,255,0,255)
	self.OffsetW = 1
	self.Move = -1
	self.CalledDoOpen = false;
	self.CalledDoClose = false;
end

function PANEL:SetDeactiveTitle(title)
	self.DeactiveTitle = title
end

function PANEL:SetActiveTitle(title)
	self.ActiveTitle = title
end

function PANEL:SetActive(bool)
	self.Active = bool
end

function PANEL:LogPos(x,y)
	self.XX = x;
	self.YY = y;
end

function PANEL:Hide()
	self.Move = -2
end

function PANEL:Show()
	self.Move = -1
end

surface.CreateFont ("ScoreboardText", ScreenScale(5), 400, true, false, "oh_small")
surface.CreateFont ("ScoreboardText", ScreenScale(10), 400, true, false, "oh_large")
function PANEL:Paint()
	local x,y = -2,0
	local w,h = self.OffsetW,self:GetTall()
	self:SetPos(0,0)
	self:SetSize(self.OffsetW,self:GetTall())
	
	if self.OffsetW != 1 then -- Only draw if it's not hiding (15px)
		local scale = math.Clamp( h/(ScrH()*0.25), 0, 1 )
		surface.SetDrawColor( self.Color.r, 0, 0, 80+80*scale )
		surface.SetTexture( Gradient )
		surface.DrawTexturedRect( x+2, y+2, w-4, h*0.75 )
		
		local scale = math.Clamp( h/(ScrH()*0.25), 0, 1 )
		surface.SetDrawColor( 0, self.Color.g, 0, 80+80*scale )
		surface.SetTexture( Gradient_UP )
		surface.DrawTexturedRect( x+2, y+2, w-4, h )
		
			surface.SetDrawColor(self.Color)
		surface.DrawRect( x+1, y+1, w-2, 1 ) 
		surface.DrawRect( x+1, y+1, 1, h-2 )
		surface.DrawRect( x+1, y+h-2, w-2, 1 )
		surface.DrawRect( x+w-2, y+2, 1, h-2 )
	end
	local titlexoff = 50
	if self.Active then titlexoff = 0 end
	if self.OffsetW > 110 then
		draw.DrawText(self.ActiveTitle or "", "ScoreboardText", self.OffsetW - (100+titlexoff),5, self.AColour,TEXT_ALIGN_LEFT)
	end
	local stitle = "Open"
	if self.Active then
		stitle = "Close"
	end
	if self.OffsetW > 120 then
		draw.DrawText(stitle, "ScoreboardText", self.OffsetW - 5, self:GetTall() - 25, self.AColour,TEXT_ALIGN_RIGHT,TEXT_ALIGN_BOTTOM)
	end
	if self.Active then
		self.Color.g = math.Approach(self.Color.g,255,25)
		self.Color.r = math.Approach(self.Color.r,0,25)
	else
		self.Color.g = math.Approach(self.Color.g,0,25)
		self.Color.r = math.Approach(self.Color.r,255,25)
	end

	if self.Active then
		self.OffsetW = math.Approach(self.OffsetW,ScrW(),100)
		self.CalledDoClose = false;
		if self.OffsetW == ScrW() and !self.CalledDoOpen then
			if self.DoOpen and type(self.DoOpen) == "function" then self.DoOpen() end
			self.CalledDoOpen = true;
		end
	elseif self.OffsetW > 150 then
		self.OffsetW = math.Approach(self.OffsetW,0,100)
		self.CalledDoOpen = false;
		if self.OffsetW <= 150 and !self.CalledDoClose then
			if self.DoClose and type(self.DoClose) == "function" then self.DoClose() end
			self.CalledDoClose = true;
		end
		
	else
		if self.Move == -1 then
			self.OffsetW = math.Approach(self.OffsetW,50,10)
			if self.OffsetW == 50 then self.Move = 0 end
		elseif self.Move == -2 then 
			self.OffsetW = math.Approach(self.OffsetW,1,10)
		elseif self.Move == 1 then
			
			self.OffsetW = math.Approach(self.OffsetW,150,10)
			if self.OffsetW == 150 then self.Move = 0 end
		end
	end


end


function PANEL:OnCursorEntered()
	if self.Active then return end
	
	self.Move = 1 
end
 
 
function PANEL:OnCursorExited()
	if self.Active then return end
	self.Move = -1
end

function PANEL:OnMousePressed()
	if self.Active then
		self.Active = false;
	else
		self.Active = true;
	end
	if self.DoClick and type(self.DoClick) == "function" then self.DoClick() end
end
oubhack:RegisterVgui("oh_DSettingsSwitch",PANEL,"DPanel")