require 'scripts/gui/gui.lua'
require 'scripts/VC_methods.lua'
require 'scripts/utils.lua'

numberOfPlayers = 0
useSRAM = true
connectedControllersInfo = {
							{controller_type = "drc", playerId = 1, invoker = true},
							{controller_type = "urcc"},
							{controller_type = "cla"},
							{},
							{}
						}



connectedControllers = {
							{},
							{},
							{},
							{},
							{}
						}

SOUND_BUTTON = 0
SOUND_CANCEL = 1
SOUND_CHECK = 2
SOUND_DECIDE_MAIN = 3
SOUND_DECIDE_SUB = 4
SOUND_ERROR = 5
SOUND_FINISH = 6
SOUND_PAGE_CHANGE = 7 
SOUND_SCROLL = 8
SOUND_SCROLL_LIMIT = 9 
SOUND_SCROLL_MSG = 10
SOUND_SELECT = 11
SOUND_SWK_TEXTMOVE = 12
SOUND_UNSELECT = 13
SOUND_WAITING = 14
SOUND_ZOOM_IN = 15
SOUND_ZOOM_OUT = 16
SOUND_KEY_CANCEL = 17
SOUND_KEY_DECIDE_SUB = 18
SOUND_KEY_FINISH = 19
SOUND_VCM_START = 20



--------------------------------------------------------------------------------
-- Menu Object script
--------------------------------------------------------------------------------

-- global instance of menu
declare 'g_menu'

-- Pages ids
MAIN_PAGE = 0
SETTINGS_PAGE = 1

MENU_STATE_IDLE = 0
MENU_STATE_ANIM_OPEN_MENU = 1
MENU_STATE_ANIM_CLOSE_MENU = 2
MENU_STATE_ANIM_OPEN_SETTINGS = 3
MENU_STATE_ANIM_CLOSE_SETTINGS = 4
MENU_STATE_STANDBY = 5
MENU_STATE_STARTUP = 6

VCMenus = class()

-- Callback when object is added to the world
function VCMenus:start()
	lockGlobals()
	
	print('VCMenus:start()')
	g_menu = self
	if VC_isUseSRAM() == 0 then
		useSRAM = false
	end

	GUI.focusedElement = nil
	GUI:setInputCamera(self.camera_drc, 854, 480)
	
	self.isInvokerConnect = false

	self.main_page_script = WorldNode_getScriptComponent(self.main_page, "MainPage")

	self.occulting_sprite = Component_getNode(self.anim_menu_fadeout)

	self.invokerDevice = nil
	self.invokerIndex = nil
	self.drcGameScreenOff = nil
	self.state = MENU_STATE_STANDBY

	self.timer = 0

end

-- Callback when object is removed from the world
function VCMenus:stop()
	GUI:focusElement(nil)
end

-- Callback called every frames
function VCMenus:update(dt)
	-- self.timer = self.timer + 1
	-- if self.state == MENU_STATE_STANDBY then
	-- 	if self.timer == 100 then
	-- 		VC_invokeMenu()
	-- 	end
	-- end
	if self.state ~= MENU_STATE_STANDBY then
		VC_setControllerInfo()
		GUI:updateInputs(dt)
		
		if self.invokerDevice == nil then
			self.invokerDevice, self.invokerIndex = self:getInvokerDevice()
			self.isInvokerConnect = true
			numberOfPlayers = VC_getNumberOfPlayers()
		end
		
		if self.isInvokerConnect and next(connectedControllers[self.invokerIndex])  then
				self.invokerDevice = self:getInvokerDevice()
				self:setTextVariable()
		end
		
		if self.isInvokerConnect and not next(connectedControllers[self.invokerIndex]) then
			WorldNode_setEnabled(self.invoker_disconnect_tv, true)
			WorldNode_setEnabled(self.invoker_disconnect_drc, true)
			AnimatorComponent_stop(self.anim_disconnect_open_tv)
			AnimatorComponent_stop(self.anim_disconnect_open_drc)
			Component_enable(self.anim_disconnect_open_tv)
			Component_enable(self.anim_disconnect_open_drc)
			AnimatorComponent_play(self.anim_disconnect_open_tv)
			AnimatorComponent_play(self.anim_disconnect_open_drc)
			self.isInvokerConnect = false
		end
		
		if not self.isInvokerConnect and next(connectedControllers[self.invokerIndex]) then
			AnimatorComponent_stop(self.anim_disconnect_open_tv)
			AnimatorComponent_stop(self.anim_disconnect_open_drc)
			Component_enable(self.anim_disconnect_close_tv)
			Component_enable(self.anim_disconnect_close_drc)
			AnimatorComponent_play(self.anim_disconnect_close_tv)
			AnimatorComponent_play(self.anim_disconnect_close_drc)
			self.isInvokerConnect = true
			self.invokerDevice = self:getInvokerDevice()
			self:setTextVariable()
		end

		if self.state == MENU_STATE_STARTUP then
			print("VCMenus Startup")
			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)
			
			self.current_page = MAIN_PAGE
			WorldNode_setEnabled(self.touch_banned, self.invokerDevice ~= PAD_DEVICE)
			GUI.isTouchBanned = (self.invokerDevice ~= PAD_DEVICE)
			self:switchPage(MAIN_PAGE)
			VC_replaceTexture(self.screen_shot_save, 0)
			VC_replaceTexture(self.screen_shot_load, 1)
			if not self.keyconfig then
				self.keyconfig = Scene_instantiate(self.keyconfig_prefab)
				WorldNode_getScriptComponent(self.keyconfig, 'KeyConfig').camera = get(self.worldNode, { 'camera_drc' }, 'camera')
				WorldNodeManager_addHierarchyToNode(self.worldNode, self.keyconfig)
			end
			WorldNode_setEnabled(self.keyconfig, false)
			VC_playSoundId(SOUND_VCM_START)

		else
			if self.invokerDevice ~= PAD_DEVICE then
				if GUI:isAnythingPressed() and not self.touch_banned_showing then
					Component_enable(self.anim_touch_banned)
					self.touch_banned_showing = true
					AnimatorComponent_play(self.anim_touch_banned)
				end
				if AnimatorComponent_isStopped(self.anim_touch_banned) then
					Component_disable(self.anim_touch_banned)
					self.touch_banned_showing = false
				end
			end
				
			if self.state == MENU_STATE_ANIM_OPEN_MENU then
				if AnimatorComponent_isStopped(self.anim_open_menu) then
					self.main_page_script.disableInput = false
					self.main_page_script:enableClickables(true)
					self.state = MENU_STATE_IDLE
				end
			elseif self.state == MENU_STATE_ANIM_CLOSE_MENU then
				AnimatorComponent_setCurrentTime(self.anim_touch_banned, 0)
				AnimatorComponent_stop(self.anim_touch_banned)
				WorldNode_setEnabled(self.touch_banned, false)
				WorldNode_setEnabled(self.invoker_disconnect_tv, false)
				WorldNode_setEnabled(self.invoker_disconnect_drc, false)
				if AnimatorComponent_isStopped(self.anim_close_menu) then
					self.state = MENU_STATE_STANDBY
					self.invokerDevice = nil
					self.invokerIndex = nil
					self.timer = 0
					GUI.state = 0
					VC_quitMenu()
					if self.drcGameScreenOff then
						VC_drcGameScreenOff()
						self.drcGameScreenOff = nil
					end
				end
			elseif self.state == MENU_STATE_ANIM_OPEN_SETTINGS then
				if AnimatorComponent_isStopped(self.anim_open_settings) then
					WorldNode_setEnabled(self.main_page, false)
					self.settings_page_script.is_active = true
					self.state = MENU_STATE_IDLE
				end
			elseif self.state == MENU_STATE_ANIM_CLOSE_SETTINGS then
				if AnimatorComponent_isStopped(self.anim_close_settings) then
					WorldNode_setEnabled(self.settings_page, false)
					self.main_page_script.is_active = true
					
					self.state = MENU_STATE_IDLE
				end
			elseif self.state == MENU_STATE_IDLE then
	
			end
		end
	end
end

function VCMenus:launchAnimation(animator, new_state)

	Component_enable(animator)
	AnimatorComponent_play(animator)

	self.state = new_state

end


-- Switch to page
function VCMenus:switchPage(page)

	if page == MAIN_PAGE then
		WorldNode_setEnabled(self.main_page, true)
		self.main_page_script.is_active = true
		self.main_page_script.disableInput = true
		self.main_page_script:enableClickables(false)
		self:launchAnimation(self.anim_open_menu, MENU_STATE_ANIM_OPEN_MENU)
	elseif page == SETTINGS_PAGE then
		GUI:focusElement(nil)
		self.main_page_script.is_active = false
		WorldNode_setEnabled(WorldNode_getChildByName(self.worldNode, 'keyconfig_page'), true)
	end
	
	self.current_page = page
	GUI_FocusElement(nil)
end

-- Quits the VC menu
function VCMenus:quit()
	if self.invokerDevice == PAD_DEVICE and GUI:isControlDown(PAD_DEVICE, GAMEPAD_RIGHT_STICK, GUI.KEY_THRESHOLD) then
		self.drcGameScreenOff = true
	end
	self.main_page_script.is_active = false
	self:launchAnimation(self.anim_close_menu, MENU_STATE_ANIM_CLOSE_MENU)
end

function VCMenus:fadeOut()

	WorldNode_setEnabled(self.occulting_sprite, true)
	AnimatorComponent_reset(self.anim_menu_fadeout)
	AnimatorComponent_setSpeed(self.anim_menu_fadeout, 1)
	AnimatorComponent_play(self.anim_menu_fadeout)

end

function VCMenus:fadeIn()

	WorldNode_setEnabled(self.occulting_sprite, true)
	AnimatorComponent_setSpeed(g_menu.anim_menu_fadeout, -1)
	AnimatorComponent_play(self.anim_menu_fadeout)

end

function VCMenus:fadeCompleted()
	return AnimatorComponent_isStopped(self.anim_menu_fadeout)
end

function VCMenus:stopFade()
	WorldNode_setEnabled(self.occulting_sprite, false)
end

function VCMenus:getInvokerDevice()
	local devices = {
				drc = PAD_DEVICE,
				cla = {CLASSIC_GAMEPAD_1, CLASSIC_GAMEPAD_2, CLASSIC_GAMEPAD_3, CLASSIC_GAMEPAD_4},
				urcc = {URCC_GAMEPAD_1, URCC_GAMEPAD_2, URCC_GAMEPAD_3, URCC_GAMEPAD_4}
			}
	if connectedControllers[1].invoker then
		return devices.drc, 1
	else
		for i = 2, 5 do
			if connectedControllers[i].invoker then
				return devices[connectedControllers[i].controller_type][i-1], i
			end
		end
	end
end

function VCMenus:setTextVariable()
	local claSet = set {CLASSIC_GAMEPAD_1, CLASSIC_GAMEPAD_2, CLASSIC_GAMEPAD_3, CLASSIC_GAMEPAD_4}
	if claSet[self.invokerDevice] then
		TextVariableManager_setVariable("BUTTON_A", Localization_getText("SYS_BUTTON_CLASSIC_A"))
		TextVariableManager_setVariable("BUTTON_B", Localization_getText("SYS_BUTTON_CLASSIC_B"))
		TextVariableManager_setVariable("BUTTON_ZR", Localization_getText("SYS_BUTTON_CLASSIC_ZL"))
	else
		TextVariableManager_setVariable("BUTTON_A", Localization_getText("SYS_BUTTON_A"))
		TextVariableManager_setVariable("BUTTON_B", Localization_getText("SYS_BUTTON_B"))
		TextVariableManager_setVariable("BUTTON_ZR", Localization_getText("SYS_BUTTON_ZR"))
	end
end
