-- local Viewport = MystViewportManager:GetInstance():GetViewport("Menu_viewport")
local Focus = {}
local BackAnim = {}

function OnSceneEnter()
  Cursors:Default()
  MystInventory:GetActive():ReleaseItem()
  
  s.sys_game_ho_2 = 1
  
  UI:AddWidget("menu.layout")
  Widget("MainMenu"):SetVisible(s.sys_dont_show_menu_widget == 0)
  Widgets.Inventory.Lock(false)
  
  UI:DayLabel("", "", false)
  UI:Helper("up", false)
  UI:Helper("down", false)
  if s.sys_hint_window_box == 1 then
    Widgets.hint_window_box.Close()
  end
  
  if Audio:GetMusic() ~= "menu" then
    s.sys_game_music = Audio:GetMusic()
    Audio:PauseAllSound()
    PlayMusic("menu",1,0)
  end
  s.sys_menu=1
end

function OnSceneExit()
  Widgets.Inventory.Lock(true)
  UI:DelWidget("MainMenu")  
  s.sys_menu=0
end

function OnUpdate()


  local vp = MystViewportManager:GetInstance():GetMainViewport()
  local view = vp:GetView()
  local elemCount = view:GetSelectedNodeCount()
  
   
  if elemCount > 0 then 
    local node = view:GetSelectedNode(0)
    local nodeName = node:GetName()
    if nodeName == "reg_s00_credits_001" then
        s.spr_s00_0000_s00_menu_credits = 0
    end
    if Focus[nodeName] == nil then
       node:LuaCall("OnMouseEnter")
       Focus[nodeName] = node
    else
    
    end
  else 
   
   for k,v in pairs(Focus) do
		if s00_0000_menu[v:GetProperty("Anim")] ~= nil then
			s00_0000_menu[v:GetProperty("Anim")].As("MystSprite2DSequence"):SetAhead(false)
			Audio:Play( v:GetProperty("snd_move_off") , false, false, -1, 0, false)
		end
    end    
  Focus = {}
  end
end

function OnGameResume()
 if states["sys_current_profile"] == 0 or states["sys_current_profile"] == "" then
    UI:LoadResource("profile_chooser.xml")
    OpenProfileChooser()
    Widget("MainMenu"):SetVisible(false)
 elseif SaveMgr:GetSlotsCount() > 0 then 	
	OnSceneExit()
	ResumeGame()
 end
end

function OnOptions()
  UI:LoadResource("options.xml")
  OpenSettingsMenu()
  Widget("MainMenu"):SetVisible(false)
end

function OnProfiles()
  UI:LoadResource("profile_chooser.xml")
  OpenProfileChooser()
  Widget("MainMenu"):SetVisible(false)
end

function OnSolution()
  UI:LoadResource("solution.xml")
  UI:LoadResource("solution_pages.xml")
  
  --if not UI:WidgetExists("Solution") then
    UI:AddWidget("solution.layout") 
  --end
  -- solution
  Solution = Widget("Solution")
  Solution:SetVisible(true)
end

function OnExit()
  MessageBox( L10n:ExpandString("sys_question_quit"), function()  Game:Exit() end , nil)  
end

function OnNewGame()
  if states["sys_current_profile"] == 0 or states["sys_current_profile"] == "" then
    UI:LoadResource("profile_chooser.xml")
    OpenProfileChooser()
    Widget("MainMenu"):SetVisible(false)
    
  elseif s.sys_game_start == 1 then
    MessageBox( L10n:ExpandString("sys_question_unsaved"), function()  
        RunNewGame()
        MystViewportManager:GetInstance():DestroyViewport("Menu") 
      end , nil )
  else
    RunNewGame()
    MystViewportManager:GetInstance():DestroyViewport("Menu")
  end
end

function RunNewGame()
  if states["sys_current_profile"] == 0 or states["sys_current_profile"] == "" then
    return 
  end
  local profile = states["sys_current_profile"]
  
  if s.sys_ho_active == 1 then
    _G[MystHiddenGroup:GetMainLuaModuleName()].GameStop()
  end
  OnSceneExit()
  ResetGame()
  MystInventory:RemoveAll()
  MystStates:Clear()
  Game:NewGame()
  
  states["sys_current_profile"] = profile  
  
  s.sys_game_start = 1
  states["hero_short_name"] = Config["hero_short_name"]
  
  MystDiary:GetInstance():ClearContent()
	OpenInventory()
  OpenTopMenu()
    
  if Config["game_scene_startup"] then 
    GoToProgress(Config["game_scene_startup"])
  else 
    Scene:ChangeCamera("s00_0100_progress")
  end  
  OnNewGameStart()
end


function ResetGame()
  --Particle:Release()
  Audio:StopMusic("")
  -- Audio:Release()
end

local MoviesStatesMap = {
  s00_0000_menu_new_rev = { "mov_s00_new_game", 0,  "mov_s00_new_game" },
  s00_menu_back_rev     = { "mov_s00_back", 0,      "mov_s00_back" },
  s00_menu_exit_rev     = { "mov_s00_exit", 0,      "mov_s00_exit" },
  s00_menu_profile_rev  = { "mov_s00_profile", 0,   "mov_s00_profile" },
  s00_menu_settings_rev = { "mov_s00_settings", 0,  "mov_s00_settings" },
  s00_menu_solution_rev = { "mov_s00_solution", 0,  "mov_s00_solution" },
  
  s00_0000_menu_new = { "mov_s00_new_game_r_1", 1,  "mov_s00_new_game" },
  s00_menu_back     = { "mov_s00_exit_r_1", 1,      "mov_s00_back" },
  s00_menu_exit     = { "mov_s00_exit_r_1", 1,      "mov_s00_exit" },
  s00_menu_profile  = { "mov_s00_profile_r_1",1 ,   "mov_s00_profile" },
  s00_menu_settings = { "mov_s00_settings_r_1", 1,  "mov_s00_settings" },
  s00_menu_solution = { "mov_s00_solution_r_1", 1,  "mov_s00_solution" }
}

function MovieEnd(movie)
  if MoviesStatesMap[movie] ~= nil then
    s[MoviesStatesMap[movie][1]] = MoviesStatesMap[movie][2]
    
    if MoviesStatesMap[movie][3] == nil then return end
    
    local r, m, c
    if string.sub(movie, string.len(movie) -2) == "rev" then
       s[MoviesStatesMap[movie][3].."_r"] = 0
       s[MoviesStatesMap[movie][3]] = 0
       s[MoviesStatesMap[movie][3].."_r_1"] = 0
    else
      if s[MoviesStatesMap[movie][3]] == 2 then
        s[MoviesStatesMap[movie][3].."_r"] = 1
        s[MoviesStatesMap[movie][3]] = 0
        s[MoviesStatesMap[movie][3].."_r_1"] = 1       
      end
    end
    --print(1, MoviesStatesMap[movie][3].."_r_1", s[MoviesStatesMap[movie][3]], c)
   
  end
end
