-- ************************************ Special Event Room 4 (water drinking) ************************************ --
SpecialRoom4 = deriveLuaClass("SpecialRoom4", "Event")

STATE_CLICKED = "clicked"
STATE_NOTCLICKED = "notclicked"
SpecialRoom4.sickGirl = nil
SpecialRoom4.waterPot = nil
SpecialRoom4.waterMeterFrame = nil
SpecialRoom4.waterMeterFiller = nil
SpecialRoom4.waterMeterFillerAlert = nil
SpecialRoom4.energy = 100
SpecialRoom4.energyFromWater = 100
SpecialRoom4.alertLevel = 30
SpecialRoom4.drainSpeed = 0.002
SpecialRoom4.completed = false
SpecialRoom4.meterReachedZero = false
SpecialRoom4.scoreOnWin = 25

function SpecialRoom4:init(desc)
	SpecialRoom4.superInit(desc)
	self.seenArrows = false
end

function SpecialRoom4:createPostShiftAnimation()
	-- Since sequences are the same, just do the lost
	if (isTable(taskSystem:getAnimationDesc("eventLost"))) then
		return self:playIfNotPlayedBefore("eventLost")
	end
end

function SpecialRoom4:onShiftPendingFinish()
	level:shiftFinish()
end

function SpecialRoom4:onShiftFinish()
	self.waterMeterFrame:animate({alpha=0, duration=800})
	self.waterMeterFiller:animate({alpha=0, duration=800})
	self.waterMeterFillerAlert:animate({alpha=0, duration=800})
	if (self.won) then
		level:heartsToBar(self.scoreOnWin,
			self.waterMeterFrame.x + self.waterMeterFrame.width/2,
			self.waterMeterFrame.y + self.waterMeterFrame.height/2, false)
	end
end

function SpecialRoom4:onInitialized()
	local watermeterPos = {x=550, y=120}

	SpecialRoom4.super.onInitialized(self)

	self.won = true

	local desc = {
		class = "Patient",
		name="special_girl",
		hasNormalClothes = false,
		x=618,
		y = 286,
		floor = -10,
		baseAnimation = "PAGEANT_D",
		visible=false,
		clickable=true,
		draining = true,
		special = true,
		doesDrool = false,
	}

	self.sickGirl = level:add(desc)
	self.sickGirl.isSpecial = true
	self.sickGirl.alpha= 0.0
	self.sickGirl:playAnimation({animation="SIT_TIRED_BLOOD"}) 

	self.waterPot = level:getObject("event_water_pot")
	self.waterPot.onClick = SpecialRoom4.onClickWaterPot
	self.waterPot.event = self
	self.waterPot.clickable = true

	-- Water meter
	local desc = {
		class = "SpriteExt",
		name="water_meter_frame",
		x = watermeterPos.x,
		y = watermeterPos.y,
		floor = 0,
		baseAnimation = "EVENT_SPECIAL_WATERMETER_FRAME",
	}
	self.waterMeterFrame = level:add(desc)

	desc = {
		class = "Object",
		name="water_meter_filler",
		x = watermeterPos.x + 14,
		y = watermeterPos.y + 30,
		floor = 20,
		baseAnimation = "EVENT_SPECIAL_WATERMETER_FILLER",
	}
	self.waterMeterFiller = level:add(desc)

	desc = {
		class = "Object",
		name="water_meter_filler_alert",
		x = watermeterPos.x + 14,
		y = watermeterPos.y + 30,
		floor = 20,
		baseAnimation = "EVENT_SPECIAL_WATERMETER_FILLER_ALERT",
		visible = false,
	}
	self.waterMeterFillerAlert = level:add(desc)
	local s = self.waterMeterFiller
	local anchor = s.anchor
	s.clipRect = { x=s.x - anchor.x, y=s.y - anchor.y + (s.height * (1-self.energy/100)), width=s.width, height=s.height}

	self.waterMeterFrame.visible = false
	self.waterMeterFiller.visible = false
end

function SpecialRoom4:onStart()
	hintManager:handle({id="water_event", sprite=self.sickGirl})
	self.waterMeterFrame.visible = true
	self.waterMeterFiller.visible = true
	self.sickGirl.visible = true
	self.sickGirl.alpha = 1.0


	eventBar.startValue = 0
	eventBar.targetValue = 1
	eventBar.progress = 1

	eventBar.showValue = false
	eventBar.showTarget = false
	eventBar:activate()
end

function SpecialRoom4:onHintClosed(id)
	if (id == 10) then
		hintManager:placeArrow({type="down", name="SpecialRoom4", parent=level.dragLayer, x=self.sickGirl.x, y=self.sickGirl.y-self.sickGirl.height})
		hintManager:placeArrow({type="down", name="SpecialRoom4", parent=level.dragLayer, x=self.waterPot.x + self.waterPot.width/2, y=self.waterPot.y})
	end
end

function SpecialRoom4:onTick(time)
	if (self.completed) then
		return
	end

	if (self.energy > 0) then

		if (self.sickGirl.draining == true) then
			self.energy = self.energy - (time*self.drainSpeed)

			self.waterMeterFrame.visible = true

			if (self.energy < self.alertLevel and not taskSystem:hasTaskFor(self.waterMeterFiller)) then
				local blinkspeed = 100
				self.waterMeterFiller:addTask({class="SetTask", visible=false})
				self.waterMeterFiller:addTask({class="Task", duration=blinkspeed})
				self.waterMeterFiller:addTask({class="SetTask", visible=true})
				self.waterMeterFiller:addTask({class="Task", duration=blinkspeed})
				self.waterMeterFillerAlert:addTask({class="SetTask", visible=true})
				self.waterMeterFillerAlert:addTask({class="Task", duration=blinkspeed})
				self.waterMeterFillerAlert:addTask({class="SetTask", visible=false})
				self.waterMeterFillerAlert:addTask({class="Task", duration=blinkspeed})
			else
				self.waterMeterFiller.visible = true
			end
		else
			self.waterMeterFillerAlert.visible = false
			self.waterMeterFiller.visible = false
			self.waterMeterFrame.visible = false
		end
		local s = self.waterMeterFiller
		local anchor = s.anchor
		s.clipRect = {
			x=s.x - anchor.x,
			y=s.y - anchor.y + (s.height * (1-self.energy/100)),
			width=s.width, height=s.height			
		}
		local a = self.waterMeterFillerAlert
		anchor = a.anchor
		a.clipRect = {
			x=a.x - anchor.x,
			y=a.y - anchor.y + (a.height * (1-self.energy/100)),
			width=a.width, height=a.height
		}
		
		eventBar.progress = self.energy/100
	else
		self.meterReachedZero = true
		self.completed = true
		self.won = false
		self.waterMeterFrame.alpha = 0.5
		self.sickGirl.clickable = false
		self.sickGirl.selected = false
		self.sickGirl:playAnimation("FAINTING", 1, "stay", 90)

		hintManager:removeArrows({id="SpecialRoom4"})
		eventBar.progress = 0
	end
end


function SpecialRoom4:onClickWaterPot()
	--self => waterPot
	if (self.event.sickGirl.selected == true) then
		self.event.sickGirl.clickable = false
		level:cancelSelectedObject()
		self.event:goForWater()
	end
end

function SpecialRoom4:goForWater()
	hintManager:removeArrows({id="SpecialRoom4"})

	self.sickGirl.emotion = ""
	local node = self.waterPot.workPos

	self.energy = math.min(self.energy + self.energyFromWater,100)
	self.sickGirl:addTask({class="SetTask", draining=false})
	self.sickGirl:addTask({class="WalkToTask", target={node.x,node.y}})
	self.sickGirl:addTask({class="PlayAnimationTask", animation="DRINK_LEFT", cycles=1, whenFinished="default", frameTime=250, wait=true})
	self.sickGirl:addTask({class="WalkToTask", target="event_chair"})
	self.sickGirl:addTask({class="PlayAnimationTask", animation="SIT_TIRED_BLOOD", cycles=0, whenFinished="SIT", frameTime=250, wait=true})
	self.sickGirl:addTask({class="SetTask", draining=true})
end
