dofile('data/Lua/Screens/MainMenu_Anim.lua')

function OnSceneLoad(screen)
    game_state = {}
    menu = screen
	menu.game_state = game_state

    LoadData(screen)

    custom_cursors = {}
    locked_objects = {}
	
	-- Create bttn
	game_state.buttons = {}
	
	table.insert(game_state.buttons, create_button(menu, "play_up", "play_over", "play_down", "Play", 136, 250, 504))
	table.insert(game_state.buttons, create_button(menu, "options_up", "options_over", "options_down", "Options", 131, 325, 503))
    table.insert(game_state.buttons, create_button(menu, "credits_up", "credits_over", "credits_down", "Credits", 184, 378, 502))
	table.insert(game_state.buttons, create_button(menu, "quit_up", "quit_over", "quit_down", "Quit", 183, 471, 501))	
	menu.change_player_button = create_button(menu, "change_p_up", "change_p_over", "change_p_down", "ChangePlayer", 248, 665, 501)
	table.insert(game_state.buttons, menu.change_player_button)
	
	menu.textUserName = nil
	---------------------------------------------------------------------------------------------
	local anim_menu = createCompositionmainM()
    anim_menu:AttachTo(screen)
    anim_menu:SetCyclic(true)
    anim_menu:SetZ(80)
    
    local anim_spider = createComposition_spider()
    anim_spider:AttachTo(screen)
    anim_spider:SetZ(81)
	---------------------------------------------------------------------------------------------
	menu.change_player_button:SetOpacity(0)
	menu:GetObject('eyes2'):SetOpacity(0)
	
	PlayAmbientMusic("menu_music", true)
	
	menu.BttnOverSound = MakeSoundEntity("button_menu_over")
end

function SetActiveUserText()
	local currentPlayer = GetGameManager():GetCurrPlayer()
	if currentPlayer then
		menu.textUserName = CreateTextEntity("Nic", '')
		menu.textUserName:AttachTo( menu )
		menu.textUserName:SetScale(1)
		menu.textUserName:SetZ(500)
		menu.textUserName:SetScrXY(235, 617)
		menu.textUserName:SetText( currentPlayer:GetName() )
	end
end

function UpdateCurrentUser()
    if menu.textUserName then
        menu.textUserName:Destroy()
        menu.textUserName = nil
    end
    local currentPlayer = GetGameManager():GetCurrPlayer()
	if GM.last_players_load_result_ == '' then		
		SetActiveUserText()
	else
	    local corrupted_name = nil
        local corrupted_name_delimeter_pos = string.find(GM.last_players_load_result_, ':')
        if corrupted_name_delimeter_pos then
            corrupted_name = string.sub(GM.last_players_load_result_, corrupted_name_delimeter_pos + 1)
        end
	
	    menu:SetAllowInput(false)
        menu:Stop()
		menu:GetObject('eyes2'):SetOpacity(0)
		
	    if GetGameManager():GetProfilesCount() > 0 then 
	        local screen = load_screen('data/Lua/Screens/DialogErrorCorruptedFileNotCurrent.lua')
            if corrupted_name then
                text_title:SetText("'"..corrupted_name.."'\n "..GetString('Q_ERROR_PROFILE_IS_CORRUPTED_NOT_CURRENT')..'\n')
			else
			    text_title:SetText(GetString('Q_ERROR_PROFILE_IS_CORRUPTED'))
			end
	    else
	        local scr = ShowInputPlayerNameScreen(GetString('Q_ERROR_PROFILE_IS_CORRUPTED')..'\n', 80)
	    end
	    
	    if currentPlayer == nil and GetGameManager():GetProfilesCount() > 0 then
	        local vecUsers = GetGameManager():GetPlayers()
	        local player = vecUsers:get(0)
	        if player then
	            GetGameManager():SetCurrPlayer(player:GetName())
	        end
	    end
	    
	    SetActiveUserText()
	    GM.last_players_load_result_ = ''
	end
	
end

function OnSceneFree()
    FreeData()
end

function OnScreenShown()
    GetGameApp():SetDontSuspend(false)
    UpdateCurrentUser()
end

function ShowInputPlayerNameScreen(caption)
    menu:SetAllowInput(false)
    menu:Stop()

    local screen
    if caption then
        screen = load_screen('data/Lua/Screens/DialogErrorCorruptedFileCurrent.lua')    
        text_title:SetText(caption)
    else
        screen = load_screen('data/Lua/Screens/DialogPlayerName.lua')
    end
    screen:SetLayer(menu:GetLayer() + 1)
    
--  menu.change_player_button:SetOpacity(0) 	
end

function OnUpdate(dt)
    if GM.last_players_load_result_ == '' then
		if GetGameManager():GetProfilesCount() == 0 then
            ShowInputPlayerNameScreen()
            return
        end
	else
	    UpdateCurrentUser()
    end

	menu.change_player_button:SetOpacity(1)
end

function OnLose()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
	if object:GetName() == 'invisible_change_player' then
		OnButtonMouseEnter('ChangePlayer')
	end
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
	if object:GetName() == 'invisible_change_player' then
		OnButtonMouseLeave('ChangePlayer')
	end
end

function OnButtonMouseEnter(title)
	menu:GetObject('eyes2'):ChangeOpacity(menu:GetObject('eyes2'):GetOpacity(), 1, 0.3)
	if  title == 'Play' or
	    title == 'Options' or
	    title == 'Credits' or
	    title == 'Quit'
	then
	    menu.BttnOverSound:StopSound()
        menu.BttnOverSound:Play(false)
	end
end

function OnButtonMouseLeave(title)
	for i, v in ipairs(menu.game_state.buttons) do
		if v:IsMouseOver() and v:GetName() ~= title then
			return
		end
	end
	menu:GetObject('eyes2'):ChangeOpacity(menu:GetObject('eyes2'):GetOpacity(), 0, 0.3)
end

function OnObjectMouseDown(object)
	if object:GetName() == 'invisible_change_player' then
		OnButtonPress('ChangePlayer')
	end
end

function OnObjectMouseUp(object)
end

function OnObjectFound(object)
end

function OnToolFound(tool)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
    
    menu:GetObject('eyes2'):ChangeOpacity(menu:GetObject('eyes2'):GetOpacity(), 0, 0.3)
    CurrPlayer  = GetGameManager():GetCurrPlayer()
    if title == 'Play' and CurrPlayer then
        if menu then
            menu:ScreenDestroy()
            menu = nil
        end
        game_state.state = GetGameManager():GetCurrPlayer():GetGameState();
        if GameStateExists('last_scene_path') then
            local save_path = GetGameState('last_scene_path'):Get(0)
            StartLevel(save_path)
        else
            local intro_screen = load_screen('data/Lua/Screens/IntroScreen.lua')
		    intro_screen:SetLayer(5)
		    
		    StopCurrentMusicTrack(true)
        end
		return
    end
    
	menu:SetAllowInput(false)
    menu:Stop()
	--menu.change_player_button:SetOpacity(0)
    if title == 'Options' then
        local screen = load_screen('data/Lua/Screens/OptionsScreen.lua')
        screen:SetLayer(menu:GetLayer() + 1)
    end
    if title == 'Credits' then
        local screen = load_screen('data/Lua/Screens/DialogCredits.lua')
        screen:SetLayer(LAYER_HELP)
    end
    if title == 'Quit' then
        local screen = load_screen('data/Lua/Screens/DialogQuit.lua')
        screen:SetLayer(2)
    end
    if title == 'ChangePlayer' then
        local screen = load_screen('data/Lua/Screens/PlayersScreen.lua')
        screen:SetLayer(2)
		menu.change_player_button:SetOpacity(0)
    end
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function OnLostFocus()
end

function OnGainFocus()
end

-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData(screen)
	local anim = CreateAnimation('data/Texture/Main_menu/bg.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bg',anim,512,384,70,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/play.png',0,0,179,99,1,0)
	anim:SetHotSpot(89,45)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('play_up',anim,371,146,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/play.png',0,99,179,99,1,0)
	anim:SetHotSpot(89,45)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('play_over',anim,378,231,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/play.png',0,99,179,99,1,0)
	anim:SetHotSpot(89,45)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('play_down',anim,757,77,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/quit.png',0,0,119,112,1,0)
	anim:SetHotSpot(59,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('quit_up',anim,487,435,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/quit.png',0,112,119,112,1,0)
	anim:SetHotSpot(59,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('quit_over',anim,622,412,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/quit.png',0,112,119,112,1,0)
	anim:SetHotSpot(59,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('quit_down',anim,551,423,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/options.png',0,0,177,99,1,0)
	anim:SetHotSpot(88,45)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('options_up',anim,131,325,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/options.png',0,99,177,99,1,0)
	anim:SetHotSpot(88,45)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('options_over',anim,766,169,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/options.png',0,99,177,99,1,0)
	anim:SetHotSpot(88,45)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('options_down',anim,629,184,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/change_p.png',0,0,351,40,1,0)
	anim:SetHotSpot(175,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('change_p_up',anim,248,665,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/change_p.png',0,40,351,40,1,0)
	anim:SetHotSpot(175,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('change_p_over',anim,680,621,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/change_p.png',0,40,351,40,1,0)
	anim:SetHotSpot(175,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('change_p_down',anim,681,582,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/anim/eyes2.png',0,0,143,37,1,0)
	anim:SetHotSpot(71,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eyes2',anim,110,94,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('invisible_change_player',anim,237,619,100,0,48,5)
	local anim = CreateAnimation('data/Texture/Main_menu/logo_small.png',0,0,403,162,1,0)
	anim:SetHotSpot(201,81)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('logo_small',anim,829,75,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/credits.png',0,0,177,119,1,0)
	anim:SetHotSpot(88,59)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('credits_up',anim,184,378,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/credits.png',0,119,177,119,1,0)
	anim:SetHotSpot(88,59)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('credits_over',anim,707,710,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Main_menu/credits.png',0,119,177,119,1,0)
	anim:SetHotSpot(88,59)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('credits_down',anim,882,694,100,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
