-- ************************************ HiddenObjectEvent ************************************ --
HiddenObjectEvent = deriveLuaClass("HiddenObjectEvent", "Event")

-- ###### Configuration variables ######
HiddenObjectEvent.scoreOneDone = 10			-- Score for finding one item
HiddenObjectEvent.scoreAllDone = 100		-- Bonus for finding all items
HiddenObjectEvent.objectsTodo = {}
HiddenObjectEvent.funcFound = HiddenObjectEvent.onObjectFound
HiddenObjectEvent.hideAllObjects = false
HiddenObjectEvent.showHintArrow = true

function HiddenObjectEvent.onObjectFound(event, obj)
end

function HiddenObjectEvent:init(desc)
	HiddenObjectEvent.superInit(desc)
	self.items = {}
	self.itemsFound = 0
	self.completed = false
	self.funcFound = desc.funcFound or self.funcFound
end

function HiddenObjectEvent:onInitialized()
	for _,v in pairs(self.objectsTodo) do
		local newItem = level:add(v)
		if (newItem) then
			table.insert(self.items, newItem)
			newItem.event = self
			if (self.hideAllObjects) then
				newItem.visible = false
			end
		end
	end

	local objects = level:getCurrentObjectNamesByType("HiddenObject")
	for i=1, table.icount(objects) do
		local object = level:getObject(objects[i])
		object.event = self
		object.drawOutline = false
		table.insert(self.items, object)
		if (self.hideAllObjects) then
			object.visible = false
		end
	end
	self.nrOfItems = table.icount(self.items)
	local eventBar = eventBar
	eventBar.startValue = 0
	eventBar.targetValue = math.max(1, self.nrOfItems)
	eventBar.progress = 0
	eventBar.description = self.eventBarText or "TEXT_EVENT_HIDDEN_OBJECT"
	if (self.eventBarImage) then
		eventBar.icon = self.eventBarImage
	else
		eventBar.icon = self.baseAnimation.."_BAR:"..self.defaultSection
	end
end

function HiddenObjectEvent:handleTrigger(trigger, data)
	HiddenObjectEvent.super.handleTrigger(self, trigger, data)
	if (trigger == "showHiddenItems") then
		self:showHiddenItems()
	end
end

function HiddenObjectEvent:showHiddenItems()
	for _,v in pairs(self.items) do
		if (not v.visible) then
			v.visible = true
			v.clickable = true
			v.alpha = 0
			v:animate({alpha=1, duration=1000})
		end
	end
end

function HiddenObjectEvent:onStart()
	self:showHiddenItems()

	if (self.showHintArrow) then
		self:placeHintArrow()
	end

	eventBar:activate()
end

function HiddenObjectEvent:placeHintArrow()
	local hao = self.items[self.hintArrowObjectId]
	if (isString(self.hintArrowObjectId)) then
		hao = level:getObject(self.hintArrowObjectId)
	end
	hintManager:placeArrow({
		type="down",
		name="Hidden object event",
		parent=level.dragLayer,
		x=hao.x + hao.width/2,
		y=hao.y
	})
end

-- ************************************ Hidden Object ************************************ --
HiddenObject = deriveLuaClass("HiddenObject", "Object")

function HiddenObject:init(desc)
	HiddenObject.superInit(desc)

	self.highlightSound = "" -- Mute the highlight
	self.drawOutline = false
end

function HiddenObject:onClick()
	-- First check if we should even be able to click on this object:
	if (self.visible) then
		-- Hide the item

		-- Dissapearing is not by default since some stuff
		-- needs to float to a destination, so fade it out yourself
		self.clickable = false

		-- Increase the found count
		self.event.itemsFound = self.event.itemsFound + 1

		--call func function
		self.event:funcFound(self)

		-- Increase score and pop a floater
		level:addHeartScoreWithFloater(self.event.scoreOneDone, self.location.x, self.location.y - 20, false)

		-- Update the event bar
		eventBar:incValue(1)

		-- Remove the hint arrow
		hintManager:removeArrows({id="Hidden object event"})

		-- Found all items?
		if (self.event.itemsFound ~= self.event.nrOfItems) then
			if (isTable(taskSystem:getAnimationDesc("eventThanks"))) then
				return taskSystem:createAnimation("eventThanks")
			end
		else
	    	-- Set event to completed
	    	self.event.completed = true
			self.event.won = true

			-- Lower curtain
			eventBar:deactivate()

			-- Increase score and pop a floater
			level:addHeartScoreWithFloater(self.event.scoreAllDone, hero.location.x + 30, hero.location.y - 200, false)

			-- Done sequence
			if (isTable(taskSystem:getAnimationDesc("eventWon"))) then
				return taskSystem:createAnimation("eventWon")
			end
		end
	end
end

function HiddenObjectEvent:onShiftFinish()
	-- Remove the hint arrow
	hintManager:removeArrows({id="Hidden object event"})
end
