.namespace ValuableItem

FLAG_MOUSER_PRESSED = 1
FLAG_MOUSEL_PRESSED = 2

g_GameVariables = GameVariables
g_Conditions = Conditions
g_FlagsFunctions = FlagsFunctions
g_background = nil
g_x = 0
g_y = 0

function OnEnable()
end


function OnWbpMouseLDown(x,y,buttonPressed)
	OPLog.Log("click sur "..self.Name)
	if not self.CanClickOn() then
		return
	end
	
	 if LeftInventory.toclose then
		 LeftInventory.SetHighlight(self.obj)
	 end

	return true
end

function OnMouseOff()
	-- if self.Name == LeftInventory.g_mainvignette.Name  then
		-- return
	-- end	
	refpos = Vector:new(self.g_x,self.g_y)
	refpos_bg = Vector:new(self.g_x,self.g_y-15)
	if LeftInventory.toclose then
		Inventory_txt.Text = GlobalFunctions.GetText(InventoryManager.GetCurrentObject().NameLoc)
	end
		SpriteZoom.Zoom(self,0.75, 0.25, nil,refpos)
		SpriteZoom.Zoom(self.g_background,0.75, 0.25, nil, refpos_bg)	
end

function OnMouseOn()
	-- if self.CanClickOn() then
		-- return
	-- end

	if self.Flags_Visible == true  then
		refpos = Vector:new(self.g_x,self.g_y)
		refpos_bg = Vector:new(self.g_x,self.g_y-15)
		PlayRandomSound({"int_objetrollover01","int_objetrollover02"})
		
		SpriteZoom.Zoom(self,1.0, 0.25, nil,refpos)
		SpriteZoom.Zoom(self.g_background,1.0, 0.25, nil, refpos_bg)
		for i,vignette in pairs(LeftInventory.g_vignettes) do
			-- OPLog.Log("hello world")
			if not vignette.Name == self.Name then
				SpriteZoom.Zoom(vignette,1.0, 0.25, nil,vignette.refpos)
				SpriteZoom.Zoom(vignette.g_background,1.0, 0.25, nil, vignette.refpos_bg)
			end
		end
		 if LeftInventory.toclose then
			 	self.oldtext = Inventory_txt.Text
				-- OPLog.Log(GlobalFunctions.GetText(self.NameLoc))
				Inventory_txt.Text = GlobalFunctions.GetText(self.NameLoc)
				
		 end
	end
end

function SetTextToThisBox()
	-- Search_ButtonInventory.UpdateTextPos(self)
	-- Search_ButtonInventory.SetTextToInventoryName(unit(self.obj).NameLoc)
	-- InventoryManager.GetCurrentObject()

end

function StartDrag()
	Cursor.SetObjectDragged(InventoryManager.GetCurrentObject())	
	PlaySoundEffect("int_click")
	-- if DebugOptions.UseTutorial and InventoryManager.GetCurrentObject().Name == "InventoryKey" then
		-- TutorialManager.OnClickObjectInventory()
	-- end
end

function OnWbpMouseLUp()
	
	if not self.CanClickOn() then
		
		return
	end
OPLog.Log("Tentative de clic sur "..self.Name)
	if not Cursor.IsDragging() then
			 if InventoryManager.GetCurrentObject().Document ~= "" then
				--Pas genial
				 filetoopen = string.gsub(InventoryManager.GetCurrentObject().Document, "_fre.png", "_"..DebugOptions.curlanguage..".png")
				 DocumentManager.OpenDocument(filetoopen,InventoryManager.GetCurrentObject().Name)
				
			 else
				StartDrag()
			 end
			LeftInventory.CloseInventory()
	end
	
	-- if Search_ButtonInventory.open then
		-- Search_ButtonInventory.CloseScroll()
	-- end
	
	return true
end

function OnDropObject()
	--bidouille qui est en fait le onmouselup qui est pas appelé car on dragndrop un objet
	OnWbpMouseLUp()
end

function CanClickOn()
	local res = self.Flags_Visible and ( 
	IsEqualVariables("CurrentGamePhase","SearchSite") 
	or IsEqualVariables("CurrentGamePhase","Closeup") 
	or  IsEqualVariables("CurrentGamePhase","Tutorial"))
	
	-- res = res and self.Inactive ~= true
	
	-- if self.PrivateCanClickOn ~= nil then
		-- res = res and self.PrivateCanClickOn()
	-- end
	
	return res
end

function OnDisable()

end

-- function isDocument()
	-- --Renvoi true si l'objet est dans notre liste de documents
	-- for ind,tab in pairs(DOCUMENTLIST) do
		-- --OPLog.Log("tab "..tab.obj.." name "..InventoryManager.GetCurrentObject().Name)
		-- if tab.obj == InventoryManager.GetCurrentObject().Name then
			-- --DocumentViewer.Image = tab.img..DebugOptions.curlanguage..".png"
			-- self.documents_img = tab.img..DebugOptions.curlanguage..".png"
			-- return true
		-- end
	-- end
	-- return false	
-- end
--attention c'est appelé à chaque frame ?
function SetCursor()
	if self.CanClickOn() then
		OPUnitFactory.GetUnit("Cursor","Cursor").ChangeCursor(CURSOR_OBJECT)
		return true
	end
	
	return false
end

