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

-- ###### Configuration variables ######
QuestionPopupEvent.actor = nil
QuestionPopupEvent.numQuestions = 10
QuestionPopupEvent.eventBarImage = ""
QuestionPopupEvent.scoreOne = 2				-- bonus for answering 1 question
QuestionPopupEvent.scoreTwo = 2				-- bonus for answering 2 question
QuestionPopupEvent.scoreThree = 2			-- bonus for answering 3 question
QuestionPopupEvent.scoreFour = 2			-- bonus for answering 4 question
QuestionPopupEvent.askingQuestion = false
QuestionPopupEvent.questionsCounter = 0
QuestionPopupEvent.questionScore = 2

function QuestionPopupEvent:init(desc)
	QuestionPopupEvent.superInit(desc)
	self.completed = false
	self.started = false
	self.actorPos = {x=130,y=440}
end

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

	self.nextQuestionTimer = 20000
	self.questionActive = false
	self.actor = self.actor or level.resident
	function self.actor:removeQuestionOverhead()
		self:removeOverhead()
		level.event.askingQuestion = false
		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 QuestionPopupEvent:onStart()
	self.started = true
	eventBar:activate()
end

function QuestionPopupEvent:getQuestion()
	assert(table.count(self.questionsDone) < self.numQuestions)
	local val = math.Random(self.numQuestions)
	while (table.contains(self.questionsDone, val)) do
		val = math.Random(self.numQuestions)
	end
	return val
end

function QuestionPopupEvent.onActorClick(actor)
	level.event.nextQuestionTimer = 20000
	actor:removeQuestionOverhead()
	level.event:startQuestion()
end

function QuestionPopupEvent:startQuestion()
	level:incPaused()

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

	local questionId = "TEXT_QUESTIONS_ROOM3_" .. question .. "_QUESTION"

	local answerIds = {}
	local answers = {1, 2, 3}
	for i=1, 3 do
		local idx = math.Random(table.icount(answers))
		table.push_back(answerIds, "TEXT_QUESTIONS_ROOM3_" .. question .. "_ANSWER" .. answers[idx])
		table.remove_at(answers, idx)
	end

	desc = {
		tipSide = "left",
		font = "EVENT_QUESTIONS_TEXT:events",
		textId = questionId,
		dialogId = "QUESTION_POPUP",
		content = { children = {} },
		labelWidth = 640,
		anchor = "topleft",
		delayBeforeHide = 2000,
	}

	local curY = 2
	for i=1,3 do
		table.insert(desc.content.children, {
			class="Switch",
			name="a"..i,
			image="EVENT_QUESTIONS_CHECKBOX_CLOUD:events",
			x=0, y=curY,
			anchor="topleft",
		})

		table.insert(desc.content.children, {
			class="Label",
			name="label_a"..i,
			textId=answerIds[i],
			wrapStyle="wordwrap",
			font="FONT_SHOP_BUY_DIALOG_INFO_TEXT:default",
			x=40, y=curY + 12,
			maxWidth=desc.labelWidth-40,
			anchor="left",
		})

		curY = curY + 50
	end
	
	desc.onDebugSkip = function(self)
		self:getChild("a1"):onClick()
	end

	local dlg = self.actor:showMessageDialog(desc)
	local x = 20 - dlg.minWidth/2
	dlg.contents.x = x
	dlg:getChild("message").x = x
	return dlg
end

function QuestionPopupEvent:eventCompleted()			
	if (self.won) then
		if (isTable(taskSystem:getAnimationDesc("eventWon"))) then
			return taskSystem:createAnimation("eventWon")		
		end		
	elseif (isTable(taskSystem:getAnimationDesc("eventLost"))) then
			return self:playIfNotPlayedBefore("eventLost")
	end
	eventBar:deactivate()
end

function Event:onMessageClick(command, id, object)
	self.actor.residentActive = true
	self.askingQuestion = false

	eventBar:incValue(1)

	table.push_back(self.answers, id)
	
	self.questionsCounter = self.questionsCounter + 1
	self.completed = self.questionsCounter >= self.numQuestions
	self.won = table.icount(self.answers) >= self.numQuestions
	self.started = not self.completed

	level:heartsToBar(self.questionScore, object.screenX, object.screenY - object.height, false)

	if (self.completed) then
		self:eventCompleted()			
	else
		if (isTable(taskSystem:getAnimationDesc("eventThanks"))) then
			return taskSystem:createAnimation("eventThanks")
		end
	end
	
	level:decPaused()
end

function QuestionPopupEvent:readyQuestion()
	self.actor.clickable = true
	self.actor.residentActive = false	
	self.actor.onClick = QuestionPopupEvent.onActorClick
	self.actor.questionTimer = 5000
	self.actor:createOverhead("ICON_QUESTION:icons")
end

function QuestionPopupEvent: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 and not self.askingQuestion) then
						self.askingQuestion = true
						self.actor:addTask({class="CallTask", func=QuestionPopupEvent.readyQuestion, object=self, actor=self.actor})
					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.askingQuestion = false
				self.nextQuestionTimer = 20000
				
				self.questionsCounter = self.questionsCounter + 1
				self.completed = self.questionsCounter >= self.numQuestions
				self.started = not self.completed
				
				self.actor:removeQuestionOverhead()
                
   				if (self.completed) then
					self:eventCompleted()
				end
			end
		end
	end
end

function QuestionPopupEvent: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
