function SimonMinigame::onWake(%this)
{
   exec("~/data/ui/minigame/simon/"@playMinigameDlg.currentLevel@$CurTake@"/SimonConfig.cs");
   
   // Set skip button
   if(($CurTake $= 1 && playMinigameDlg.currentLevel $= "MusicalLevel") ||
      ($CurTake $= 4 && playMinigameDlg.currentLevel $= "MusicalLevel") ||
      ($CurTake $= 2 && PlayMinigameDlg.currentLevel $= "WesternLevel"))
      SimonSkipButton.setBitmap("scriptsAndAssets/data/ui/minigame/quit.png");
   else   
      SimonSkipButton.setBitmap("scriptsAndAssets/data/ui/minigame/skip.png");

   %this.rightSound = new SFXProfile()
   {
      filename = "~/data/ui/minigame/simon/right";
      description = "AudioGui";
      preload = true;
   };

   %this.wrongSound = new SFXProfile()
   {
      filename = "~/data/ui/minigame/simon/wrong";
      description = "AudioGui";
      preload = true;
   };

    %this.gameFinished = false;
   
   %this.numOfButtons = 0;
   
   %this.curSeq = -1;
   
   %this.curSeqShowInd = 1;
   %this.curSeqPlayInd = -1;
   
   SimonMinigame.initSpecificGui();
   
   %this.pushToBack(SimonSkipButton);
   // Force resizing
   %this.setExtent(getWord(Canvas.extent,0), getWord(Canvas.extent,1));
   Canvas.setWidescreen(MinigameRefCenter, 0, false);
}

// Callback for resizing events
function SimonMinigame::onResize(%this, %width, %height)
{
   Canvas.setWidescreen(MinigameRefCenter, 2000, true);
}

function SimonMinigame::start(%this)
{
   scheduleObjLCA(%this, "Minigame", 1000, "nextSequence", 0);
}

function SimonMinigame::addButton(%this, %x, %y, %width, %height, %soundFile, %pressedBitmap)
{
   %sound = new SFXProfile()
   {
      filename = %soundFile;
      description = "AudioGui";
      preload = true;
   };

   %image = new GuiBitmapCtrl() {
      canSaveDynamicFields = "1";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = %x SPC %y;
      Extent =  %width SPC %height;
      MinExtent = "2 2";
      canSave = "1";
      Visible = "0";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      lockMouse = "0";
      groupNum = "-1";
      buttonType = "PushButton";
      useMouseEvents = "0";
      bitmap = %pressedBitmap;
      command = "SimonMinigame.pressButton("@%this.numOfButtons@");";
   };

   %this.buttons[%this.numOfButtons] = new GuiBitmapButtonCtrl() {
      canSaveDynamicFields = "1";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = %x SPC %y;
      Extent =  %width SPC %height;
      MinExtent = "2 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      lockMouse = "0";
      groupNum = "-1";
      buttonType = "PushButton";
      useMouseEvents = "0";
      bitmap = "";
      command = "SimonMinigame.pressButton("@%this.numOfButtons@");";
         image = %image;
         sound = %sound;
   };
   
   MinigameRefCenter.add(%image);
   MinigameRefCenter.add(%this.buttons[%this.numOfButtons]);

   %this.numOfButtons++;
}

function SimonMinigame::pressButton(%this, %btInd)
{
   if (%btInd < 0)
   {
      if (%btInd == -10)
      {
         %this.curSeqPlayInd = -%this.curSeqPlayInd;
         %this.onSeqShowEnd();
         return;
      }
         
      %btInd = %this.seq[%this.curSeqShowInd];
      if (%btInd $= "")
      {
         %btInd = getRandom(%this.numOfButtons - 1);
         %this.seq[%this.seqSize + %this.curSeqInd] = %btInd;
      } 
      
      if (%this.curSeqShowInd == %this.lastOfCurSeq)
         scheduleObjLCA(%this, "Minigame", 1000, "pressButton", 1, -10); 
      else
      {
         scheduleObjLCA(%this, "Minigame", SimonMinigame.seqTime[%this.curSeqShowInd] $= ""? 
            SimonMinigame.defaultInterval : SimonMinigame.seqTime[%this.curSeqShowInd], "pressButton", 1, -1);
         %this.curSeqShowInd++;
      }
   }
   else
   {
      if (%this.curSeqPlayInd < 0)
         return;
         
      if (%this.seq[%this.curSeqPlayInd] != %btInd)
      {
         %this.curSeqPlayInd = - %this.firstOfCurSeq;
         sfxPlay(%this.wrongSound);
         %this.curSeq--;
         scheduleObjLCA(%this, "Minigame", 1000, "nextSequence", 0);
         return;
      }
      else
      {
         if (%this.curSeqPlayInd++ > %this.lastOfCurSeq)
         {
            %this.curSeqPlayInd = -%this.curSeqPlayInd;
            
            scheduleObjLCA(%this.meter, "Minigame", 1500, "increaseMeter", 1, SimonMinigame.barsGainedPerSequence);            
            
            scheduleObjLCA(%this, "Minigame", 2500, "nextSequence", 0);
         }
      }
  }

   %button = %this.buttons[%btInd];
   sfxPlay(%button.sound);
   
   %button.image.setVisible(true);
   scheduleObjLCA(%button.image, "Minigame", 250, "setVisible", 1, false);
   
   %this.onButtonPressed(%btInd);
}

function SimonMinigame::onButtonPressed(%this, %btInd)
{
}

function SimonMinigame::nextSequence(%this) 
{
   if (%this.seqSize[%this.curSeq++] $= "")
   {
      %this.gameFinished = true;
      %this.quit();
      return;     
   }

   %this.firstOfCurSeq = 1;
   if (!SimonMinigame.sameStart)
      for (%i = 0; %i < %this.curSeq; %i++)
      {
         %this.firstOfCurSeq += %this.seqSize[%i];
      }
   
   %this.lastOfCurSeq = %this.firstOfCurSeq + %this.seqSize[%this.curSeq] - 1;
   
   %this.curSeqShowInd = %this.firstOfCurSeq;
   %this.curSeqPlayInd = -%this.firstOfCurSeq;
    
   %this.onSeqShowStart(); 
   %this.pressButton(-1);
}

// Quit minigame
function SimonMinigame::quit(%this)
{
   lockMouse(false);
   Canvas.showCursor();   
   
   sfxStopAll();
   
   // Widescreen support
   if(isObject(MinigameCenter))
   {
      Minigame.bringToFront(%this);
      MinigameCenter.setVisible(true);
      Minigame.pushToBack(MinigameCenter);   
   }
   
   // Number of hints won
   Minigame.hintsGained = %this.meter.getHintsGained(); 
         
   if(mFloor(Minigame.hintsGained) > 0 && %this.gameFinished /*&& PlayMinigameDlg.isPlayingMinigame !$= true*/)
   {
      if(!%this.ConfettiIsPlaying)
      {
         if(isObject(PlayMinigameDlg))
         {            
            PlayMinigameDlgCenter.pushToBack(MinigameCircles);
            PlayMinigameDlgCenter.pushToBack(MinigameParticleFx);
            PlayMinigameDlgCenter.pushToBack(WinText);
            PlayMinigameDlgCenter.pushToBack(HintNumber);
            PlayMinigameDlgCenter.pushToBack(PMClapper);
            PlayMinigameDlgCenter.pushToBack(PMBoard);
            PlayMinigameDlgCenter.pushToBack(PMClapperStart);   
                     
            PlayMinigameDlg.bringToFront(%this);
            
            PlayMinigameDlg.setVisible(true);            
            PlayMinigameDlgCenter.setVisible(true);
         }
            
         // Play confetti
         MinigameParticleFx.ConfettiFx(15); // Play confetti for 15 secs
         %this.ConfettiIsPlaying = true;

         if(PlayMinigameDlg.isPlayingMinigame $= true)
            scheduleObjLCA(playMinigameDlg, "Minigame", 15*1000, "quitMinigame", 0);
         else
            // Quit a bit earlier
            scheduleObjLCA(playMinigameDlg, "Minigame", 12*1000, "quitMinigame", 0);
      }
   }
   else
   {
      Minigame.hintsGained = 0;
      
      if(isObject(PlayMinigameDlg))
         PlayMinigameDlg.bringToFront(PlayMinigameDlg.curMinigame);
      
      playMinigameDlg.quitMinigame();
   }
}

function SimonSkipButton::onMouseEnter(%this){
   if(($CurTake $= 1 && playMinigameDlg.currentLevel $= "MusicalLevel") ||
      ($CurTake $= 4 && playMinigameDlg.currentLevel $= "MusicalLevel") ||
      ($CurTake $= 2 && PlayMinigameDlg.currentLevel $= "WesternLevel"))
      %this.setBitmap("scriptsAndAssets/data/ui/minigame/quit_rollover.png");
   else
      %this.setBitmap("scriptsAndAssets/data/ui/minigame/skip_rollover.png");
}

function SimonSkipButton::onMouseLeave(%this){
   if(($CurTake $= 1 && playMinigameDlg.currentLevel $= "MusicalLevel") ||
      ($CurTake $= 4 && playMinigameDlg.currentLevel $= "MusicalLevel") ||
      ($CurTake $= 2 && PlayMinigameDlg.currentLevel $= "WesternLevel"))
      %this.setBitmap("scriptsAndAssets/data/ui/minigame/quit.png");
   else   
      %this.setBitmap("scriptsAndssets/data/ui/minigame/skip.png");
}

function SimonSkipButton::onMouseDown(%this){
   if(($CurTake $= 1 && playMinigameDlg.currentLevel $= "MusicalLevel") ||
      ($CurTake $= 4 && playMinigameDlg.currentLevel $= "MusicalLevel") ||
      ($CurTake $= 2 && PlayMinigameDlg.currentLevel $= "WesternLevel"))
      %this.setBitmap("scriptsAndAssets/data/ui/minigame/quit_pressed.png");
   else   
      %this.setBitmap("scriptsAndAssets/data/ui/minigame/skip_pressed.png");
}
