// Called when the final score window pops up
function IntroGui::onWake(%this)
{
   IntroGuiBkgnd.setDML("scriptsAndAssets/data/ui/ho/inoutro/" @ $CurSceneName @ "_bkgnd.jpg");
      
   if(!$PlayTutorial && $CurFirstPlay $= 1)
   {
      %this.startLevel(false);
      return;
   }
   
   %this.initialize();
   
   %this.lastText = "";   
   %this.lastFontSize = "";
   %this.lastTextExtent = 10;
   %this.lastTextOffset = 0;
   %this.lastArrowButtonY = 0;
   %this.skip = false;
   
   // Play background noise
   sfxPlay(BkgndNoise@SceneGui.currentLevel);
   
   Canvas.setWidescreen(IntroGuiCenter, 100, false);   
}

function IntroGui::startLevel(%this, %playingMinigame)
{
   sfxStopAll();   
   Canvas.popDialog(IntroGui);
   SceneGui.start(%playingMinigame);
}

// Exit intro screen
function IntroGui::exit(%this)
{
   sfxStopAll();
   
   if(SceneGui.inIntro)
   {
      SceneGui.inIntro = false;
      %this.remove(Minigame);
      Minigame.delete();
   }
   
   // Show clapper board
   %path = "scriptsAndAssets/data/ui/clapperboard/clapper.jpg";
   IntroClapper.setDML(%path);    
   IntroClapper.rewind();
   
   %path = "scriptsAndAssets/data/ui/clapperboard/inoutro/" @ $CurSceneName @ "_intro_take" @ $CurTake @ ".jpg";
   IntroBoard.setDML(%path);        
   IntroBoard.rewind();

   IntroClapper.setVisible(true);
   IntroBoard.setVisible(true);
   IntroClapper.play(1);
   IntroBoard.play(1);
//   %this.schedule(1700, "startLevel");
   
   sfxPlay(AudioClapperBoardMoving);
   
//   %this.schedule(1600, "playClapperBoardCloseSound", 0);
}

function IntroGui::playClapperBoardCloseSound(%this)
{
   sfxPlay(AudioClapperBoardClose);
}

function IntroGui::initialize(%this)
{      
   IntroDirTriggerImga.setAlpha(0);
   IntroDirTriggerImga.setVisible(false);
   IntroDirTriggerImgb.setAlpha(0);
   IntroDirTriggerImgb.setVisible(false);
   %this.crossFadingDirector = false;
   
   IntroDirTriggerBubble.setVisible(false);   
   IntroDirTriggerBubbleText.setVisible(false);
   IntroDirTriggerBubbleText.setText("");
   
   IntroDirTriggerArrow.setVisible(false);
   
   %this.loadDirectorXML();
}

// Parse XML file looking for director behavior tags
function IntroGui::loadDirectorXML(%this)
{
   %path = "./scriptsAndAssets/data/ui/director" @ $CurSceneName @ "_take" @ $CurTake @ ".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 "director" onto 
   // the XML document stack.
   if(!%pXml.pushFirstChildElement("director"))
   {
      echo("Failed to load director tag.");
      return;
   }
   
   // Fields of SceneGui.dirTrigger[SceneGui.dirTriggerNum]      
   // 10 = text size

   %this.DirTriggerNum = 0;
   if(!%pXml.pushFirstChildElement("ani"))
   {
      echo("Director animations not found.");
      return;
   }
   
   while(true)
   {
      %showat = %pXml.attribute("showat");
             
      if(%showat $= "intro")
      {
         %showat = %pXml.attributeF32("showat");
         
         %textNum = 0;
         if(%pXml.pushFirstChildElement("bubble"))
         {
            %this.DirTrigger[%this.DirTriggerNum,"text",%textNum] = %pXml.attribute("text");
            %this.DirTrigger[%this.DirTriggerNum,"textSize",%textNum] = %pXml.attributeF32("size");
            %this.DirTrigger[%this.DirTriggerNum,"textDuration",%textNum] = %pXml.attributeF32("duration")*1000;  
            %this.DirTrigger[%this.DirTriggerNum,"imageid",%textNum]=%pXml.attributeS32("imageid");
            %this.DirTrigger[%this.DirTriggerNum,"hintValue",%textNum]=%pXml.attributeS32("hintvalue");
            %this.DirTrigger[%this.DirTriggerNum,"hintCondition",%textNum]=%pXml.attribute("hintcondition");            

            // Pause button?
            %havePauseButton = %pXml.attribute("pause");
            if(%havePauseButton !$= "true") %havePauseButton = false; else %havePauseButton = true;
            %this.DirTrigger[%this.DirTriggerNum,"pause",%textNum]=%havePauseButton;
            
            // Play minigame?
            %playMinigame = %pXml.attribute("playminigame");
            if(%playMinigame !$= "true") %playMinigame = false; else %playMinigame = true;
            %this.DirTrigger[%this.DirTriggerNum,"playMinigame",%textNum]=%playMinigame;

            %textNum++;         
         
            while(true)
            {
               if(%pXml.nextSiblingElement("bubble"))
               {
                  %this.DirTrigger[%this.DirTriggerNum,"text",%textNum] = %pXml.attribute("text");
                  %this.DirTrigger[%this.DirTriggerNum,"textSize",%textNum] = %pXml.attributeF32("size");
                  %this.DirTrigger[%this.DirTriggerNum,"textDuration",%textNum] = %pXml.attributeF32("duration")*1000;
                  %this.DirTrigger[%this.DirTriggerNum,"imageid",%textNum]=%pXml.attributeS32("imageid");
                  %this.DirTrigger[%this.DirTriggerNum,"hintValue",%textNum]=%pXml.attributeS32("hintvalue");
                  %this.DirTrigger[%this.DirTriggerNum,"hintCondition",%textNum]=%pXml.attribute("hintcondition");                  

                  // Pause button?                  
                  %havePauseButton = %pXml.attribute("pause");
                  if(%havePauseButton !$= "true") %havePauseButton = false; else %havePauseButton = true;
                  %this.DirTrigger[%this.DirTriggerNum,"pause",%textNum]=%havePauseButton;
                  
                  // Play minigame?
                  %playMinigame = %pXml.attribute("playminigame");
                  if(%playMinigame !$= "true") %playMinigame = false; else %playMinigame = true;
                  %this.DirTrigger[%this.DirTriggerNum,"playMinigame",%textNum]=%playMinigame;                  

                  %textNum++;
               }
               else
               {
                  %this.DirTrigger[%this.DirTriggerNum,"text",%textNum] = "";
                  break;               
               }
            }
            %pXml.popElement();
         }
         
         %this.showDirAni1(%this.DirTriggerNum);
            
         %this.DirTriggerNum++;            
      }
      
      // Go to next animation tag
      if(!%pXml.nextSiblingElement("ani"))
         break;
   }
   
   %pXml.popElement();
   
   %pXml.delete();   
   
   echo("Debug: XML loaded");
}

// Show director (stage 1: hide director chair)
function IntroGui::showDirAni1(%this, %triggerNum)
{  
   %this.DirTriggerImg = IntroDirTriggerImga;   
   %this.CurTriggerNum = %triggerNum;
   %this.showDirAni2(%triggerNum);
}

// Show director (stage 2: show director and bubble)
function IntroGui::showDirAni2(%this, %triggerNum)
{
   %path = "scriptsAndAssets/data/ui/ho/inoutro/";    
   switch$($CurSceneName)
   {
      case "SpyLevel":      %path = %path @ "directors/spy";
      case "GangsterLevel": %path = %path @ "directors/gangster";
      case "KungFuLevel":   %path = %path @ "directors/kungfu";
      case "MusicalLevel":  %path = %path @ "directors/musical";
      case "DinosaurLevel": %path = %path @ "directors/dinosaur";
      case "SciFiLevel":    %path = %path @ "directors/scifi";
      case "WesternLevel":  %path = %path @ "directors/western";
      case "MonsterLevel":  %path = %path @ "directors/monster";
   }
   %this.DirTriggerImg.setDML(%path @ %this.DirTrigger[%triggerNum,"imageid",0] @ ".png");
   %this.DirTriggerImg.Visible = true;
     
   // Scale factors when resolution is different than 800x600
   %winScaleX = 1.0 / 800 * getWord(IntroGuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(IntroGuiCenter.getExtent(),1);   

   %showTime = 500;
   %this.DirTriggerImg.setAlpha(0);
   %this.DirTriggerImg.setVisible(true);         
   %this.DirTriggerImg.animateAlpha(1, 0, 500, "0 0");         

   %this.schedule(%showTime + 200, "showDirAniText1", %triggerNum, 0);
}

// Vertically centers ML text in a bubble control
// We use a scheduling scheme to wait for Torque to update the control extent
// so we can compute the center position.
// %bubble = instance of bubble bitmap control
// %text = instance of Gui to be centered
// %origText = extent of %text
// %vOffset = a vertical offset to be added lastly to the text
// %arrowButtonY = vertical position of the arrow button, if presented
function IntroGui::vCenterTextInBubble(%this, %bubble, %text, %origExtent, %vOffset, %arrowButtonY)
{
   %bubble.setVisible(true);
   %text.setVisible(true);   
   
   /*%extent = %text.getExtent();   
   if(getWord(%extent,1) !$= getWord(%origExtent,1))
   {
      %centery = (getWord(%bubble.getExtent(),1) - getWord(%extent,1)) / 2;
      %newY = getWord(%bubble.getPosition(),1)+%centery + getWord(%extent,1)/27;      
      
      if(%vOffset !$= 0 && getWord(%extent,1)+%newY < %arrowButtonY)
         %vOffset = 0;
      %text.setPosition(getWord(%text.getPosition(),0), %newY+%vOffset);
      
      %bubble.setVisible(true);
      %text.setVisible(true);
   }
   else
      %this.schedule(50, "vCenterTextInBubble", %bubble, %text, %origExtent, %vOffset, %arrowButtonY);*/
}

// Show director bubble text
function IntroGui::showDirAniText1(%this, %triggerNum, %textNum)
{
   // Scale factors when resolution is different than 800x600
   %winScaleX = 1.0 / 800 * getWord(IntroGuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(IntroGuiCenter.getExtent(),1);
   
   // Check hint condition
   %hintvalue = %this.DirTrigger[%triggerNum,"hintvalue",%textNum];
   %hintcondition = %this.DirTrigger[%triggerNum,"hintcondition",%textNum];
   if(%hintcondition !$= "")
   {
      %skip = true;
      
      switch$(%hintcondition)
      {
         case "less":
            if(Minigame.hintsGained < %hintvalue)
               %skip = false;
         
         case "greater":
            if(Minigame.hintsGained > %hintvalue)
               %skip = false;
      }
      
      if(%skip)
      {
         %this.showDirAniText3(%triggerNum, %textNum, 0);
         return;
      }
   }
   
   %path = "scriptsAndAssets/data/ui/ho/inoutro/";
   switch$($CurSceneName)
   {
      case "SpyLevel":      %path = %path @ "directors/spy";
      case "GangsterLevel": %path = %path @ "directors/gangster";
      case "KungFuLevel":   %path = %path @ "directors/kungfu";
      case "MusicalLevel":  %path = %path @ "directors/musical";
      case "DinosaurLevel": %path = %path @ "directors/dinosaur";    
      case "SciFiLevel":    %path = %path @ "directors/scifi";
      case "WesternLevel":  %path = %path @ "directors/western";
      case "MonsterLevel":  %path = %path @ "directors/monster";  
   }
//   %this.DirTriggerImg.setDML(%path @ %this.DirTrigger[%triggerNum,"imageid",%textNum] @ ".png");
   
   %newDML = %path @ %this.DirTrigger[%triggerNum,"imageid",%textNum] @ ".png";
   %this.swapDirectors(%newDML, 200);

   %vOffset = 0;
   %arrowButtonY = 280;
   %pause = %this.DirTrigger[%triggerNum,"pause",%textNum];
   if(%pause) 
      %vOffset -= getWord(IntroDirTriggerArrow.getExtent(),1)/2;    

   %text = %this.DirTrigger[%triggerNum,"text",%textNum];
   %text = strreplace(%text, "%player%" , $CurPlayerName);
   
   %this.lastText = %text;
   %this.lastFontSize = %this.DirTrigger[%triggerNum,"textSize",%textNum];
   %this.lastTextOffset = %vOffset;
   %this.lastArrowButtonY = %arrowButtonY;
   
   //IntroDirTriggerBubbleText.setText("<just:center><font:Comic Sans MS:"@%this.DirTrigger[%triggerNum,"textSize",%textNum]@">"@%text);
   //%this.vCenterTextInBubble(IntroDirTriggerBubble, IntroDirTriggerBubbleText, %extent, %vOffset, %arrowButtonY);
   %this.updateSpeechBubble();

   if(%pause)
   {
       IntroDirTriggerArrow.setVisible(true);
       IntroDirTriggerArrow.triggerNum = %triggerNum;
       IntroDirTriggerArrow.textNum = %textNum;
   }
   else
   {
      IntroDirTriggerArrow.setVisible(false);
      %this.showDirAniText2(%triggerNum, %textNum);
   }
}

// Update the speech bubble text
function IntroGui::updateSpeechBubble(%this)
{
   //if(%this.lastFontSize $= "")
      //return;
   
   %text = %this.lastText;   
   %fontSize = %this.lastFontSize * Canvas.scaleSize;
   if(%fontSize > 62) %fontSize = 62;
   
   IntroDirTriggerBubbleText.setVisible(false);
   //IntroDirTriggerBubbleText.setExtent(getWord(IntroDirTriggerBubbleText.getExtent(),0),1);
   //%extent = IntroDirTriggerBubbleText.getExtent();   
   %vOffset      = %this.lastTextOffset;
   if(%vOffset !$= 0) %vOffset = 0; // -5
   %arrowButtonY = %this.lastArrowButtonY;
   
   //IntroDirTriggerBubbleText.setText("<just:center><font:Arial:"@%fontSize@">"@%text);

   IntroDirTriggerBubbleText.setText(%text);
   
   %this.vCenterTextInBubble(IntroDirTriggerBubble, IntroDirTriggerBubbleText, %extent, %vOffset, %arrowButtonY);
}

// Second part of showDirAniText2
function IntroGui::showDirAniText2(%this, %triggerNum, %textNum)
{
   sfxPlay(AudioButtonPress); //Button Press Sound
   
   // Go directly to minigame or exit
   if(%this.skip)
   {
      // Do we have a minigame at the end?
      do 
      {
         if(%this.dirTrigger[%triggerNum,"playMinigame",%textNum])
         {
            %this.playMinigame();
            %this.triggerNum = %triggerNum;
            %this.textNum = %textNum;
            return;            
         }
         %textNum++;
      } while(%this.DirTrigger[%triggerNum,"text",%textNum] !$= "")
   }
   
   %classID = "HO";   

   %duration = %this.DirTrigger[%triggerNum,"textDuration",%textNum];
   if(%duration < 0)
      %duration = 0;

   if(%this.DirTrigger[%triggerNum,"pause",%textNum])
      %duration = 0;
      
   // Play minigame?
   if(%this.dirTrigger[%triggerNum,"playMinigame",%textNum])
   {
      %this.playMinigame();
      %this.triggerNum = %triggerNum;
      %this.textNum = %textNum;
      return;
   }
      
   %this.showDirAniText3(%triggerNum, %textNum, %duration);
}

function IntroGui::showDirAniText3(%this, %triggerNum, %textNum, %duration)
{
   if(%this.DirTrigger[%triggerNum,"text",%textNum+1] !$= "")
   {
      // Show next text
      %this.schedule(%duration, "showDirAniText1", %triggerNum, %textNum+1);
   }
   else
   {
      // End of text tags
      %this.schedule(%duration, "hideDirAni", %triggerNum);
   }   
}

// Hide director
function IntroGui::hideDirAni(%this, %triggerNum)
{
   %this.lastFontSize = "";
   IntroDirTriggerArrow.setVisible(false);  
   IntroDirTriggerBubble.Visible = false;
   IntroDirTriggerBubbleText.Visible = false;
     
   %showTime = 500;
   %this.DirTriggerImg.setAlpha(1);
   %this.DirTriggerImg.setVisible(true);         
   %this.DirTriggerImg.animateAlpha(0, 0, %showTime, "0 0");
   
   %this.exit();
}

function IntroGui::onResize(%this, %width, %height)
{
   %fontSize = 36 * Canvas.scaleSize; if(%fontSize > 62) %fontSize = 62;
   GuiLCAButtonProfile2.fontSize = %fontSize;

   IntroGuiExitButton.setProfile(GuiDefaultProfile);      
   IntroGuiExitButton.setProfile(GuiLCAButtonProfile2);
   
   %this.updateSpeechBubble();
   
   Canvas.setWidescreen(IntroGuiCenter, 2000, true);
}

// Play minigame during intro screen
function IntroGui::playMinigame(%this)
{
   sfxStopAll();   
   SceneGui.inIntro = true;
   if(isObject(Minigame))
      Minigame.delete();
   exec("scriptsAndAssets/client/ui/Minigame.gui");
   IntroGui.add(Minigame);
   IntroGui.pushToBack(Minigame);
   
   Minigame.play();
   
   %path = "scriptsAndAssets/data/ui/clapperboard/clapper.jpg";
   IntroClapper.setDML(%path);    
   IntroClapper.rewind();

   %path = "scriptsAndAssets/data/ui/clapperboard/minigame/" @ $CurSceneName @ "_take" @ $CurTake @ ".jpg";
   IntroBoard.setDML(%path);        
   IntroBoard.rewind();

   IntroClapper.setVisible(true);
   IntroBoard.setVisible(true);
   
   IntroClapper.play(1);
   IntroBoard.play(1);

   %this.schedule(1700, "startLevel", true);
   
   sfxPlay(AudioClapperBoardMoving);
   
   %this.schedule(1600, "playClapperBoardCloseSound", 0);
}

function IntroClapper::onStoppedAtEnd(%this)
{   
   IntroGui.playClapperBoardCloseSound();
   IntroGui.schedule(400, "startLevel", SceneGui.inIntro);
}

// Triggers a cross-fading effect between current director's talking pose and next pose
// %newDML is the path of the new pose image
// %aniTime Crossfading time, in ms
function IntroGui::swapDirectors(%this, %newDML, %aniTime)
{
   if(%this.DirTriggerImg $= IntroDirTriggerImga)
   {
      %objOrig = IntroDirTriggerImga;
      %objDest = IntroDirTriggerImgb;
   }
   else
   {
      %objOrig = IntroDirTriggerImgb;
      %objDest = IntroDirTriggerImga;
   }
   
   %objDest.setDML(%newDML);
   if(%objOrig.DML $= %objDest.DML)
      return;   

   %objOrig.setAlpha(1);
   %objOrig.setVisible(true);
   %objDest.setAlpha(0);
   %objDest.setVisible(true);   

   %this.DirTriggerImg = %objDest;
   %this.crossFadingDirector = true;
   %objOrig.animateAlpha(0, 0, %aniTime, "0 0");
   %objDest.animateAlpha(1, 0, %aniTime, "0 0");
   scheduleObjLCA(%this, "HO", %aniTime, "onEndDirectorCrossFading", 0);   
}

// Called when the crossfading effect of director's finishes
function IntroGui::onEndDirectorCrossFading(%this)
{
   %this.crossFadingDirector = false;
}
