-- ************************************ GuineaPig ************************************ --
GuineaPig = deriveLuaClass("GuineaPig", "CareCharacter")

function GuineaPig:init(desc)
	desc.hitMaskThreshold = -1
	GuineaPig.superInit(desc)
	self.hitMaskThreshold = -1

	self.drawOutline = false
	self.walking = false
	self.visible = false
	self.clickable = true
	self.baseAnimation = desc.baseAnimation or "GUINEAPIG"
	self.moveType = self.moveType or "random"
	self.showTime = self.showTime or 1000
	self.walkFrameDistanceHor = 37/(2*2)
	self.walkFrameDistanceVer = 10

	self.valueMultipliers = self.valueMultipliers or {}
	self.valueMultipliers.walkSpeed = 1.2

	self:pickNewWalkDelay()
	self:pickNewPosition()
	if (self.targetPos) then
		self.location = self.targetPos
	end

	self.__getters.shadowResource = function() return "" end
end

function GuineaPig:onClick()
	if (self.visible and not self.dummy) then
		player:foundGuineaPig()
		level:heartsToBar(1, self.x, self.y, false)
		taskSystem:cancelAllTasksFor(self)

		local tmp = level:add({
			class="SpriteExt", x=self.x, y=self.y,
			children={
				{class="ParticleEffect", library="MINIGAME_GOOD", section="objects", start=true},
				{class="ParticleEffect", library="FOUND_GUINEAPIG", section="objects", start=true},
				{class="ParticleEffect", library="FOUND_GUINEAPIG", section="objects", start=true, rotation=180},
			},
		})
		tmp:animate({ {delay=4000}, {remove=true} })

		self:animate({
			{alpha=0, duration=1}, {delay=80},
			{alpha=1, duration=1}, {delay=80},
			{alpha=0, duration=1}, {delay=80},
			{alpha=1, duration=1}, {delay=80},
			{alpha=0, duration=1}, {delay=80},
			{alpha=1, duration=1}, {delay=80},
			{alpha=0, duration=1}, {delay=80},
			{alpha=1, duration=1}, {delay=80},
			{alpha=0, duration=1}, {delay=80},
			{alpha=1, duration=1}, {delay=80},
			{alpha=0, duration=1}, {delay=80},
			{alpha=1, duration=1}, {delay=80},
			{remove=true},
		})

		self:playSample("SND_GUINEAPIG_POPS_UP:sound")
		level:doFloater(ghf.getString("GUINEA_FOUND"), self.x, self.y-30)
		self.clickable = false
	end
end

function GuineaPig:onTick(time)
	self:tick(time)

	if (not self.walking and self.walkDelay > 0) then
		if (level.needsToAddLevelTime or self.dummy) then
			self.walkDelay = self.walkDelay - time
			if (self.walkDelay <= 0) then
				self:pickNewWalkDelay()
				self:pickNewPosition()

				if (self.moveType == "random" or self.moveType == "cycle") then
					if (self.targetPos) then
						self:addTask({class="SetTask", visible=true, walking=true})
						if (not self.dummy) then
							self:addTask({class="PlaySampleTask", sound="SND_GUINEAPIG_POPS_UP:sound"})
							self:addTask({class="PlaySampleTask", sound="SND_GUINEAPIG_RUNS:sound"})
						end
						self:addTask({class="WalkToTask", target=self.targetPos})
						self:addTask({class="SetTask", visible=false, walking=false})
					end
				else
					if (self.targetPos) then
						self.location = self.targetPos
					end
					if (self.moveType) then
						self:addTask({class="SetTask", visible=true, walking=true})
						if (not self.dummy) then
							self:addTask({class="PlaySampleTask", sound="SND_GUINEAPIG_POPS_UP:sound"})
						end
						self:addTask({class="PlayAnimationTask", animation=self.moveType, cycles=1, frameTime=100, whenFinished="stay", wait=true})
						self:addTask({class="Task", duration=self.showTime})
						self:addTask({class="PlayAnimationTask", animation=self.moveType.."_HIDE", cycles=1, frameTime=100, whenFinished="stay", wait=true})
						self:addTask({class="SetTask", visible=false, walking=false})
					end
				end
			end
		end
	end
end

function GuineaPig:pickNewWalkDelay()
	self.walkDelay = math.Random(self.minWalkDelay, self.maxWalkDelay)
end

function GuineaPig:pickNewPosition()
	if (self.positions) then
		local num = table.icount(self.positions)
		assert(num > 0, "No positions for this pig")
		if (self.moveType == "cycle") then
			if (self.curTargetIdx == nil or self.curTargetIdx > num) then
				self.curTargetIdx = 1
			else
				self.curTargetIdx = self.curTargetIdx + 1
			end
			self.targetPos = self.positions[self.curTargetIdx]
		elseif (num > 1) then
			local newPos = self.targetPos
            local tries = 1000
			while (self.targetPos == newPos and tries > 0) do
				newPos = self.positions[math.Random(num)]
                tries = tries - 1
			end
			self.targetPos = newPos
		end
	end
end
