dofile("script/layouts/room1_defaults.lua")
dofile("script/shifts/shift1_1_animations.lua")

levelDesc.name = "Shift 1.1"
levelDesc.levelId = 11
levelDesc.music = "SELECTION"
levelDesc.musicMood = ""
levelDesc.autoStartShift = false
levelDesc.trigger =
{
	class = "TriggerSystem",
	triggers =
	{
		{ trigger = "spawn", delay = 1000, hearts=3, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "FIND_SORE_SPOT"} } },
		{ trigger = "spawn", delay = 22000, hearts=2, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "PUT_ON_BANDAID"} } },
		{ trigger = "spawn", delay = 20000, hearts=1, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "FIND_SORE_SPOT", "PUT_ON_BANDAID"} } },
		{ trigger = "spawn", delay = 25000, hearts=2, diseaseDesc = { class="Disease", steps={"STETHOSCOPE", "CHECK_UP"} } },
		{ trigger = "spawn", delay = 15000, hearts=4, diseaseDesc = { class="Disease", steps={"FIND_SORE_SPOT"} } },
		{ trigger = "spawn", delay = 15000, hearts=2, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "PUT_ON_BANDAID",} } },
		{ trigger = "spawn", delay = 12000, hearts=2, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "STETHOSCOPE"} } },
		{ trigger = "spawn", delay = 15000, hearts=2, diseaseDesc = { class="Disease", steps={"FIND_SORE_SPOT", "PUT_ON_BANDAID"} } },
	}
}
levelDesc.patientList =
{
	p0 = { weight = 50,
		base = "axel",
	},
	p1 = { weight = 50,
		base = "lady",
	},
	p2 = { weight = 50,
		base = "mother",
	},
	p3 = { weight = 50,
		base = "local_male",
	},
}

levelDesc.tutorialStations = {
	"changestation_1",
	"changestation_2",
	"bathrobe",
	--"watercooler",
	"researchtable_1",
	--"researchtable_2",
	"workstation_1",
	--"workstation_2",
	"queueOut",
}

function levelDesc:prepareStationsForTutorial()
	for _,v in pairs(self.tutorialStations) do
		local station = level:getObject(v)
		station.old_onClick = station.onClick
	end
end

function levelDesc:setupStationsForTutorial(animName)
	self.shouldDisplayTutorialMessage = true
	local f = function()
		if (self.shouldDisplayTutorialMessage) then
			self.shouldDisplayTutorialMessage = false
			taskSystem:createAnimation(animName)
		end
	end
	for _,v in pairs(self.tutorialStations) do
		level:getObject(v).onClick = f
	end
end

function levelDesc:disableStationsForTutorial()
	local e = function() end
	for _,v in pairs(self.tutorialStations) do
		level:getObject(v).onClick = e
	end
end

function levelDesc:restoreStationsForTutorial()
	for _,v in pairs(self.tutorialStations) do
		local station = level:getObject(v)
		station.onClick = station.old_onClick
	end
end

function levelDesc:onMessageClick(command, id, object)
	if (id == "DO_TUTORIAL") then
		if (command == "yes") then
			self:disableStationsForTutorial()
			taskSystem:createAnimation("startTutorial")
		elseif (command == "no") then
			taskSystem:createAnimation("startShift")
		end
	end
end

function levelDesc:handleTrigger(trigger, meta)
	CareLevel.handleTrigger(self, trigger, meta)

	if (trigger == "click to change sarah") then
		self:prepareStationsForTutorial()
		self:disableStationsForTutorial()
		self:setSpecialState()
		local changestation2 = self:getObject("changestation_2")
		changestation2.onClick = function(station)
			changestation2.onClick = function() end
			taskSystem:createAnimation("sarahChangesIntoWorkClothes")
		end

	elseif (trigger == "start shift") then
		self:restoreStationsForTutorial()
		self.miniGameLevels = {} -- Reset minigame levels
		self:shiftStart()
		--print(trigger)

	elseif (trigger == "tutorial: click patient in chair") then
		self:disableStationsForTutorial()
		self.tut_pat = self:getCharacter("tut_pat")
		self.tut_pat.onClick = function()
			self.tut_pat.onClick = function() end
			taskSystem:createAnimation("clickedPatientInChair")
		end

	elseif (trigger == "tutorial: click changestation") then
		self:setupStationsForTutorial("clickedWrongStation_ChangeStation")
		self.tut_pat:resetQueueSpot()

		local changestation1 = self:getObject("changestation_1")
		local changestation2 = self:getObject("changestation_2")

		changestation1.onClick = function(station)
			self.tutorialChosenChangeStation = station
			self:disableStationsForTutorial()
			level:getObject("bathrobe"):decStock()
			taskSystem:createAnimation(
				"clickedChangeStation",
				{STATION_X=self.tutorialChosenChangeStation})
		end
		changestation2.onClick = changestation1.onClick

		local old_onWorkFinish1 = changestation1.onWorkFinish
		local old_onWorkFinish2 = changestation2.onWorkFinish
		changestation1.onWorkFinish = function(station, task)
			changestation1.onWorkFinish = old_onWorkFinish1
			changestation2.onWorkFinish = old_onWorkFinish2
			station.onWorkFinish(station, task)
			taskSystem:createAnimation("exitedChangeStation")
		end
		changestation2.onWorkFinish = changestation1.onWorkFinish

	elseif (trigger == "tutorial: click patient at changestation") then
        self:disableStationsForTutorial()
		self.tut_pat.onClick = function()
			self.tut_pat.onClick = function() end
			self.tut_pat.onTick = nil
			taskSystem:createAnimation("clickedPatientAtChangeStation")
		end

		self.reactionTime = 3000
		self.tut_pat.onTick = function(_, time)
			self.tut_pat:tick(time)
			if (self.reactionTime > 0) then
				self.reactionTime = self.reactionTime - time
				if (self.reactionTime <= 0) then
					self.tut_pat.onTick = nil
					taskSystem:createAnimation("changeStationTooLong")
				end
			end
		end

	elseif (trigger == "tutorial: click workstation") then
		self:setupStationsForTutorial("clickedWrongStation")
		self.workstation = self:getObject("researchtable_1")
		self.workstation.onClick = function()
			self:disableStationsForTutorial()
			self.workstation.onClick = function() end
			taskSystem:createAnimation(
				"clickedWorkStation",
				{STATION_X=self.tutorialChosenChangeStation})
		end

	elseif (trigger == "tutorial: click patient at workstation") then
		self:disableStationsForTutorial()
 		self.tut_pat.onClick = function()
			self.tut_pat.onClick = function() end
			taskSystem:createAnimation("clickedPatientInWorkStation")
		end

	elseif (trigger == "tutorial: click on patient for desk") then
		self:disableStationsForTutorial()
 		self.tut_pat.onClick = function()
			self.tut_pat.onClick = function() end
			taskSystem:createAnimation("clickedPatientAtResearchTable")
		end

	elseif (trigger == "tutorial: click desk") then
		self:setupStationsForTutorial("clickedWrongStation")
		self.desk = self:getObject("queueOut")
 		self.desk.onClick = function()
			self:disableStationsForTutorial()
			self.desk.onClick = function() end
			taskSystem:createAnimation("clickedPatientToDesk")
		end

	elseif (trigger == "tutorial: click desk again") then
		self:setupStationsForTutorial("didntClickDesk")
 		self.desk.onClick = function()
			self:disableStationsForTutorial()
			self.desk.onClick = function() end
			taskSystem:createAnimation("clickedSarahToDesk")
		end

	elseif (trigger == "tutorial: finish") then
		taskSystem:createAnimation("startShift")

	end
end
