dofile('Data/Scripts/Common/Screens/MainMenuBase.lua')

class 'MainMenu' (MainMenuBase)

function MainMenu:__init(name)
	MainMenuBase.__init(self, name)
	self.mouseBlocker   = MouseBlocker:new( self:getMainEntity() )
	self:initMainSound()
	self:initMainMusic()

	self.timer:add( function()
						self:logoBlink()
					end, 4)
end

function MainMenu:setupButtons()
	self:getMainEntity():getEntityNamed('bttn_exit').onPress:add(bind(self.onExitButtonClick, self))
	self:getMainEntity():getEntityNamed('bttn_options').onPress:add(bind(self.onOptionsButtonClick, self))
	self:getMainEntity():getEntityNamed('bttn_change_player').onPress:add(bind(self.onChangePlayerButtonClick, self))
	self:getMainEntity():getEntityNamed('bttn_achievments').onPress:add(bind(self.onAchievmentsButtonClick, self))
	self:getMainEntity():getEntityNamed('bttn_play').onPress:add(bind(self.onPlayButtonClick, self))
	self:getMainEntity():getEntityNamed('bttn_more_info').onPress:add(bind(self.onMoreInfoButtonClick, self))  
    self:getMainEntity():getEntityNamed('bttn_more_games').onPress:add(bind(self.onMoreGamesButtonClick, self))
    self:getMainEntity():getEntityNamed('bttn_collection').onPress:add(bind(self.onCollectionsButtonClick, self))
	

	self:getMainEntity():getEntityNamed('bttn_extras'):hide()
	self:getEntityNamed('bttn_extras_idle'):hide()
	self:getEntityNamed('bttn_extras_hover'):hide()

	-- add sfx-sound on click
	do		
		self:getMainEntity():getEntityNamed('bttn_more_games'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
		self:getMainEntity():getEntityNamed('bttn_extras'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
		self:getMainEntity():getEntityNamed('bttn_more_info'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )		
		self:getMainEntity():getEntityNamed('bttn_options'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )		
		self:getMainEntity():getEntityNamed('bttn_achievments'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
		self:getMainEntity():getEntityNamed('bttn_collection'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
		self:getMainEntity():getEntityNamed('bttn_exit'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )

		self:getMainEntity():getEntityNamed('bttn_play'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
		self:getMainEntity():getEntityNamed('bttn_change_player'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
	end  

	self:initBttnAnimations()
end
-------------------------------------------------------------------------------
function MainMenu:onPlayButtonClick()
	if not current_player then
		return 
	end
	assert(not current_scene)

	local state = current_player.gameState
	local start =   function()
						PopUpScreensMgr:destroyAll()
						startGame(EGameStateTag.SE)
					end
	self:getEntityNamed('compositionBG'):getElementByName('layer_handle_mask'):setOpacity(1)
	self:getEntityNamed('bttn_play'):hide()
	self:getEntityNamed('compositionStart'):show()
	self:getEntityNamed('compositionStart'):play()
	
	playCommonSound('PushPlayButton')

	if state:hasState(SE_COMPLETED, EGameStateTag.SE) then
		self.mouseBlocker:push()
		self:getEntityNamed('compositionStart').onFinish:add( function()
			self.mouseBlocker:pop()

			self:getEntityNamed('compositionStart').onFinish:removeAll()
			createYesNoScreen("RESTART_GAME_PROGRESS", 	function()
															state:clear(EGameStateTag.SE)

															createPlayingModeScreen(function()
																						local msg = StringManager:getString('PLAY_MODE_CHANGE_NOTE')
																						createOkScreen(msg, start, false)
																					end)
														end, nil, false)
		end)

		return
	end

	
	if state:hasState(PLAY_MODE, EGameStateTag.SE) then			

		self.mouseBlocker:push()
		self:getEntityNamed('compositionStart').onFinish:add( function()
																self:getEntityNamed('compositionStart').onFinish:removeAll()
																self.mouseBlocker:pop()
																start()
															  end)
	else
		self.mouseBlocker:push()
		self:getEntityNamed('compositionStart').onFinish:add( function()
																self.mouseBlocker:pop()
																self:getEntityNamed('compositionStart').onFinish:removeAll()
																createPlayingModeScreen(function()
																	local msg = StringManager:getString('PLAY_MODE_CHANGE_NOTE')
																	createOkScreen(msg, start, false)
																end)
															  end)		
	end
end
-------------------------------------------------------------------------------
function MainMenu:initBttnAnimations()
	self:getEntityNamed('compositionBG'):getElementByName('layer_handle_mask'):setOpacity(0)

	self.bttnsName = { 'bttn_options', 'bttn_more_info', 'bttn_more_games', 'bttn_achievments', 
				   	   'bttn_collection', 'bttn_extras', 'bttn_exit' }

	for _, name in pairs(self.bttnsName) do
		self:getEntityNamed(name .. '_idle'):play()
		self:getEntityNamed(name).onFocusIn:add( function(entity) self:onButtonFocusIn(entity) end )
		self:getEntityNamed(name).onFocusOut:add( function(entity) self:onButtonFocusOut(entity) end )
	end
end
-------------------------------------------------------------------------------
function MainMenu:getEntityColor(entity)
	local color = {}
	color.r      = entity:getColor():getR()
	color.g      = entity:getColor():getG()
	color.b      = entity:getColor():getB()
	color.a      = entity:getColor():getA()

	return color
end
-------------------------------------------------------------------------------
function MainMenu:onButtonFocusIn(entity, focus)
	local name = entity:getName()
	local idle = self:getEntityNamed(name .. '_idle')
	local hover = self:getEntityNamed(name .. '_hover')
	local color1 = self:getEntityColor(idle)
	local color2 = self:getEntityColor(hover)
	
	playCommonSound('MenuButtonHover')
 
	idle:setColor(color1.r, color1.g, color1.b, 1)
	hover:show()
	hover:play()
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function MainMenu:onButtonFocusOut(entity, focus)
	local name = entity:getName()

	local idle = self:getEntityNamed(name .. '_idle')
	local hover = self:getEntityNamed(name .. '_hover')
	local color1 = self:getEntityColor(idle)
	local color2 = self:getEntityColor(hover)
 
	idle:setColor(color1.r, color1.g, color1.b, 255)
	hover:hide()
	idle:play()
end

-------------------------------------------------------------------------------
function MainMenu:initMainSound()
	local root = getGameRoot()
	local sound_entity = root:getEntityNamed(NAME_MAIN_SOUND)
	if sound_entity then
		sound_entity:attachTo( self:getMainEntity() )
	else
		local sound_entity = createMainSoundEntity()
		sound_entity:attachTo( self:getMainEntity() )
	end
end
-------------------------------------------------------------------------------
function MainMenu:initMainMusic()
	local root = getGameRoot()
	local music_entity = root:getEntityNamed(NAME_MAIN_MUSIC)
	if music_entity then
		music_entity:attachTo( self:getMainEntity() )
	else
		local music_entity = createMainMusicEntity()
		music_entity:attachTo( self:getMainEntity() )
	end
end
-------------------------------------------------------------------------------
function MainMenu:onAchievmentsButtonClick()
	if not current_player 
        or not current_player.gameState:hasState(PLAY_MODE, DEFAULT_TAG_NAME) 
    then
        return 
    end	
	
	PopUpScreensMgr:destroyAll()
	AddUniqueGameState('PARENT_ACHIEVMENT_MAINMENU', 'true', EGameStateTag.GENERAL)
	if HasGameState(PLAY_MODE, tag) == false then
		AddUniqueGameState('PLAY_MODE_NEED_TO_BE_REMOVED', 'true', EGameStateTag.GENERAL)
		SetGameState(PLAY_MODE, EPlayMode.CASUAL, tag)
	end
	startLevel('l_Room')
end

function MainMenu:onCollectionsButtonClick()	
	if not current_player 
        or not current_player.gameState:hasState(PLAY_MODE, DEFAULT_TAG_NAME) 
    then
        return 
    end

	PopUpScreensMgr:destroyAll()
	createClockScreen()	
end

function MainMenu:onExitButtonClick()
	createYesNoScreen("REALLY_QUIT_GAME", bind(self.ApplicationExit, self), nil, false, nil, YES_NO_SCREEN_EXIT_HEADER)
end

function MainMenu:logoBlink()
	local rand_time = math.random(4, 12)

	self:getEntityNamed('logo_blink_sq'):play()
	self.timer:add( function()
						self:logoBlink()
					end, rand_time)
end

--------------------------------------------------------------------------------
--                      GENERATED CONTENT. DO NOT EDIT.                       --
--------------------------------------------------------------------------------

function MainMenu:CreateLoaderTable()
	local objects =
	{
		{ name = 'bg',  entityType = 'SpriteEntity',  x = 512,  y = 384,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 683, y = 384},  width = 2,  height = 2,  },
		{ name = 'menu_idle',  entityType = 'VideoEntity',  parentName = 'bg',  x = 0,  y = -108,  order = 0,  isRespondable = false,  scale = 1.5,  editingLocked = false,  treeItemIsExpanded = true,  path = 'Data/Video/Menu/menu_idle.webm',  hasAlpha = false,  isLooped = true,  fps = 24,  },
		{ name = 'bttn_achievments',  entityType = 'ButtonEntity',  parentName = 'bg',  x = -358,  y = 24,  order = 12,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Bttn/bttn_achievments.png',  text = '',  },
		{ name = 'bttn_collection',  entityType = 'ButtonEntity',  parentName = 'bg',  x = -386,  y = 83,  order = 12,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Bttn/bttn_collection.png',  text = '',  },
		{ name = 'bttn_more_info',  entityType = 'ButtonEntity',  parentName = 'bg',  x = -390,  y = -96,  order = 13,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Bttn/bttn_more_info.png',  text = '',  },
		{ name = 'bttn_play',  entityType = 'ButtonEntity',  parentName = 'bg',  x = 407,  y = 185,  order = 12,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Bttn/bttn_play.png',  text = '',  },
		{ name = 'bttn_more_games',  entityType = 'ButtonEntity',  parentName = 'bg',  x = -372,  y = -37,  order = 12,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Bttn/bttn_more_games.png',  text = '',  },
		{ name = 'bttn_options',  entityType = 'ButtonEntity',  parentName = 'bg',  x = -412,  y = -157,  order = 12,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Bttn/bttn_options.png',  text = '',  },
		{ name = 'bttn_extras',  entityType = 'ButtonEntity',  parentName = 'bg',  x = -422,  y = 143,  order = 12,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Bttn/bttn_extras.png',  text = '',  },
		{ name = 'layer_logo',  entityType = 'SpriteEntity',  parentName = 'bg',  x = -337,  y = -285,  order = 15,  scale = 0.48,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Layers/layer_logo.png',  width = 716,  height = 402,  },
		{ name = 'logo_blink_sq',  entityType = 'SpriteEntity',  parentName = 'layer_logo',  x = 0,  y = -22,  order = 0,  scale = 4.88,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Menu/Layers/logo_blink_sq.png',  width = 150,  height = 76,  fps = 12,  framesCount = 12,  },
		{ name = 'bttn_exit',  entityType = 'ButtonEntity',  parentName = 'bg',  x = -440,  y = 318,  order = 15,  angle = 1,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Bttn/bttn_exit.png',  text = '',  },
		{ name = 'layer_player_name',  entityType = 'SpriteEntity',  parentName = 'bg',  x = 373,  y = -63,  order = 12,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Layers/layer_player_name.png',  width = 234,  height = 42,  },
		{ name = 'layer_welcome_player',  entityType = 'SpriteEntity',  parentName = 'layer_player_name',  x = 0,  y = -55,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Layers/layer_welcome_player.png',  width = 234,  height = 38,  },
		{ name = 'textPlayerName',  entityType = 'TextEntity',  parentName = 'layer_player_name',  x = 3,  y = -2,  order = 20,  scale = 0.9,  editingLocked = false,  treeItemIsExpanded = true,  text = 'player_name',  font = 'Data/Fonts/diary_white.fnt',  height = 32,  },
		{ name = 'bttn_change_player',  entityType = 'ButtonEntity',  parentName = 'layer_player_name',  x = -2,  y = 42,  order = 12,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Bttn/bttn_change_player.png',  text = '',  },
		{ name = 'MainMenu_Anim_Bg',  entityType = 'CompositionContainer',  parentName = 'bg',  x = -683,  y = -384,  order = 2,  editingLocked = false,  treeItemIsExpanded = true,  path = 'Data/Scripts/Screens/Anim/MainMenu_Anim_Bg.lua',  },
		{ name = 'compositionBG',  entityType = 'CompositionSequence',  parentName = 'MainMenu_Anim_Bg',  x = 0,  y = 0,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  isCyclic = true,  speed = 1,  },
		{ name = 'bttn_options_idle',  entityType = 'SpriteEntity',  parentName = 'bg',  x = -411,  y = -154,  order = 10,  isRespondable = false,  scaleX = 1.08,  scaleY = 0.85,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_1_[].png',  width = 183,  height = 90,  mode = 4,  fps = 24,  framesCount = 24,  },
		{ name = 'bttn_options_hover',  entityType = 'SpriteEntity',  parentName = 'bttn_options_idle',  x = 0,  y = 0,  order = 11,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_3_[].png',  width = 183,  height = 90,  mode = 4,  fps = 12,  framesCount = 12,  },
		{ name = 'bttn_more_info_idle',  entityType = 'SpriteEntity',  parentName = 'bg',  x = -390,  y = -92,  order = 10,  isRespondable = false,  scaleX = 1.34,  scaleY = 0.83,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_1_[].png',  width = 183,  height = 90,  mode = 4,  fps = 24,  framesCount = 24,  },
		{ name = 'bttn_more_info_hover',  entityType = 'SpriteEntity',  parentName = 'bttn_more_info_idle',  x = 0,  y = 0,  order = 11,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_3_[].png',  width = 183,  height = 90,  mode = 4,  fps = 12,  framesCount = 12,  },
		{ name = 'bttn_more_games_idle',  entityType = 'SpriteEntity',  parentName = 'bg',  x = -374,  y = -32,  order = 10,  isRespondable = false,  scaleX = 1.53,  scaleY = 0.86,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_1_[].png',  width = 183,  height = 90,  mode = 4,  fps = 24,  framesCount = 24,  },
		{ name = 'bttn_more_games_hover',  entityType = 'SpriteEntity',  parentName = 'bttn_more_games_idle',  x = 0,  y = 0,  order = 11,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_3_[].png',  width = 183,  height = 90,  mode = 4,  fps = 12,  framesCount = 12,  },
		{ name = 'bttn_achievments_idle',  entityType = 'SpriteEntity',  parentName = 'bg',  x = -358,  y = 29,  order = 10,  isRespondable = false,  scaleX = 1.68,  scaleY = 0.89,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_1_[].png',  width = 183,  height = 90,  mode = 4,  fps = 24,  framesCount = 24,  },
		{ name = 'bttn_achievments_hover',  entityType = 'SpriteEntity',  parentName = 'bttn_achievments_idle',  x = 0,  y = 0,  order = 11,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_3_[].png',  width = 183,  height = 90,  mode = 4,  fps = 12,  framesCount = 12,  },
		{ name = 'bttn_collection_idle',  entityType = 'SpriteEntity',  parentName = 'bg',  x = -383,  y = 89,  order = 10,  isRespondable = false,  scaleX = 1.4,  scaleY = 0.89,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_1_[].png',  width = 183,  height = 90,  mode = 4,  fps = 24,  framesCount = 24,  },
		{ name = 'bttn_collection_hover',  entityType = 'SpriteEntity',  parentName = 'bttn_collection_idle',  x = 0,  y = 0,  order = 11,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_3_[].png',  width = 183,  height = 90,  mode = 4,  fps = 12,  framesCount = 12,  },
		{ name = 'bttn_extras_idle',  entityType = 'SpriteEntity',  parentName = 'bg',  x = -420,  y = 146,  order = 10,  isRespondable = false,  scaleX = 0.95,  scaleY = 0.89,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_1_[].png',  width = 183,  height = 90,  mode = 4,  fps = 24,  framesCount = 24,  },
		{ name = 'bttn_extras_hover',  entityType = 'SpriteEntity',  parentName = 'bttn_extras_idle',  x = 0,  y = 0,  order = 11,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_3_[].png',  width = 183,  height = 90,  mode = 4,  fps = 12,  framesCount = 12,  },
		{ name = 'bttn_exit_idle',  entityType = 'SpriteEntity',  parentName = 'bg',  x = -442,  y = 320,  order = 10,  isRespondable = false,  scaleX = 0.73,  scaleY = 0.96,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_1_[].png',  width = 183,  height = 90,  mode = 4,  fps = 24,  framesCount = 24,  },
		{ name = 'bttn_exit_hover',  entityType = 'SpriteEntity',  parentName = 'bttn_exit_idle',  x = 0,  y = 0,  order = 11,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Menu/Anim/Smoke_3_[].png',  width = 183,  height = 90,  mode = 4,  fps = 12,  framesCount = 12,  },
		{ name = 'MainMenu_Anim_Start_Game',  entityType = 'CompositionContainer',  parentName = 'bg',  x = -681,  y = -384,  order = 14,  editingLocked = false,  treeItemIsExpanded = true,  path = 'Data/Scripts/Screens/Anim/MainMenu_Anim_Start_Game.lua',  },
		{ name = 'compositionStart',  entityType = 'CompositionSequence',  parentName = 'MainMenu_Anim_Start_Game',  x = 0,  y = 0,  order = 0,  isVisible = false,  isRespondable = false,  isActive = false,  editingLocked = false,  treeItemIsExpanded = false,  speed = 1,  },
	}
	return objects
end
