//-----------------------------------------------------------------------------
// Torque Game Engine 
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// MainMenuGui is the main Control through which the game shell is viewed.
//-----------------------------------------------------------------------------

function MainMenuGui::onWake(%this)
{
   %this.loadAwardXML();
   
   %this.firstTime = %this.isFirstTime();   
      
   %this.pushThread(main_menu_thread);
      
   //if $ReplayButtonPressed = true, needs to go back to main menu 
   // and reload the scene (so dont play the music!!!)
   if(!$ReplayButtonPressed || $ReplayButtonPressed $= false){
      // Play menu music
      sfxPlay(MusicMainMenu);
   }
   
   $ReplayButtonPressed = false;
   
//   if (isObject(SceneGui))
//   {
//      PlayGameButton.setVisible(false);
//      ResumeButton.setVisible(true);
//   }
   
//   %pXml = loadMenuXML();
//   parseMainMenuXML(%pXml);
//   %pXml.delete();
   
   // Relese SceneGui objects
   releaseSceneGui();
   releaseWorldMapGui();
   
   Canvas.setWidescreen(MainMenuDlgCenter, 100, true);   
}

// Parse XML file looking for the welcome tag
function MainMenuGui::loadAwardXML(%this)
{
   %path = "./scriptsAndAssets/data/ui/award.xml";
   
   %pXml = new SimXMLDocument();
   // Load and parse XML file.
   if(!%pXml.loadFile(%path))
   {
      echo("Failed to load " @ %path);
      return;
   }
   
   // Push the first child element named "award" onto 
   // the XML document stack.
   if(!%pXml.pushFirstChildElement("award"))
   {
      echo("Failed to load award tag.");
      return;
   }
   
   // Get welcome texts
   if(!%pXml.pushFirstChildElement("welcome" @ %i))
   {
      echo("Failed to load welcome tag.");
      return;
   }
   %this.welcome = %pXml.attribute("text");
   %pXml.popElement();
   
   if(!%pXml.pushFirstChildElement("welcome2" @ %i))
   {
      echo("Failed to load welcome2 tag.");
      return;
   }
   %this.welcome2 = %pXml.attribute("text");
   %pXml.popElement();
   	
   %pXml.popElement();
   
   %pXml.delete();   
   
   echo("Debug: Award MainMenuGui XML loaded");
}


function MainMenuGui::playButtonPressSound(%this)
{
   sfxPlay(AudioButtonPress);
}

//-------------------------------------
function MainMenuGui::getThread(%this)
{
   %top = menu_thread_stack.getCount();
   if (%top > 0)
      return menu_thread_stack.getObject(%top-1);
   else
      return 0;
}

//-------------------------------------
function MainMenuGui::popCurrent(%this)
{   
   if (%thread = %this.getThread())
   {
      Canvas.popDialog(%thread.getObject(%thread.page));
      //echo ("POP Current: " @ %thread.getName() @ " page: " @ %thread.page);
   }
}

//-------------------------------------
function MainMenuGui::pushThread(%this, %thread)
{

   //echo ("PushThread: " @ %thread.getname());
   
//   StopMenuShrinkGrowMainMenu();
//   StopMenuShrinkGrowTakeMenu();

   %this.popCurrent();
   %thread.page = 0;
   menu_thread_stack.add(%thread);
   %this.update();
}

//-------------------------------------
function MainMenuGui::popThread(%this)
{
   Canvas.setWidescreen(MainMenuDlgCenter, 250, false);
   
   if (%thread = %this.getThread())
   {
      //echo ("PopThread: " @ %thread.getname());

      %this.popCurrent();
      menu_thread_stack.remove(%thread);
      %this.update();
   }
}

//-------------------------------------
function MainMenuGui::isNextPage(%this)
{
   if ( !(%thread = %this.getThread()) )
      return false;
   //echo ("isNext: " @ %thread.page @ " - " @ %thread.getCount());
   return %thread.page < (%thread.getCount() - 1);
}

//-------------------------------------
function MainMenuGui::isPrevPage(%this)
{
   if ( !(%thread = %this.getThread()) )
      return false;
   return %thread.page > 0;
}

//-------------------------------------
function MainMenuGui::isPrevThread(%this)
{
   return menu_thread_stack.getCount() > 1;
}

//-------------------------------------
function MainMenuGui::nextPage(%this)
{
   if (%this.isNextPage())
   {
      %this.popCurrent();
      %thread = %this.getThread();
      %thread.page = (%thread.page+1);
      %this.update();
   }
   else
      %this.popThread();
}

//-------------------------------------
function MainMenuGui::prevPage(%this)
{
   if (%this.isPrevPage())
   {
      %this.popCurrent();
      %thread = %this.getThread();
      %thread.page = (%thread.page-1);
      %this.update();
   }
}

//-------------------------------------
function MainMenuGui::update(%this)
{
   if (Canvas.getContent().getName() $= "GuiEditorGui")
   {
	   %this.popCurrent();
	   Canvas.popdialog(OverlayDlg);
	   return;
   }
	   
   if (%thread = %this.getThread())
   {
      //echo ("Update: " @ %thread.getName() @ " page: " @ %thread.page);
      %page = %thread.getObject(%thread.page);
            
      // Push the current page
      Canvas.pushDialog(%page);
   }
   
   %winScaleY = 1.0 / 600 * getWord(MainMenuDlgCenter.getExtent(),1);
   
   if(PlayerProfile.PlayerName1 !$= "" && PlayerProfile.PlayerName1 $= $pref::Player::Name) { $CurPlayerNum = 1; $CurPlayerName = $pref::Player::Name; }
   if(PlayerProfile.PlayerName2 !$= "" && PlayerProfile.PlayerName2 $= $pref::Player::Name) { $CurPlayerNum = 2; $CurPlayerName = $pref::Player::Name; }
   if(PlayerProfile.PlayerName3 !$= "" && PlayerProfile.PlayerName3 $= $pref::Player::Name) { $CurPlayerNum = 3; $CurPlayerName = $pref::Player::Name; }
   if(PlayerProfile.PlayerName4 !$= "" && PlayerProfile.PlayerName4 $= $pref::Player::Name) { $CurPlayerNum = 4; $CurPlayerName = $pref::Player::Name; }         
   
   // Show welcome text
   %welcomeText = %this.welcome2;
   if($pref::Player::Name !$= "")
      %welcomeText = strreplace(%this.welcome, "#" , $pref::Player::Name);   
      //%welcomeText = "Welcome,\n" @ $pref::Player::Name;
            
   PlayGameButton.setPosition(getWord(PlayGameButton.Position,0), (249+45)*%winScaleY);
//   ResumeButton.setPosition(getWord(ResumeButton.Position,0), (249+45)*%winScaleY);
   PlayMinigameButton.setPosition(getWord(PlayMinigameButton.Position,0), (386+43)*%winScaleY);
   SelectPlayerButton.setPosition(getWord(SelectPlayerButton.Position,0), (339+45)*%winScaleY);
   OptionsButton.setPosition(getWord(OptionsButton.Position,0), (293+45)*%winScaleY);
   ExitButton.setPosition(getWord(ExitButton.Position,0), (473+45)*%winScaleY);

   MainMenuPlayerText.setText(%welcomeText);   
}

//-------------------------------------
function MainMenuGui::onSleep(%this)
{
}

function MainMenuGui::onResize(%this, %width, %height)
{
   Canvas.setWidescreen(MainMenuDlgCenter, 2000, true);
}

//-------------------------------------
function MainMenuGui::goHome(%this)
{
   disconnect();
   if (Canvas.getContent() != %this.getId())
      Canvas.pushDialog(%this);
   else {
      while (%this.isPrevThread())
         %this.popThread();
   }
}

// Check if we already have a player profile
function MainMenuGui::isFirstTime(%this)
{
   if(!isObject(LCAPickler))
      new XMLPickler("LCApickler");

   %myFolder = LCApickler.getUserHomeDir();

   %myPath = %myFolder @ "/My Games/BehindTheScenes/";
   LCApickler.createPath(%myPath);
   
   %myPath = "file://" @ %myPath @ "Profiles.xml";
   
   if(!isObject(PlayerProfile))   
      new LCAPlayerProfile(PlayerProfile);

   // Try reading existing profile
   if(!LCApickler.unpickle(%myPath))
      return true;
      
   %remainingPlayers = 0;
   if(PlayerProfile.PlayerName1 !$= "") %remainingPlayers++;
   if(PlayerProfile.PlayerName2 !$= "") %remainingPlayers++;
   if(PlayerProfile.PlayerName3 !$= "") %remainingPlayers++;
   if(PlayerProfile.PlayerName4 !$= "") %remainingPlayers++;

   if(%remainingPlayers $= 0)
      return true;
   
   return false;
}
