--[[ 
	Fichier de la classe MenuButton
	Ces boutons sont représentés par 3 sprites différents
	- état normal
	- état highlight
	- état appuyé
--]]

-- Attention, ne pas oublier d'utiliser SetUnitToNotify pour recevoir le clic dans l'unit qui en a besoin

.namespace MenuButton

function OnEnable()
	self.Pressed = false
	
	self.Text = nil
	if self.IDText ~= nil and self.IDText ~= "" then
		self.Text = OPUnitFactory.CreateUnit("Text")
		self.Text.Parent = self
		self.Text.Name = self.Name.."_Text"
		self.Text.Alignment = 1
		self.Text.AutoDimension_X = false
		self.Text.AutoDimension_Y = true
		self.Text.WordWrap = true
		self.Text.CharSpacing = 1
		self.Text.LineSpacing = -20
		
		if self.Font~= nil then
			self.Text.Font = self.Font
		else
			self.Text.Font = Font_Arial22
		end
		
		self.Text.Text = GlobalFunctions.GetText(self.IDText,true)
		
		self.Text.Dimension_Width = self.Dimension_Width - 10
		self.Text.Position_X = self.Dimension_Width/2 - self.Text.Dimension_Width/2
		self.Text.Position_Y = self.Dimension_Height/2 - self.Text.Dimension_Height/2 - 4
	end
	
	self.gv = OPUnitFactory.GetUnit("GameVariables")
	self.firstframe = true
end

function SetUnitToNotify(unit)
	self.UnitToNotify = unit
end

function OnWbpMouseLUp(x,y)
	if not self.CanClickOn() then
		return
	end
	
	self.Pressed = false
	self.SetFrame(0)
	
	--if self.UnitToNotify ~= nil and self.UnitToNotify.OnButtonPressed ~= nil then
	--	self.UnitToNotify.OnButtonPressed(self.Name)
	--end
	
	return true
end


function OnWbpMouseLDown(x,y)
	if not self.CanClickOn() then
		return
	end
	
	self.Pressed = true
	
	local value, nbTokenImage = string.gsub(self.Image,'|','|')
	if nbTokenImage > 1 then
		self.SetFrame(2)
	end
	
	if self.UnitToNotify ~= nil and self.UnitToNotify.OnButtonPressed ~= nil then
		self.UnitToNotify.OnButtonPressed(self.Name)
	end
	
	return true
end


function SetCursor()
	if self.CanClickOn() then
		if self.GetFrame() < 2 then
			self.OnMouseOn()
		end
		return true
	end
end


function CanClickOn()
	local res = self.Flags_Visible and (
		OPUnitFactory.GetUnit("GameVariables","DataArray").IsEqual("CurrentGamePhase","Menu") or
		OPUnitFactory.GetUnit("GameVariables","DataArray").IsEqual("CurrentGamePhase","LevelMap")
		)
	res = res and (self.Inactive == false)
	
	if self.PrivateCanClickOn ~= nil then
		res = res and self.PrivateCanClickOn()
	end
	
	return res
end


function OnMouseOff()
	if not self.CanClickOn() then
		return
	end
	self.SetFrame(0)
end

function OnMouseOn()
	if not self.CanClickOn() then
		return
	end
	
	local value, nbTokenImage = string.gsub(self.Image,'|','|')
	if nbTokenImage > 0 then
		self.SetFrame(1)
		
	end
		
	if self.gv.Get("lastonmouseon") ~= self.Name and self.gv.Get("lastonmouseon") ~= "" then
		local ut = OPUnitFactory.GetUnit(self.gv.Get("lastonmouseon"))
		if ut ~= nil and ut.ParentEnabled and not ut.Inactive then
			--OPLog.Log("on remet la frame 0 à "..ut.Name)
			ut.SetFrame(0)
		end
	end
	
	self.gv.Set("lastonmouseon",self.Name)

end

function OnDisable()
	self.SetFrame(0)
	
	if self.Text ~= nil then
		OPUnitFactory.CloseUnit( self.Text )
		self.Text = nil
	end
end



