// Audio profiles for the keypad minigame
new SFXProfile(AudioKeypad0)
{
   filename = "~/data/ui/minigame/spy/buttons/SPY_fx_keypad_3b.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioKeypad1)
{
   filename = "~/data/ui/minigame/spy/buttons/SPY_fx_keypad_1c.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioKeypad2)
{
   filename = "~/data/ui/minigame/spy/buttons/SPY_fx_keypad_2a.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioKeypad3)
{
   filename = "~/data/ui/minigame/spy/buttons/SPY_fx_keypad_3b.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioKeypad4)
{
   filename = "~/data/ui/minigame/spy/buttons/SPY_fx_keypad_4a.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioKeypad5)
{
   filename = "~/data/ui/minigame/spy/buttons/SPY_fx_keypad_5a.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioKeypad6)
{
   filename = "~/data/ui/minigame/spy/buttons/SPY_fx_keypad_6a.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioKeypad7)
{
   filename = "~/data/ui/minigame/spy/buttons/SPY_fx_keypad_7b.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioKeypad8)
{
   filename = "~/data/ui/minigame/spy/buttons/SPY_fx_keypad_8b.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioKeypad9)
{
   filename = "~/data/ui/minigame/spy/buttons/SPY_fx_keypad_9b.wav";
   description = LCASoundEffects;
   preload = true;
};


// Called when minigame GUI shows up
function KeypadMinigame::onWake(%this)
{
   // Please move the code here to start()
   
   // Force resizing
   %this.setExtent(getWord(Canvas.extent,0), getWord(Canvas.extent,1));
}

// Called by miniGame.cs
function KeypadMinigame::start()
{
   $KeypadSequenceNumber = 0;
   $KeypadLevel = 1;
   $KeypadReady = false;
   $KeypadWrongAnswers = 0;
   $KeypadCorrectAnswers = 0;
   KeypadDummy.setVisible(true);
   
//   $KeypadScore = 10;
   $KeypadScore1 = 0;
//   $KeypadScore2 = 0;
//   $KeypadScore3 = 0;
   
   // Choose a random number
   $KeypadCurSequence = "";
   $KeypadDigits = 2;
   for(%i=0; %i < $KeypadDigits; %i++)
      $KeypadCurSequence = $KeypadCurSequence @ getRandom(0,9);
   
   KeypadMinigame::NewSequence(true);
}

// Starts a new Simon memory game sequence
function KeypadMinigame::NewSequence(%appendDigit)
{
   if(%appendDigit)
   {
      // Append a random number
      $KeypadCurSequence = $KeypadCurSequence @ getRandom(0,9);
      $KeypadDigits++;
   }

   $KeypadTypedDigits = 0;

   if(%appendDigit $= false)
      KeypadPanelText.setText("<just:center><font:Arial:42><color:FFFFFF>Let's try again...");      
   else
   {
      if($KeypadSequenceNumber $= 0)
         KeypadPanelText.setText("<just:center><font:Arial:42><color:FFFFFF>Pay attention to the first sequence...");
      else
         KeypadPanelText.setText("<just:center><font:Arial:42><color:FFFFFF>Pay attention to the next sequence...");
   }
   
   // Wait 2 seconds before hiding director
   //scheduleLCA("Minigame", 2000, 0, "KeypadMinigame::SimonDigitsPart1", 1, 1);
   scheduleLCA("Minigame", 2000, 0, "KeypadMinigame::SimonDigitsPartOne", 1, 1);   
}

// Blinks a digit of the current sequence (part 1)
function KeypadMinigame::SimonDigitsPartOne(%digitToShow)
{
   %curDigit = getSubStr($KeypadCurSequence, %digitToShow-1, 1);
   if(%curDigit >= 0 && %curDigit <= 9)
      sfxPlayOnce(AudioKeypad@%curDigit);
   
   KeypadPanelText.setText("<just:center><font:Arial:60><color:FFFFFF>" @ %curDigit);   
   ("KeypadOvl"@%curDigit).setVisible(true);
     
   scheduleLCA("Minigame", 1500, 0, "KeypadMinigame::SimonDigitsPartTwo", 1, %digitToShow);   
}

// Blinks a digit of the current sequence (part 2)
function KeypadMinigame::SimonDigitsPartTwo(%digitToShow)
{
   %curDigit = getSubStr($KeypadCurSequence, %digitToShow-1, 1);
   ("KeypadOvl"@%curDigit).setVisible(false);
      
   KeypadPanelText.setText("");
   
   if(%digitToShow $= $KeypadDigits)
      scheduleLCA("Minigame", 200, 0, "KeypadMinigame::SimonDigitsPartThree", 0);
   else
      scheduleLCA("Minigame", 150, 0, "KeypadMinigame::SimonDigitsPartOne", 1, %digitToShow+1);   
}

// Blinks a digit of the current sequence (part 3)
function KeypadMinigame::SimonDigitsPartThree()
{
   KeypadPanelText.setText("<just:center><font:Arial:50><color:FFFFFF>Go!");
   
   scheduleLCA("Minigame", 500, 0, "KeypadMinigame::GetReady", 0);
}

// Hide the director and let the player play
function KeypadMinigame::GetReady()
{
   KeypadPanelText.setText("");
   
   $KeypadReady = true;
   
   KeypadDummy.setVisible(false);

   $KeypadTypedNumber = "";
}

// Called when clicking on keypad buttons
function KeypadPlay(%number)
{
   if(!$KeypadReady)
      return;
      
   if(%number >= 0 && %number <= 9)
      sfxPlayOnce(AudioKeypad@%number);
      
   // Concatenate number
   $KeypadTypedNumber = $KeypadTypedNumber @ %number;

   $KeypadTypedDigits++;
   
   %testNumber = getSubStr($KeypadCurSequence, 0, $KeypadTypedDigits);
   if(%testNumber !$= $KeypadTypedNumber)
   {
      $KeypadReady = false;
      KeypadDummy.setVisible(true);
      KeypadMinigame::ShowResult(false);
      return;
   }

   // Have we finished?      
   if($KeypadTypedDigits $= $KeypadDigits)
   {
      $KeypadReady = false;
      KeypadDummy.setVisible(true);
      KeypadMinigame::ShowResult(true);
   }
}

// Show result of last sequence (wrong, well done)
function KeypadMinigame::ShowResult(%isCorrect)
{
   %appendDigit = true;
   
   if(%isCorrect)
   {
      $KeypadScore1++;
/*
      if($KeypadCorrectAnswers < 1)
         $KeypadScore1 += $KeypadScore;
      else
      if($KeypadCorrectAnswers < 3)
         $KeypadScore2 += $KeypadScore;
      else
      if($KeypadCorrectAnswers < 4)
         $KeypadScore3 += $KeypadScore;
*/
      $KeypadCorrectAnswers++;
     
      $KeypadLevel++;
/*
      if($KeypadScore $= 10)
         $KeypadScore = 50;
      else
         $KeypadScore *= 2;
*/     
      KeypadPanelText.setText("<just:center><font:Arial:50><color:FFFFFF>Well done!");
      
      KeypadTopScoreBar.setBitmap(
         "scriptsAndAssets/data/ui/minigame/spy/scorebars/greenscorebar" @ 
         $KeypadLevel-1);      
   }
   else
   {
      $KeypadWrongAnswers++;
      KeypadPanelText.setText("<just:center><font:Arial:50><color:FFFFFF>Wrong!");
         
      KeypadBottomScoreBar.setBitmap(
         "scriptsAndAssets/data/ui/minigame/spy/scorebars/redscorebar" @ 
         $KeypadWrongAnswers); 
         
      if($KeypadWrongAnswers > 2)
      {
         // Finish earlier
         scheduleLCA("Minigame", 1500, 0, "KeypadMinigame::quit", 0);         
         return;
      }
         
      %appendDigit = false;
   }

   $KeypadSequenceNumber++;

   // Wait 1.5 seconds until showing next sequence
   if($KeypadLevel-1 > 5){
      //scheduleLCA("Minigame", 1500, 0, "KeypadMinigame::quit", 0);
      sfxStopAll();
      
      // Play confetti
      MinigameParticleFx.ConfettiFx(15); // Play confetti for 15 secs
      // Quit a bit earlier
      scheduleLCA("Minigame", 12*1000, 0, "KeypadMinigame::quit", 0);
      
   }else
      scheduleLCA("Minigame", 1500, 0, "KeypadMinigame::NewSequence", 1, %appendDigit);
}

// Quit minigame
function KeypadMinigame::quit()
{
   sfxSopAll();
   cancelSchedulesLCA("Minigame");
   MiniGame.playClapperBoardScoreIn();
}

//Functions for the Exit Button
function KeypadExitButton::onMouseEnter(%this){
   %this.setBitmap("scriptsAndAssets/data/ui/minigame/exit_select.png");
}

function KeypadExitButton::onMouseLeave(%this){
   %this.setBitmap("scriptsAndAssets/data/ui/minigame/exit.png");
}

