-- Popups ids
POPUP_ID_NONE = 0
POPUP_ID_CREATE_POINT = 1
POPUP_ID_CREATING_POINT = 2
POPUP_ID_CREATE_POINT_FINISH = 3
POPUP_ID_CREATE_POINT_SUCCESS = 4
POPUP_ID_RESET = 5
POPUP_ID_LOAD_POINT = 6
POPUP_ID_LOAD_POINT_SUCCESS = 7

--------------------------------------------------------------------------------
-- MainPage Object script
--------------------------------------------------------------------------------

-- constants
MAINPAGE_STATE_IDLE = 0
MAINPAGE_STATE_ANIM = 1
MAINPAGE_STATE_FADEOUT_TO_MANUAL = 2
MAINPAGE_STATE_START_FADEIN_FROM_MANUAL = 3
MAINPAGE_STATE_FADEIN_FROM_MANUAL = 4
MAINPAGE_STATE_DRC_CAMERA_UP = 5
MAINPAGE_STATE_DRC_BUTTON_FLASHING = 6
MAINPAGE_STATE_STANDBY = 7
MAINPAGE_STATE_STARTUP = 8
MAINPAGE_STATE_SAVE_FINISHED = 9

MainPage = class()

-- Callback when object is added to the world
function MainPage:start()
	print('MainPage:start()')

	self.controller_icon_nodes = {self.drc_icon_node, self.p1_icon_node, self.p2_icon_node, self.p3_icon_node, self.p4_icon_node}
	self.drc_led_label_texts = {"SYS_DRCPLAYER_SMALL_1P", "SYS_DRCPLAYER_SMALL_2P", "SYS_DRCPLAYER_SMALL_3P", "SYS_DRCPLAYER_SMALL_4P"}
	self.player_guide_label_texts = {"SYS_PLAYER_GUIDE_1P", "SYS_PLAYER_GUIDE_2P", "SYS_PLAYER_GUIDE_3P", "SYS_PLAYER_GUIDE_4P",}
	self.anims_show_icon = {self.anim_show_drc_icon, self.anim_show_p1_icon, self.anim_show_p2_icon, self.anim_show_p3_icon, self.anim_show_p4_icon}
	self.anims_hide_icon = {self.anim_hide_drc_icon, self.anim_hide_p1_icon, self.anim_hide_p2_icon, self.anim_hide_p3_icon, self.anim_hide_p4_icon}

	self.create_button = ScriptComponent_getScriptTable(self.create_button)
	self.load_button = ScriptComponent_getScriptTable(self.load_button)
	self.reset_button = ScriptComponent_getScriptTable(self.reset_button)
	self.settings_button = ScriptComponent_getScriptTable(self.settings_button)
	self.manual_button = ScriptComponent_getScriptTable(self.manual_button)
	self.back_button = ScriptComponent_getScriptTable(self.back_button)
	self.drc_top_button = ScriptComponent_getScriptTable(self.drc_top_button)
	self.drc_bottom_button = ScriptComponent_getScriptTable(self.drc_bottom_button)
	
	self.p1_unselected_button = ScriptComponent_getScriptTable(self.p1_unselected_button)
	self.p2_unselected_button = ScriptComponent_getScriptTable(self.p2_unselected_button)
	self.p3_unselected_button = ScriptComponent_getScriptTable(self.p3_unselected_button)
	self.p4_unselected_button = ScriptComponent_getScriptTable(self.p4_unselected_button)

	self.state = MAINPAGE_STATE_STANDBY
	self.is_active = false

end

-- Callback called every frames
function MainPage:update(dt)
	if not self.is_active then
		return
	end

	if self.state == MAINPAGE_STATE_STANDBY then
		if not self.balloon_showed then
			self.balloon_timer = 0
			self.balloon_showed = false
			self.balloon_hidden = true
		end
		self.state = MAINPAGE_STATE_STARTUP
		self.current_dialog = nil
	end
	
	if self.state == MAINPAGE_STATE_STARTUP then
		print("MAINPAGE STARTUP")
		self.controllers = copyTab(connectedControllers)
		self.icons_currentPos = self:layoutIcons()
		self.icons_targetPos = copyTab(self.icons_currentPos)
		self.icons_currentVel = {0, 0, 0, 0, 0}
		self.controllersChanged = {}
		self.icon_hide_delay = 0
		self.current_dialog = nil
		for i = 1, 5 do
			WorldNode_setLocalPosition(self.controller_icon_nodes[i], self.icons_currentPos[i], -666)
		end
		self:showControllerIcons()
		
		if not self.has_state then 
			self.has_state = VC_hasState()
		end
		
		if not self.has_state then
			print("no restore point")
			self.load_button:setState("disabled")
			WorldNode_setEnabled(self.screenshots_node, false)
		else
			print("restore point")
			self.load_button:setState("idle")
			WorldNode_setEnabled(self.screenshots_node, true)
		end
		-- if DRC is invoker, show the DRC indicator pannel
		if self.controllers[1].invoker then
			WorldNode_setEnabled(self.drc_indicator_node, true)
			self.drc_unselected_buttons = { self.p1_unselected_button, self.p2_unselected_button, self.p3_unselected_button, self.p4_unselected_button }
			self.drc_button_label_nodes = {self.p1_button_label_node, self.p2_button_label_node, self.p3_button_label_node, self.p4_button_label_node}
			self.drc_led_pos_y = -334.0
			self.drc_led_pos_x_table = { 353.0, 405.0, 457.0, 509.0 }
			self.drc_button_flash_pos_y = -68
			self.drc_button_flash_pos_x_table = { -385, -128, 129, 388}
			self.drc_bottom_button:setState("idle")
			self.drc_top_button:setState("disabled")
			for i = 1, 4 do
				if ( i <= numberOfPlayers ) then
					WorldNode_setEnabled(self.drc_button_label_nodes[i], true)
					self.drc_unselected_buttons[i]:setState("idle")
					if i == self.controllers[1].playerId then
						WorldNode_setLocalPosition(self.drc_led_node, self.drc_led_pos_x_table[i], self.drc_led_pos_y)
						LabelComponent_setText(self.drc_led_label, Localization_getText(self.drc_led_label_texts[i]))
						self.drc_unselected_buttons[i]:setChecked(true)
					else
						self.drc_unselected_buttons[i]:setChecked(false)
					end
				else
					WorldNode_setEnabled(self.drc_button_label_nodes[i], false)
					self.drc_unselected_buttons[i]:setState("disabled")
				end
			end
		else
			WorldNode_setEnabled(self.drc_indicator_node, false)
		end
		GUI:focusElement(nil)
		self.state = MAINPAGE_STATE_IDLE
		self.animation_controller = nil
	end

	if self.state == MAINPAGE_STATE_IDLE then

		if self.balloon_timer > 0 then
			self.balloon_timer = self.balloon_timer - dt
		end
		
		if not self.balloon_hidden and self.balloon_timer < 0 then
			self:hideBalloon()
			self.balloon_timer = 0
		end
		
		if self.balloon_showed == false then
			self:showBalloon()
			self.balloon_timer = 7.0
		end
	
		
		self.controllersChanged  = self:controllersStatusChanged()
		self.icons_targetPos = self:layoutIcons()
	
		for i = 1, 5 do
			if self.controllersChanged[i] == -1 then
				self:hideControllerIcon(i)
				self.icon_hide_delay = 0.2
			elseif self.controllersChanged[i] == 1 then
				self:showControllerIcon(i)
				self.icons_currentPos[i] = self.icons_targetPos[i]
				self.delay = 0
			elseif self.controllersChanged[i] == 2 then
				self:switchControllerIcon(i)
			else
			end
		end
	
		if self.icon_hide_delay > 0 then
			self.icon_hide_delay = self.icon_hide_delay - dt
		else
			self:iconsLayoutUpdate(dt)
		end
		
		if self.balloon_showed and not self.balloon_hidden then
			self:balloonPosAdjust()
			if not self.disableInput and (GUI.focusedElement ~= nil or GUI:isCancelPressed(g_menu.invokerDevice) or GUI:isLeftZPressed(g_menu.invokerDevice) or ((g_menu.invokerDevice == PAD_DEVICE) and GUI:isTouched())) then
				self:hideBalloon()
			end
		end
		if not self.disableInput then
			if self.current_dialog == nil and GUI.pressedCommandTarget == nil then
				if GUI:isLeftPressed(g_menu.invokerDevice) then
					if GUI.focusedElement ~= self.create_button then
						GUI:focusElement(self.create_button)
						VC_playSoundId(SOUND_SELECT)
					end
				elseif GUI:isRightPressed(g_menu.invokerDevice) then
					if self.has_state and GUI.focusedElement ~= self.load_button then
						GUI:focusElement(self.load_button)
						VC_playSoundId(SOUND_SELECT)
					end
				elseif GUI:isUpPressed(g_menu.invokerDevice) then
					if GUI.focusedElement ~= self.reset_button then
						GUI:focusElement(self.reset_button)
						VC_playSoundId(SOUND_SELECT)
					end
				elseif GUI:isDownPressed(g_menu.invokerDevice) then
					if GUI.focusedElement ~= self.settings_button then
						GUI:focusElement(self.settings_button)
						VC_playSoundId(SOUND_SELECT)
					end
				elseif GUI:isLeftZPressed(g_menu.invokerDevice) then
					VC_playSoundId(SOUND_KEY_DECIDE_SUB)
					self.manual_button:setState("pressed")	
					g_menu:fadeOut()
					self.state = MAINPAGE_STATE_FADEOUT_TO_MANUAL
				elseif GUI:isCancelPressed(g_menu.invokerDevice) then
					self.back_button:playClickedAnimation()
					VC_playSoundId(SOUND_KEY_FINISH)
					self.balloon_showed = false
					self.state = MAINPAGE_STATE_STANDBY
					g_menu:quit()
				end
			else
				if self.current_dialog ~= nil and self.current_dialog.popup_id == POPUP_ID_CREATING_POINT then
					if VC_isSaveFinished() == 1 then
						self.state = MAINPAGE_STATE_SAVE_FINISHED
					end
				end
			end
		end
	
	elseif self.state == MAINPAGE_STATE_FADEOUT_TO_MANUAL then
		if g_menu:fadeCompleted() then
			VC_openPDF()
			self.state = MAINPAGE_STATE_START_FADEIN_FROM_MANUAL
		end
	elseif self.state == MAINPAGE_STATE_START_FADEIN_FROM_MANUAL then
		self.state = MAINPAGE_STATE_FADEIN_FROM_MANUAL
		g_menu:fadeIn()
		self.manual_button:setState("idle")
	elseif self.state == MAINPAGE_STATE_FADEIN_FROM_MANUAL then
		if g_menu:fadeCompleted() then
			self.state = MAINPAGE_STATE_IDLE
			g_menu:stopFade()
			self:enableClickables(true)
		end
	elseif self.state == MAINPAGE_STATE_DRC_CAMERA_UP then
		if self.current_dialog == nil then
			if GUI:isCancelPressed(g_menu.invokerDevice) then
				self.drc_top_button:setState("disabled")
				self.drc_bottom_button:setState("disabled")
				VC_playSoundId(SOUND_CANCEL)
				self:launchAnimation(self.anim_camera_drc_down, MAINPAGE_STATE_ANIM)
			end
		end
	elseif self.state == MAINPAGE_STATE_SAVE_FINISHED then
		self.current_dialog.closeSaveDialog = true
		VC_playSoundId(SOUND_DECIDE_SUB)
		self.current_dialog = createPopupDialog(self, self.popup_save_success, "VCM_DIALOG_SAVE_TITLE", "VCM_DIALOG_SAVE_FINISH")
		self.current_dialog.popup_id = POPUP_ID_CREATE_POINT_SUCCESS
		self:enableClickables(false)
		self.state = MAINPAGE_STATE_IDLE
	else
		if AnimatorComponent_isStopped(self.animation_controller) then
			if self.animation_controller == self.anim_camera_drc_up then
				self.state = MAINPAGE_STATE_DRC_CAMERA_UP
				self.drc_top_button:setState("idle")
				self.animation_controller = nil
			elseif self.animation_controller == self.anim_drc_button_flash then
				VC_playSoundId(SOUND_DECIDE_SUB)
				self.state = MAINPAGE_STATE_DRC_CAMERA_UP
				self.animation_controller = nil
				self:launchAnimation(self.anim_camera_drc_down, MAINPAGE_STATE_ANIM)
			elseif self.animation_controller == self.anim_camera_drc_down then
				self:enableClickables(true)
				self.drc_bottom_button:setState("idle")
				self.state = MAINPAGE_STATE_IDLE
				self.animation_controller = nil
			end
		end
	end
end

-- Callback when object is a button and is clicked
function MainPage:onButtonClick(button)
	
	if not self.is_active then
		return
	end
	if not self.disableInput then	
		if self.state == MAINPAGE_STATE_IDLE and self.current_dialog == nil then
			-- RESET
			if button == self.reset_button then
				self:enableClickables(false)
				if GUI.focusedElement ~= nil then
					VC_playSoundId(SOUND_KEY_DECIDE_SUB)
				else
					VC_playSoundId(SOUND_DECIDE_SUB)
				end
				self.current_dialog = createPopupDialog(self, self.popup_message_2_buttons2, "VCM_DIALOG_RESET_GUIDE", "VCM_DIALOG_RESET_NOTICE")
				self.current_dialog.popup_id = POPUP_ID_RESET
			-- LOAD RESTORE POINT
			elseif button == self.load_button then
				self:enableClickables(false)
				if GUI.focusedElement ~= nil then
					VC_playSoundId(SOUND_KEY_DECIDE_SUB)
				else
					VC_playSoundId(SOUND_DECIDE_SUB)
				end
				
				if useSRAM then
					self.current_dialog = createPopupDialog(self, self.popup_load_restorepoint , "VCM_DIALOG_LOAD_TITLE", "VCM_DIALOG_LOAD_NOTICE_WITH_SRAM")
				else
					self.current_dialog = createPopupDialog(self, self.popup_load_restorepoint , "VCM_DIALOG_LOAD_TITLE", "VCM_DIALOG_LOAD_NOTICE")
				end

				LabelComponent_setTextFromStringId(self.current_dialog.message2, "VCM_DIALOG_LOAD_GUIDE")
				self.current_dialog.popup_id = POPUP_ID_LOAD_POINT
			-- CREATE RESTORE POINT
			elseif button == self.create_button then
				self:enableClickables(false)
				if GUI.focusedElement ~= nil then
					VC_playSoundId(SOUND_KEY_DECIDE_SUB)
				else
					VC_playSoundId(SOUND_DECIDE_SUB)
				end
				
				if not self.has_state then
					if useSRAM then
						self.current_dialog = createPopupDialog(self, self.popup_save_restorepoint, "VCM_DIALOG_SAVE_TITLE", "VCM_DIALOG_SAVE_GUIDE_WITH_SRAM")
					else
						self.current_dialog = createPopupDialog(self, self.popup_save_restorepoint, "VCM_DIALOG_SAVE_TITLE", "VCM_DIALOG_SAVE_GUIDE")
					end
				else
					self.current_dialog = createPopupDialog(self, self.popup_save_restorepoint_exist, "VCM_DIALOG_SAVE_TITLE", "VCM_DIALOG_SAVE_NOTICE")
					if useSRAM then
						LabelComponent_setTextFromStringId(self.current_dialog.message2, "VCM_DIALOG_SAVE_GUIDE_WITH_SRAM")
					else
						LabelComponent_setTextFromStringId(self.current_dialog.message2, "VCM_DIALOG_SAVE_GUIDE")
					end
				end
				self.current_dialog.isSaveDialog = true

				self.current_dialog.popup_id = POPUP_ID_CREATE_POINT

			-- SETTINGS
			elseif button == self.settings_button then
				self:enableClickables(false)
				g_menu:switchPage(SETTINGS_PAGE)
				self.state = MAINPAGE_STATE_STANDBY
				if GUI.focusedElement ~= nil then
					VC_playSoundId(SOUND_KEY_DECIDE_SUB)
				else
					VC_playSoundId(SOUND_DECIDE_SUB)
				end
			-- DRC PANEL
			elseif button == self.drc_bottom_button then
				self:enableClickables(false)
				VC_playSoundId(SOUND_DECIDE_SUB)
				self.drc_top_button:setState("disabled")
				self.drc_bottom_button:setState("disabled")
				self:launchAnimation(self.anim_camera_drc_up, MAINPAGE_STATE_ANIM)
			-- MANUAL	
			elseif button == self.manual_button then
				self:enableClickables(false)
				g_menu:fadeOut()
				self.state = MAINPAGE_STATE_FADEOUT_TO_MANUAL
				VC_playSoundId(SOUND_DECIDE_SUB)
			elseif button == self.back_button then
				self:enableClickables(false)
				self.balloon_showed = false
				self.state = MAINPAGE_STATE_STANDBY
				VC_playSoundId(SOUND_FINISH)
				g_menu:quit()
			end
		elseif self.state == MAINPAGE_STATE_DRC_CAMERA_UP then
			if button == self.drc_top_button then
				self.drc_top_button:setState("disabled")
				self.drc_bottom_button:setState("disabled")
				VC_playSoundId(SOUND_CANCEL)
				self:launchAnimation(self.anim_camera_drc_down, MAINPAGE_STATE_ANIM)
			end
		end
	end

end

-- Callback when object is a button and is pressed
function MainPage:onButtonPress(button)
	if not self.disableInput and GUI.focusedElement == nil then
		VC_playSoundId(SOUND_SELECT)
	end
end

function MainPage:onCheckToggle(check, checked_flag)
	print("Check toggled " .. tostring(checked_flag))
	self.drc_top_button:setState("disabled")
	self.drc_bottom_button:setState("idle")
	self:changeDRCPlayerId(check)
end

function MainPage:onPressed()
end

-- Callback when object is a button and is released
function MainPage:onReleased()

end

function MainPage:launchAnimation(animator, new_state)
	Component_enable(animator)
	AnimatorComponent_play(animator)
	if new_state then
		self.state = new_state
		self.animation_controller = animator
	end
end

function MainPage:changeDRCPlayerId(button)
	for i = 1, numberOfPlayers  do
		if button == self.drc_unselected_buttons[i] then
			WorldNode_setLocalPosition(self.flash_node, self.drc_button_flash_pos_x_table[i], self.drc_button_flash_pos_y)
			VC_playSoundId(SOUND_CHECK)
			if self.animation_controller == nil then
				self:launchAnimation(self.anim_drc_button_flash, MAINPAGE_STATE_ANIM)
			else
				self:launchAnimation(self.anim_drc_button_flash)
			end
			LabelComponent_setText(self.drc_led_label, Localization_getText(self.drc_led_label_texts[i]))
			LabelComponent_setText(self.drc_icon_label, Localization_getText(self.player_guide_label_texts[i]))
			self.drc_unselected_buttons[i]:setState("idle")
			self.drc_unselected_buttons[i]:setChecked(true)
			WorldNode_setLocalPosition(self.drc_led_node, self.drc_led_pos_x_table[i], self.drc_led_pos_y)
			connectedControllers[1].playerId = i
			VC_setDRC0PlayID(i-1)
		else
			self.drc_unselected_buttons[i]:setState("idle")
			self.drc_unselected_buttons[i]:setChecked(false)
		end
	end
end

function MainPage:controllersStatusChanged()
	local result = {}
	for i = 1, 5 do 
		if self.controllers[i].controller_type ~= connectedControllers[i].controller_type then
			if self.controllers[i].controller_type == nil then
				result[i] = 1
			elseif connectedControllers[i].controller_type == nil then
				result[i] = -1
			else
				result[i] = 2
			end
		else
			result[i] = 0
		end
	end
	self.controllers = copyTab(connectedControllers)
	return result
end

function MainPage:showControllerIcons()
	local invoker_node
	local normal_node
	local cla_node
	local urcc_node
	local controller_count = 0
	for i = 1, 5 do
		if (next(self.controllers[i])) then
			controller_count = controller_count + 1
			if i == 1 then
				WorldNode_setEnabled(self.drc_icon_node, true)
				LabelComponent_setText(self.drc_icon_label, Localization_getText(self.player_guide_label_texts[self.controllers[1].playerId]))
				invoker_node = WorldNode_getChildByName(self.drc_icon_node, "invoker")
				normal_node = WorldNode_getChildByName(self.drc_icon_node, "normal")
				if self.controllers[i].invoker then
					WorldNode_setEnabled(invoker_node, true)
					WorldNode_setEnabled(normal_node, false)
				else
					WorldNode_setEnabled(invoker_node, false)
					WorldNode_setEnabled(normal_node, true)
				end
			else
				WorldNode_setEnabled(self.controller_icon_nodes[i], true)
				cla_node = WorldNode_getChildByName(self.controller_icon_nodes[i], "cla")
				urcc_node = WorldNode_getChildByName(self.controller_icon_nodes[i], "urcc")
				if self.controllers[i].controller_type == "cla" then
					WorldNode_setEnabled(cla_node, true)
					WorldNode_setEnabled(urcc_node, false)
					invoker_node = WorldNode_getChildByName(cla_node, "invoker")
					normal_node = WorldNode_getChildByName(cla_node, "normal")
					if self.controllers[i].invoker then
						WorldNode_setEnabled(invoker_node, true)
						WorldNode_setEnabled(normal_node, false)
					else
						WorldNode_setEnabled(invoker_node, false)
						WorldNode_setEnabled(normal_node, true)
					end
				elseif self.controllers[i].controller_type == "urcc" then
					WorldNode_setEnabled(cla_node, false)
					WorldNode_setEnabled(urcc_node, true)
					invoker_node = WorldNode_getChildByName(urcc_node, "invoker")
					normal_node = WorldNode_getChildByName(urcc_node, "normal")
					if self.controllers[i].invoker then
						WorldNode_setEnabled(invoker_node, true)
						WorldNode_setEnabled(normal_node, false)
					else
						WorldNode_setEnabled(invoker_node, false)
						WorldNode_setEnabled(normal_node, true)
					end
				end
			end
			AnimatorComponent_stop(self.anims_hide_icon[i])
			AnimatorComponent_setCurrentTime(self.anims_show_icon[i], 0.4)
		else
			WorldNode_setEnabled(self.controller_icon_nodes[i], false)
		end
	end
end

function MainPage:showControllerIcon(i)
	local invoker_node
	local normal_node
	if i == 1 then
		WorldNode_setEnabled(self.drc_icon_node, true)
		LabelComponent_setText(self.drc_icon_label, Localization_getText(self.drc_led_label_texts[self.controllers[1].playerId]))
		invoker_node = WorldNode_getChildByName(self.drc_icon_node, "invoker")
		normal_node = WorldNode_getChildByName(self.drc_icon_node, "normal")
		if self.controllers[1].invoker then
			WorldNode_setEnabled(invoker_node, true)
			WorldNode_setEnabled(normal_node, false)
		else
			WorldNode_setEnabled(invoker_node, false)
			WorldNode_setEnabled(normal_node, true)
		end
	else	
		WorldNode_setEnabled(self.controller_icon_nodes[i], true)
		local cla_node = WorldNode_getChildByName(self.controller_icon_nodes[i], "cla")
		local urcc_node = WorldNode_getChildByName(self.controller_icon_nodes[i], "urcc")
		if self.controllers[i].controller_type == "cla" then
			WorldNode_setEnabled(cla_node, true)
			WorldNode_setEnabled(urcc_node, false)
			invoker_node = WorldNode_getChildByName(cla_node, "invoker")
			normal_node = WorldNode_getChildByName(cla_node, "normal")
			if self.controllers[i].invoker then
				WorldNode_setEnabled(invoker_node, true)
				WorldNode_setEnabled(normal_node, false)
			else
				WorldNode_setEnabled(invoker_node, false)
				WorldNode_setEnabled(normal_node, true)
			end
		elseif self.controllers[i].controller_type == "urcc" then
			WorldNode_setEnabled(cla_node, false)
			WorldNode_setEnabled(urcc_node, true)
			invoker_node = WorldNode_getChildByName(urcc_node, "invoker")
			normal_node = WorldNode_getChildByName(urcc_node, "normal")
			if self.controllers[i].invoker then
				WorldNode_setEnabled(invoker_node, true)
				WorldNode_setEnabled(normal_node, false)
			else
				WorldNode_setEnabled(invoker_node, false)
				WorldNode_setEnabled(normal_node, true)
			end
		end
	end
	AnimatorComponent_stop(self.anims_hide_icon[i])
	self:launchAnimation(self.anims_show_icon[i])
end

function MainPage:hideControllerIcon(i)
	AnimatorComponent_stop(self.anims_show_icon[i])
	self:launchAnimation(self.anims_hide_icon[i])
end

function MainPage:switchControllerIcon(i)
	local invoker_node
	local normal_node
	local cla_node = WorldNode_getChildByName(self.controller_icon_nodes[i], "cla")
	local urcc_node = WorldNode_getChildByName(self.controller_icon_nodes[i], "urcc")
	if self.controllers[i].controller_type == "cla" then
		WorldNode_setEnabled(cla_node, true)
		WorldNode_setEnabled(urcc_node, false)
		invoker_node = WorldNode_getChildByName(cla_node, "invoker")
		normal_node = WorldNode_getChildByName(cla_node, "normal")
		if self.controllers[i].invoker then
				WorldNode_setEnabled(invoker_node, true)
				WorldNode_setEnabled(normal_node, false)
			else
				WorldNode_setEnabled(invoker_node, false)
				WorldNode_setEnabled(normal_node, true)
			end
	elseif self.controllers[i].controller_type == "urcc" then
		WorldNode_setEnabled(cla_node, false)
		WorldNode_setEnabled(urcc_node, true)
		invoker_node = WorldNode_getChildByName(urcc_node, "invoker")
		normal_node = WorldNode_getChildByName(urcc_node, "normal")
		if self.controllers[i].invoker then
			WorldNode_setEnabled(invoker_node, true)
			WorldNode_setEnabled(normal_node, false)
		else
			WorldNode_setEnabled(invoker_node, false)
			WorldNode_setEnabled(normal_node, true)
		end
	end
end

function MainPage:layoutIcons()
	local controller_count = 0
	local icon_widths_ref_table = { drc = {105, 93}, cla = {85, 72}, urcc = {89, 74} }
	local gap = 10
	local container_start_x = 192
	local container_end_x = 614
	local icon_start_x = 0
	local icon_widths = {}
	local icon_group_width = 0
	local icon_positions = {}
	for i = 1, 5 do
		if (next(self.controllers[i])) then
			controller_count = controller_count + 1
			if self.controllers[i].controller_type == "drc" then
				if self.controllers[i].ivoker then
					icon_widths[i] = icon_widths_ref_table.drc[1]
				else
					icon_widths[i] = icon_widths_ref_table.drc[2]
				end
			elseif self.controllers[i].controller_type == "cla" then
				if self.controllers[i].ivoker then
					icon_widths[i] = icon_widths_ref_table.cla[1]
				else
					icon_widths[i] = icon_widths_ref_table.cla[2]
				end
			elseif self.controllers[i].controller_type == "urcc" then
				if self.controllers[i].ivoker then
					icon_widths[i] = icon_widths_ref_table.urcc[1]
				else
					icon_widths[i] = icon_widths_ref_table.urcc[2]
				end
			end
		else
			icon_widths[i] = 0
		end
		icon_group_width = icon_group_width + icon_widths[i]
	end
	icon_group_width = icon_group_width + (controller_count-1) * gap
	icon_start_x = (container_end_x - container_start_x - icon_group_width)/2 + container_start_x
	for i = 1, 5 do
		if icon_widths[i] ~= 0 then
			icon_positions[i] = icon_start_x + icon_widths[i]/2
			icon_start_x = icon_positions[i] + icon_widths[i]/2 + gap
		else
			icon_positions[i] = 0
		end
	end
	return icon_positions
end

function MainPage:iconsLayoutUpdate(dt)
	local stiffness = 15
	for i = 1, 5 do
		if(next(self.controllers[i])) then
			self.icons_currentPos[i], self.icons_currentVel[i] = springSmoothUpdate(self.icons_currentPos[i], self.icons_currentVel[i], self.icons_targetPos[i], stiffness, dt)
			WorldNode_setLocalPosition(self.controller_icon_nodes[i], self.icons_currentPos[i], -666)
		end
	end

end

function MainPage:showBalloon()
	self:balloonPosAdjust()
	self:launchAnimation(self.anim_show_balloon)
	self.balloon_showed = true
	self.balloon_hidden = false
end

function MainPage:hideBalloon()
	if self.balloon_hidden == false then
		AnimatorComponent_stop(self.anim_show_balloon)
		self:launchAnimation(self.anim_hide_balloon)
		self.balloon_hidden = true
	end
end

function MainPage:balloonPosAdjust()
	local pos_x = self.icons_currentPos[g_menu.invokerIndex]
	WorldNode_setLocalPosition(self.active_icon_balloon_node, pos_x, -615.0)
	if pos_x <= 502 then
		WorldNode_setLocalPosition(self.icon_balloon_node, 79.0, 0.0)
	else
		WorldNode_setLocalPosition(self.icon_balloon_node, 581 - pos_x , 0.0)
	end
end

-- Callback when popup close
function MainPage:onPopupClose(popup, answer)
	print("popup close with answer " .. answer)

	if answer == "ok" or answer == "yes" then
		if self.current_dialog.popup_id == POPUP_ID_CREATE_POINT then
			self:enableClickables(false)
			g_menu.timer2 = g_menu.timer
			VC_saveState()
			self.current_dialog.popup_id = POPUP_ID_CREATING_POINT

		elseif self.current_dialog.popup_id == POPUP_ID_LOAD_POINT then
			self:enableClickables(false)
			VC_restoreState()
			
			VC_playSoundId(SOUND_DECIDE_SUB)
			
			self.current_dialog = createPopupDialog(self, self.popup_1_button, "VCM_DIALOG_LOAD_TITLE", "VCM_DIALOG_LOAD_FINISH")
			VC_replaceTexture(g_menu.screen_shot_save, 1)

			self.current_dialog.popup_id = POPUP_ID_LOAD_POINT_SUCCESS
			
		elseif self.current_dialog.popup_id == POPUP_ID_CREATE_POINT_SUCCESS then
			
			self.current_dialog = nil

			VC_replaceTexture(g_menu.screen_shot_load, 0)
			
			self.has_state = true
			self.load_button:setState("idle")
			WorldNode_setEnabled(self.screenshots_node, true)
			
			local year, month, day, hour, minute, second = VC_getSaveStateDate()

			TextVariableManager_setIntVariable("FILE_YEAR", year)
			TextVariableManager_setIntVariable("FILE_MONTH", month)
			TextVariableManager_setIntVariable("FILE_DAY", day)
			TextVariableManager_setIntVariable("FILE_HOUR", hour)
			TextVariableManager_setIntVariable("FILE_MINUTE", minute)
			
			print("PopupClosed Ok POPUP_ID_CREATE_POINT_SUCCESS")
	
		elseif self.current_dialog.popup_id == POPUP_ID_LOAD_POINT_SUCCESS then
			
			self.current_dialog = nil

		elseif self.current_dialog.popup_id == POPUP_ID_RESET then
			self:enableClickables(false)
			self.balloon_showed = false
			self.state = MAINPAGE_STATE_STANDBY
			VC_reset()
			g_menu:quit()
			-- self.current_dialog = nil
		end
	else
		self.current_dialog = nil
	end

	if self.current_dialog == nil then
		self:enableClickables(true)
	end
end

function MainPage:enableClickables(enable)
	
	Component_setEnabled(self.reset_button.clickable, enable)
	Component_setEnabled(self.load_button.clickable, enable)
	Component_setEnabled(self.create_button.clickable, enable)
	Component_setEnabled(self.settings_button.clickable, enable)
	Component_setEnabled(self.manual_button.clickable, enable)
	Component_setEnabled(self.back_button.clickable, enable)
	Component_setEnabled(self.drc_bottom_button.clickable, enable)
	
end
