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

-- ###### Configuration variables ######
QuestionsEvent.actor = nil
QuestionsEvent.numQuestions = 10
QuestionsEvent.scoreOne = 2				-- bonus for answering 1 question
QuestionsEvent.scoreTwo = 2				-- bonus for answering 2 question
QuestionsEvent.scoreThree = 2			-- bonus for answering 3 question
QuestionsEvent.scoreFour = 2			-- bonus for answering 4 question
QuestionsEvent.roomID = -1
QuestionsEvent.alwaysCorrect = false
QuestionsEvent.endTextId = ""
QuestionsEvent.nextQuestionTimer = 20000

function QuestionsEvent:init(desc)
	QuestionsEvent.superInit(desc)
	self.completed = false
	self.started = false
	self.clickToEnd = false
end

function QuestionsEvent:setActor(actor)
	self.actor = actor
	self.actor.residentActive = true
	self.actor.questionTimer = 0
	self.actor.patientPickup = 0
	function self.actor:removeQuestionOverhead()
		self:removeOverhead()
		self.clickable = false
		self.onClick = nil
	end
end

function QuestionsEvent:onInitialized()
	eventBar.startValue = 0
	eventBar.targetValue = self.numQuestions
	eventBar.progress = 0
	eventBar.description = self.eventBarText or "TEXT_EVENT_QUESTIONS"
	self.answers = {}
	self.questionsDone = {}

	self.questionActive = false
	self.actor = self.actor or level.resident
	function self.actor:removeQuestionOverhead()
		self:removeOverhead()
		self.clickable = false
		self.onClick = nil
	end

	if (self.eventBarImage) then
		eventBar.icon = self.eventBarImage
	else
		eventBar.icon = self.baseAnimation.."_BAR:"..self.defaultSection
	end
end

function QuestionsEvent:onShiftStart()
end

function QuestionsEvent:onShiftFinish()
	self.completed = true
end

function QuestionsEvent:onStart()
	self.started = true
	eventBar:activate()
end

function QuestionsEvent:getQuestion()
	return table.count(self.questionsDone) + 1
end

function QuestionsEvent.onActorClick(actor)
	actor:removeQuestionOverhead()
	level.event:startQuestion()
end

function QuestionsEvent:startQuestion()
	local question = self:getQuestion()
	table.insert(self.questionsDone, question)

	-- create a question dialog
	local dialog = scene:createDialog(self.layouts)
	scene:openDialog(dialog)
	dialog:initQuestion(question, self.answers, self.roomID)
	level:incPausedForMinigame()
	level.addLevelTime = false
end


function QuestionsEvent:endQuestionDialog()
	self.actor.residentActive = true
	level:decPausedForMinigame()
	level.addLevelTime = true
	scene:closeDialog("questions_dialog")
	if (self.completed) then
		if (isTable(taskSystem:getAnimationDesc("eventWon"))) then
			return taskSystem:createAnimation("eventWon")
		end
	else
		if (isTable(taskSystem:getAnimationDesc("eventThanks"))) then
			return taskSystem:createAnimation("eventThanks")
		end
	end
end

function QuestionsEvent:endQuestion(answer, dialog)
	table.push_back(self.answers, answer)
	self.completed = table.icount(self.answers) >= 4
	self.won = self.completed
	self.started = not self.completed

	local givePoints = function()
		local x = 500
		local y = 320
		level:addHeartScoreWithFloater(level:calcValueI("questionScore", 1, nil), x, y, false)
		self.clickToEnd = true
	end

	if (self.completed) then
		local a = {}
		for i = 1,4 do
			a[i] = ghf.getString("TEXT_QUESTIONS_ROOM" .. self.roomID .. "_" .. i .. "_END" .. self.answers[i])
		end
	
		dialog:getChild("END").text= string.format(ghf.getString(self.endTextId), a[1], a[2], a[3], a[4])
		dialog:animate(
			"EndQuestion",
			{
				{sprite=dialog:getChild("OPTION1"), alpha=0, duration=800},
				{sprite=dialog:getChild("OPTION2"), alpha=0, duration=800},
				{sprite=dialog:getChild("OPTION3"), alpha=0, duration=800},
				{sprite=dialog:getChild("OPTION4"), alpha=0, duration=800},
				{sprite=dialog:getChild("ANSWER1"), alpha=0, duration=800},
				{sprite=dialog:getChild("ANSWER2"), alpha=0, duration=800},
				{sprite=dialog:getChild("ANSWER3"), alpha=0, duration=800},
				{sprite=dialog:getChild("ANSWER4"), alpha=0, duration=800},
				{sprite=dialog:getChild("QUESTION"), alpha=0, duration=800},
				{sprite=dialog:getChild("ANSWER1_HEADING"), alpha=0, duration=800},
				{sprite=dialog:getChild("QUESTION_HEADING"), alpha=0, duration=800},
			},
			{call=function() self.clickToEnd = true end},
			{			
				{sprite=dialog:getChild("CLICK"), alpha=1, duration=500},
				{sprite=dialog:getChild("END"), alpha=1, duration=500},
				{sprite=dialog:getChild("QUESTION_HEADING_END"), alpha=1, duration=500},
			},
			{call=givePoints}
			--{delay=20000},
			--{call=function() self:endQuestionDialog() end}
		)
	else
		self:endQuestionDialog()
	end
end

function QuestionsEvent:onTick(time)
	if (not self.completed and self.started) then
		if (not self.actor.clickable) then
			if (self.actor.residentActive) then
				if (self.nextQuestionTimer <= 0) then
					if (self.actor.patientPickup == 0) then
						self.actor.clickable = true
						self.actor.residentActive = false
						self.actor.onClick = QuestionsEvent.onActorClick
						self.actor.questionTimer = 5000
						self.actor:createOverhead("ICON_QUESTION:icons")
						self.nextQuestionTimer = 10000
					end
					return
				end
				self.nextQuestionTimer = self.nextQuestionTimer - time
			end
		else
			self.actor.questionTimer = self.actor.questionTimer - time
			if (self.actor.questionTimer <= 0) then
				self.actor.residentActive = true
				self.actor:removeQuestionOverhead()
			end
		end
	end
end

--function QuestionsEvent:handleTrigger(trigger, meta)
	--if (trigger == "next question") then
		--return true
	--end
	--return QuestionsEvent.super.handleTrigger(self, trigger, meta)
--end

QuestionsEvent.layouts =
{class="QuestionsDialog", name="questions_dialog", movable=false, x=160, y=60, background="EVENT_QUESTIONS_BG", section="events",
	components = {

		{class="Label", name="QUESTION_HEADING", x=130, y=197, rotation=-11.5, font="EVENT_QUESTIONS_HEADING:events", roundPos=false},
		{class="Label", name="QUESTION_HEADING_END", alpha=0, x=142, y=242, rotation=-11.5, font="EVENT_QUESTIONS_HEADING:events", roundPos=false, textId="TEXT_QUESTIONS_HEADING_END"},

		{class="Label", name="QUESTION", x=139, y=234, rotation=-11.5, font="EVENT_QUESTIONS_TEXT:events", maxWidth=370, color0=0x000000, wrapStyle="wordwrap", roundPos=false},

		{class="Label", name="ANSWER1_HEADING", x=162, y=337, rotation=-11.5, font="EVENT_QUESTIONS_HEADING:events", roundPos=false, textId="TEXT_ANSWER_HEADING"},
		{class="Label", name="ANSWER1", x=201, y=373, rotation=-11.5, font="EVENT_QUESTIONS_TEXT:events", maxWidth=370, color0=0x000000, wrapStyle="wordwrap", roundPos=false},
		{class="Label", name="ANSWER2", x=212, y=414, rotation=-11.5, font="EVENT_QUESTIONS_TEXT:events", maxWidth=340, color0=0x000000, wrapStyle="wordwrap", roundPos=false},
		{class="Label", name="ANSWER3", x=223, y=457, rotation=-11.5, font="EVENT_QUESTIONS_TEXT:events", maxWidth=370, color0=0x000000, wrapStyle="wordwrap", roundPos=false},
		{class="Label", name="ANSWER4", x=234, y=497, rotation=-11.5, font="EVENT_QUESTIONS_TEXT:events", maxWidth=370, color0=0x000000, wrapStyle="wordwrap", roundPos=false},
		{class="Switch", name="OPTION1", x=182, y=374, anchor="top", image="EVENT_QUESTIONS_CHECKBOX:events", command="okClick", tag=1},
		{class="Switch", name="OPTION2", x=193, y=415, anchor="top", image="EVENT_QUESTIONS_CHECKBOX:events", command="okClick", tag=2},
		{class="Switch", name="OPTION3", x=203, y=458, anchor="top", image="EVENT_QUESTIONS_CHECKBOX:events", command="okClick", tag=3},
		{class="Switch", name="OPTION4", x=214, y=498, anchor="top", image="EVENT_QUESTIONS_CHECKBOX:events", command="okClick", tag=4},

		{class="Label", name="BOTTOM_HEADING", x=34, y=544, font="FONT_AGE:default", roundPos=false, textId="TEXT_QUESTIONS_BAR"},
		{class="Sprite", name="ACTIVE_BG",x=209, y=532, image="EVENT_QUESTIONS_ACTIVE_BG:events"},
		{class="Sprite", name="BOX1", x=232, y=541, image="EVENT_QUESTIONS_BOX1:events"},
		{class="Sprite", name="BOX2", x=329, y=541, image="EVENT_QUESTIONS_BOX2:events"},
		{class="Sprite", name="BOX3", x=426, y=541, image="EVENT_QUESTIONS_BOX3:events"},
		{class="Sprite", name="BOX4", x=522, y=541, image="EVENT_QUESTIONS_BOX4:events"},
		{class="Sprite", name="CHECKMARK1", x=254, y=545, image="EVENT_QUESTIONS_CHECKMARKS:events", visible=false},
		{class="Sprite", name="CHECKMARK2", x=352, y=545, image="EVENT_QUESTIONS_CHECKMARKS:events", visible=false},
		{class="Sprite", name="CHECKMARK3", x=449, y=545, image="EVENT_QUESTIONS_CHECKMARKS:events", visible=false},
		{class="Sprite", name="CHECKMARK4", x=545, y=545, image="EVENT_QUESTIONS_CHECKMARKS:events", visible=false},

		{class="Label", name="END", alpha=0, x=181, y=377, rotation=-11.5, font="EVENT_QUESTIONS_TEXT:events", maxWidth=370, color0=0x000000, wrapStyle="wordwrap", roundPos=false},
		{class="Label", name="CLICK", alpha=0, x=380, y=510, anchor="center", font="EVENT_QUESTIONS_TEXT:events", maxWidth=370, color0=0x000000, roundPos=false, textId="CTEXT_CLICK_TO_CONTINUE", },

		{class="Sprite", name="SARAH", x=576, y=433, image="EVENT_QUESTIONS_SARAH:events"},
	},
}

-- QuestionsDialog --
QuestionsDialog = deriveLuaClass("QuestionsDialog", "DialogCare")

function QuestionsDialog:init(desc)
	QuestionsDialog.superInit(desc)
	self:getChild("OPTION1").extraLabel = self:getChild("ANSWER1")
	self:getChild("OPTION2").extraLabel = self:getChild("ANSWER2")
	self:getChild("OPTION3").extraLabel = self:getChild("ANSWER3")
	self:getChild("OPTION4").extraLabel = self:getChild("ANSWER4")
	self:getChild("SARAH"):animate({startFrame=0, frameTime=300, loop="infinite"})
end

function QuestionsDialog:handleCommand(command, clickObject)
	if (command == "okClick") then
		self:getChild("OPTION1").enabled = false
		self:getChild("OPTION2").enabled = false
		self:getChild("OPTION3").enabled = false
		self:getChild("OPTION4").enabled = false
		self:animateAnswers(clickObject.tag)
		clickObject.enabled = false
	end
end

function QuestionsDialog:animateAnswers(clickedQuestion)
	local clickedLabel = self:getChild("ANSWER"..clickedQuestion)
	local clickedOption = self:getChild("OPTION"..clickedQuestion)

	local atEnd = function()
		level.event:endQuestion(clickedQuestion, self)
	end

	self:getChild("CHECKMARK"..self.questionId).visible = true

	if (level.event.alwaysCorrect == true) then
		self.correctAnswer = clickedQuestion
	end

	if (clickedQuestion == self.correctAnswer) then
		clickedLabel:animate({
			{delay=500},
			{layerColor="green", layer=0, duration=300},
			{layerColor="black", layer=0, duration=300},
			{layerColor="green", layer=0, duration=300},
			{delay=2000},
			{call=atEnd},
		})
		self:getChild("CHECKMARK"..self.questionId).frame = 1

		eventBar:incValue(1)
	else
		clickedLabel:animate({
			{delay=500},
			{layerColor="red", layer=0, duration=300},
			{layerColor="black", layer=0, duration=300},
			{layerColor="red", layer=0, duration=300},
			{delay=500},
			{client=self:getChild("ANSWER"..self.correctAnswer), layerColor="green", layer=0, duration=300},
			{client=self:getChild("ANSWER"..self.correctAnswer), layerColor="black", layer=0, duration=300},
			{client=self:getChild("ANSWER"..self.correctAnswer), layerColor="green", layer=0, duration=300},
			{delay=500},
			{call=atEnd},
		})
		self:getChild("CHECKMARK"..self.questionId).frame = 0
	end
end

function QuestionsDialog:initQuestion(questionId, prevAnswers, roomID)
	level.miniGameLayer:addChild(self)
	--local questionId = "TEXT_QUESTIONS_ROOM" .. self.roomID .. "_" .. questionId .. "_QUESTION"


	self:getChild("ACTIVE_BG").x = self:getChild("ACTIVE_BG").x + (table.icount(prevAnswers) * 96)
	for i=1, table.icount(prevAnswers)+1 do
		self:getChild("BOX" .. i).frame = 1
	end

	self.correctAnswers = 0
	for i=1, table.icount(prevAnswers) do
		self:getChild("CHECKMARK" .. i).visible = true
		if (prevAnswers[i] ~= nil) then
			self.correctAnswers = self.correctAnswers + 1
			self:getChild("CHECKMARK" .. i).frame = 1
		end
	end

	self.questionId = table.icount(prevAnswers)+1

	self:getChild("QUESTION_HEADING").text = string.format(ghf.getString("TEXT_QUESTION_HEADING"), self.questionId)

	local roomId = level.currentRoom.id + 1
	self:getChild("QUESTION").textId = "TEXT_QUESTIONS_ROOM" .. roomID .. "_" .. questionId .. "_QUESTION"

	self.correctAnswer = 1
	for i=1, 4 do
		self:getChild("ANSWER" .. i).textId = "TEXT_QUESTIONS_ROOM" .. roomID .. "_" .. questionId .. "_ANSWER" .. i
	end
end

function QuestionsDialog:onMouseDown(x, y)
	if (level.event.clickToEnd) then
		ghf.completeAnimation("EndQuestion")
		level.event:endQuestionDialog()
		level.event.clickToEnd = false
	end
end


