
.namespace PopupManager
g_oldphase = ""
g_popupYN = false
g_popupansw = false
g_message = ""
--popup pour un nouvel objet (c'est du code spécifique finalement)
function OpenPopup(message,utnotify,objname)
	g_popupYN = false
	if objname == "Livre" then
		popups_objectinventorytxt.Text = GlobalFunctions.GetText("msg_ig_grandpre")
		popups_objectinventory.Image = "media/graph/inventory/livre_ferme.png"		
		PlaySoundEffect("int_bookadd")
	elseif objname ~= nil then
		local objut = unit(objname)
		popups_objectinventorytxt.Text = GlobalFunctions.GetText(objut.NameLoc)
		popups_objectinventory.Image = objut.Image	
	end

	self.curobjname = objname
	AssertPopup(type(message) == "string", "prop param openpopup")
	-- g_oldphase = GetVariables("CurrentGamePhase")
	if string.find(GetCurrentManager().Name,"SearchSite") then
		g_oldphase = "SearchSite"
	else
		g_oldphase = "Closeup"
	end
	self.utnotify = utnotify
	popups_objectinventorytxt.Flags_Visible = true
	popups_objectinventory.Flags_Visible = true
	SetVariables("CurrentGamePhase","Document")
	popups_parent.Enabled = true
	SpriteFader.Fade(popups_background,true,0.4)
	SpriteFader.Fade(popups_content,true,0.4)
	popups_content.Text = GlobalFunctions.GetText(message)
	PopupManager.active = true
	-- yn_parent.Enabled = false
	yn_yes.Enabled = false
	yn_no.Enabled = false
	info_ok.Enabled = false
end

function OpenPopupInfo(message,utnotify)
	-------Localization-------
	info_ok_txt.Text = GlobalFunctions.GetText("btn_ok")
	--------------------------	
	OpenPopupYN(message,utnotify)
	yn_yes.Enabled = false
	yn_no.Enabled = false
	info_ok.Enabled = true
end

function OpenPopupYN(message,utnotify,customyes,customno)
	-------Localization-------
	yn_yes_txt.Text = GlobalFunctions.GetText("btn_yes")
	yn_no_txt.Text = GlobalFunctions.GetText("btn_no")
	--------------------------
	if customyes ~= "" then
		yn_yes_txt.Text = customyes
	end
	if customno ~= "" then
		yn_no_txt.Text = customno
	end
	g_popupYN = true	
	self.utnotify = utnotify
	g_message = message
	popups_parent.Enabled = true	
	popups_objectinventorytxt.Flags_Visible = false
	popups_objectinventory.Flags_Visible = false
	AssertPopup(type(message) == "string", "prop param openpopup YN")
	if string.find(GetCurrentManager().Name,"SearchSite") then
		g_oldphase = "SearchSite"
		popups_background.Position_X = 411
	elseif string.find(GetCurrentManager().Name,"_c") then
		g_oldphase = "Closeup"
		popups_background.Position_X = 305
	else
		g_oldphase = "Menu"	
		popups_background.Position_X = 305
		
	end

	SetVariables("CurrentGamePhase","Document")

	-- yn_parent.Enabled = true
	
	yn_yes.Enabled = true
	yn_no.Enabled = true
	info_ok.Enabled = false
	popups_fade.Enabled = true
	SpriteFader.Fade(popups_fade,true,0.4)
	SpriteFader.Fade(popups_background,true,0.4)
	SpriteFader.Fade(yn_yes,true,0.4)
	SpriteFader.Fade(yn_no,true,0.4)
	popups_content.Text = GlobalFunctions.GetText(message)
	PopupManager.active = true	
	
end

function OnEnable()
	PopupManager.active = false
	OPInput.RegisterListener(self, EVENT_MOUSE_LUP)
	OPInput.SetListenerPriority(self,LISTENER_HIGH_PRIORITY)
	self.Flags_AcceptAllInput = true
end



function OnWbpMouseLUp()
	if not PopupManager.active then
		return false
	end	
	-- PlaySoundEffect("int_click01")
	if yn_yes.Enabled == false and info_ok.Enabled == false then
		PopupManager.active = false	
		PlaySoundEffect("int_click")		
		SpriteFader.Fade(popups_background,false,0.4,self)
		return true
	end
	if self.Name == "yn_yes" or self.Name == "info_ok" then
		PopupManager.active = false
		g_popupansw = true		
		SpriteFader.Fade(popups_background,false,0.4,self)
		SpriteFader.Fade(yn_yes,false,0.4)
		SpriteFader.Fade(yn_no,false,0.4)
		SpriteFader.Fade(info_ok,false,0.4)	
		return true		
	end
	if self.Name == "yn_no" then
		PopupManager.active = false
		g_popupansw = false		
		SpriteFader.Fade(popups_background,false,0.4,self)
		SpriteFader.Fade(yn_yes,false,0.4)
		SpriteFader.Fade(yn_no,false,0.4)
	
		return true
	end	
	return false
end

function OnFaderEnd()
	PopupManager.active = false	
	SetVariables("CurrentGamePhase",g_oldphase) 
	if g_popupYN == true then
		if  GetCurrentManager().OnPopupYNLeave ~= nil then
			GetCurrentManager().OnPopupYNLeave(g_popupansw, g_message)
		end
		if self.utnotify ~= nil and self.utnotify.OnPopupYNLeave ~= nil then
			OPLog.Log(self.utnotify.Name.." est notifié")
			self.utnotify.OnPopupYNLeave(g_popupansw, g_message)

		end		
		OPLog.Log(self.utnotify.Name.." est pas notifié")
	else
		if  GetCurrentManager().OnPopupLeave ~= nil then
			GetCurrentManager().OnPopupLeave(self.curobjname)
			OPLog.Log("passé par ici")
		end	
		if self.utnotify~= nil and self.utnotify.ParentEnabled and self.utnotify.OnPopupLeave ~= nil then
			self.utnotify.OnPopupLeave(self.curobjname)
			OPLog.Log("passé par là")

		end
	end
	


end

