require 'scripts/gui/gui_popup.lua'
--------------------------------------------------------------------------------
-- VCPopup Object script
--------------------------------------------------------------------------------

POPUP_STATE_IDLE = 0
POPUP_STATE_ANIM_OPEN = 1
POPUP_STATE_ANIM_CLOSE = 2
POPUP_STATE_END = 3

VCPopup = class(GUIPopup)


-- Callback when object is added to the world
function VCPopup:start()
	print('VCPopup:start()')
	
	GUIPopup.start(self)
	
	if self.anim_open then
		self.state = POPUP_STATE_ANIM_OPEN
	
		self.current_animation = self.anim_open
		Component_enable(self.current_animation)
		AnimatorComponent_play(self.current_animation)
		self:disable()
	else
		self.state = POPUP_STATE_IDLE
		
		self.enable()
	end

	if not self.anim_close_cancel then
		self.anim_close_cancel = self.anim_close
	end

	self.isKeyPressed = false

end

function VCPopup:close(userChoice)
	
	GUI:debugPrint("User closed with " .. tostring(userChoice))
	
	self.userChoice = userChoice
	
	Component_disable(self.anim_open)
	
	local anim_close = nil
	if userChoice == "ok" or userChoice == "yes" then
		if self.isSaveDialog then
			VC_saveStarted()
		end
		
		anim_close = self.anim_close
		if not self.isKeyPressed  then
			if GUI.focusedElement ~= nil then
				VC_playSoundId(SOUND_KEY_DECIDE_SUB)
			else
		 		VC_playSoundId(SOUND_DECIDE_SUB)
		 	end
		else 
			self.isKeyPressed = false
		end
	else
		anim_close = self.anim_close_cancel
		if not self.isKeyPressed then
			if GUI.focusedElement ~= nil then
				VC_playSoundId(SOUND_KEY_CANCEL)
			else
		 		VC_playSoundId(SOUND_CANCEL)
		 	end
		else 
			self.isKeyPressed = false
		end
	end
	GUI:focusElement(nil)
	Component_enable(anim_close)
	AnimatorComponent_play(anim_close)
	self:disable()

	self.current_animation = anim_close
	
	self.state = POPUP_STATE_ANIM_CLOSE
end

function VCPopup:onElementFocus(element)
	VC_playSoundId(SOUND_SELECT)
end

-- Callback called every frames
function VCPopup:update(dt)
	if self.state == POPUP_STATE_ANIM_OPEN then
		if AnimatorComponent_isStopped(self.current_animation) then
			self.current_animation = nil
			self:enable()
			self.state = POPUP_STATE_IDLE
			-- GUIPopup.open(self)
		end
	elseif self.state == POPUP_STATE_ANIM_CLOSE then
		if AnimatorComponent_isStopped(self.current_animation) then
			GUIPopup.close(self, self.userChoice)
			
			if not self.isSaveDialog or self.userChoice == "cancel" then
				WorldNode_destroy(self.worldNode)
				self.current_animation = nil
			else
				self.current_animation = nil
				self.state = POPUP_STATE_END
			end
		end
	elseif self.state == POPUP_STATE_END then
		if self.closeSaveDialog then
			WorldNode_setEnabled(self.worldNode, false)
			WorldNode_destroy(self.worldNode)
			VC_saveFinished()
		end
	elseif self.state == POPUP_STATE_IDLE and not self.disabled and GUI.pressedCommandTarget == nil then
		if GUI:isLeftPressed(g_menu.invokerDevice) or GUI:isUpPressed(g_menu.invokerDevice) or GUI:isDownPressed(g_menu.invokerDevice) then
			if self.cancel_button then
				GUI:focusElement(self.cancel_button)
			elseif self.no_button then
				GUI:focusElement(self.no_button)
			end
		elseif GUI:isRightPressed(g_menu.invokerDevice) then
			if self.cancel_button or self.no_button then
				if self.ok_button then
					GUI:focusElement(self.ok_button)
				elseif self.yes_button then
					GUI:focusElement(self.yes_button)
				end
			end
		elseif GUI:isCancelPressed(g_menu.invokerDevice) then
			if GUI.focusedElement ~= nil and (GUI.focusedElement == self.ok_button or GUI.focusedElement == self.yes_button) then
				if (self.ok_button ~= nil and (self.ok_button == self.no_button or self.ok_button == self.cancel_button)) or (self.yes_button ~= nil and (self.yes_button == self.cancel_button or self.yes_button == self.no_button)) then
					Input_freeze()
 					GUI:focusElement(nil)
 					self.isKeyPressed = true
					if self.ok_button then
						self.ok_button:playClickedAnimation()
					elseif self.yes_button then
						self.yes_button:playClickedAnimation()
					end
					VC_playSoundId(SOUND_KEY_DECIDE_SUB)
					self:close("ok")
				else
 					if self.cancel_button then
						GUI:focusElement(self.cancel_button)
					elseif self.no_button then
	 					GUI:focusElement(self.no_button)
	 				end	
				end
			else
				Input_freeze()
				GUI:focusElement(nil)
 				self.isKeyPressed = true
 				if self.cancel_button then
 					self.cancel_button:playClickedAnimation()
 				elseif self.no_button then
 					self.no_button:playClickedAnimation()
				end
				if (self.ok_button ~= nil and (self.ok_button == self.no_button or self.ok_button == self.cancel_button)) or (self.yes_button ~= nil and (self.yes_button == self.cancel_button or self.yes_button == self.no_button)) then
					VC_playSoundId(SOUND_KEY_DECIDE_SUB)
					self:close("ok")
				else
					VC_playSoundId(SOUND_KEY_CANCEL)
					self:close("cancel")
                end
			end
		elseif GUI.focusedElement == nil and GUI:isValidatePressed(g_menu.invokerDevice) then
			Input_freeze()
			GUI:focusElement(nil)
			self.isKeyPressed = true
			if self.ok_button then
				self.ok_button:playClickedAnimation()
			elseif self.yes_button then
				self.yes_button:playClickedAnimation()
			end
			VC_playSoundId(SOUND_KEY_DECIDE_SUB)
			self:close("ok")
		end
	end
	
end

function VCPopup:onButtonPress(button)
	VC_playSoundId(SOUND_SELECT)
end

-- function VCPopup:onAnimationEvent(eventId, eventTime, triggerTime)
	
-- 	if eventId == "middle" then
		
-- 		LabelComponent_setTextFromStringId(self.message, "VCM_DIALOG_SAVE_FINISH")
		
-- 	end
-- end