// Initialize minigame particle effects manager
function MinigameParticleFx::onWake(%this)
{
   %this.aboutToExit = false;
   %this.timerFadeIn = 0;
   
   %this.particleMax = 1000;
   %this.particleInUse = -1;
   %this.particleIdxTable = 0;   
   
   // Create particles
   for(%i=0; %i<%this.particleMax; %i++)
   {
      %obj = ("MinigameP"@%i);
      if(isObject(%obj))
         %obj.delete();

      %newP = new GuiPlasticBitmapCtrl(%obj);

      %newP.useAddBlending = "1";         
      %newP.setVisible(false);
      MinigameParticleFx.add(%newP);
   }
   
   %this.particleCircleMax = 50;
   %this.particleCircleInUse = -1;
   %this.particleCircleIdxTable = 0;      
   
   // Create particles for animated circles
   for(%i=0; %i<50; %i++)
   {
      %obj = ("MinigameCircle"@%i);
      if(!isObject(%obj))      
         %newP = new GuiPlasticBitmapCtrl(%obj);
      else
         %newP = %obj;
      %newP.useAddBlending = "1";         
      %newP.setVisible(false);
      MinigameCircles.add(%newP);
   }   
}

// Returns the ID of a particle
function MinigameParticleFx::getAvailableParticle(%this)
{     
   %this.particleInUse++;
   
   return %this.particleInUse % %this.particleMax;
}

// Hide sparkle when end of animation is reached
function MinigameParticleFx::onDeathGroup(%this, %idxTable, %idxNum)  
{
   for(%i=0; %i<%idxNum; %i++)
      ("MinigameP"@%this.particleTable[%idxTable, %i]).setVisible(false);
}

// Hide particle when end of animation is reached
function MinigameParticleFx::onDeath(%this, %idx)  
{
   ("MinigameP"@%idx).setVisible(false);
}

// Returns the ID of a circle particle
function MinigameParticleFx::getAvailableCircle(%this)
{     
   %this.particleCircleInUse++;
   
   return %this.particleCircleInUse % %this.particleCircleMax;
}

// Hide animated circle when end of animation is reached
function MinigameParticleFx::onDeathCircle(%this, %idx)  
{
   ("MinigameCircle"@%idx).setVisible(false);
}

function MinigameParticleFx::ConfettiFx(%this, %aniTime)
{
   if(Minigame.hintsGained > 0 && Minigame.hintsGained < 6)
   {
      %hintsGained = mFloor(Minigame.hintsGained);
      
      HintNumber.setDML("scriptsAndAssets/data/ui/minigame/wineffect/hint_number/"@%hintsGained@".png");
      
      %minigameNum = $MinigameTable[$CurSceneName, $CurTake];
      %highScoreVector = PlayerProfile.getFieldValue("MinigameStars"@$CurPlayerNum);
      
      %highScore = getWord(%highScoreVector, %minigameNum-1);
      
      if(%hintsGained > %highScore)
      {
         %highScore = %hintsGained;      
         %newHighScoreVector = setWord(%highScoreVector, %minigameNum-1, %highScore);
         PlayerProfile.setFieldValue("MinigameStars"@$CurPlayerNum, %newHighScoreVector);
         SavePlayerProfile();
      }
   }
   
   if(PlayMinigameDlg.isPlayingMinigame $= true)
      WinText.setDML("scriptsAndAssets/data/ui/minigame/wineffect/win_text2.png");
   else
      WinText.setDML("scriptsAndAssets/data/ui/minigame/wineffect/win_text.png");   
   WinText.setVisible(true);
   HintNumber.setVisible(true);

   %this.timerFadeIn = 0;
   
   scheduleObjLCA(%this, "MinigameParticle", 1000, "FadeInScore", 0);
   
   MinigameCircles.setVisible(true);
//   Minigame.pushToBack(MinigameCircles);
   
   %scalex = 1.0 / 800 * getWord(MinigameRefCenter.getExtent(),0);
   %scaley = 1.0 / 600 * getWord(MinigameRefCenter.getExtent(),1);   
   
   %this.setVisible(true);
//   Minigame.pushToBack(%this);
   
   %offsetX = 45;
   
   sfxPlay(MinigameWinCheering);
   sfxPlay(MinigameWinFireworks);
   
   scheduleObjLCA(%this, "MinigameParticle", 100, "ConfettiFxHelper", 5, 0-%offsetX, 306, %aniTime, 1, true);
   scheduleObjLCA(%this, "MinigameParticle", 500, "ConfettiFxHelper", 5, 0-%offsetX, 403, %aniTime, 1, true);
   scheduleObjLCA(%this, "MinigameParticle", 900, "ConfettiFxHelper", 5, 0-%offsetX, 502, %aniTime, 1, true);

   scheduleObjLCA(%this, "MinigameParticle", 100, "ConfettiFxHelper", 5, 800+%offsetX, 306, %aniTime, 1, false);
   scheduleObjLCA(%this, "MinigameParticle", 500, "ConfettiFxHelper", 5, 800+%offsetX, 403, %aniTime, 1, false);
   scheduleObjLCA(%this, "MinigameParticle", 900, "ConfettiFxHelper", 5, 800+%offsetX, 502, %aniTime, 1, false);

   // Show animated circles
   for(%i=0; %i<10; %i++)
      %this.AnimateCircle();
      
   scheduleObjLCA(%this, "MinigameParticle", (%aniTime-2)*1000, "stopAnimations", 0);
}

//Fade in effect for the score in confetti effect
function MinigameParticleFx::FadeInScore(%this)
{
   if(%this.timerFadeIn > 9){
      WinText.setAlpha(1);
      HintNumber.setAlpha(1);
      return;
   }
      
   %this.timerFadeIn++;
   
   %alpha = %this.timerFadeIn / 10;
   
   WinText.setAlpha(%alpha);
   HintNumber.setAlpha(%alpha);
   
   scheduleObjLCA(%this, "MinigameParticle", 100, "FadeInScore", 0);
}

// Stop all animations and exit
function MinigameParticleFx::stopAnimations(%this)
{
   %this.aboutToExit = true;
   
   // Fade out particles
   for(%i=0; %i<%this.particleMax; %i++)
   {
      %obj = ("MinigameP"@%i);
      if(isObject(%obj))
      {
         %obj.animateAlpha(0, 0, 1000, "0 0");
      }
   }
   
   // Fade out animated circles
   for(%i=0; %i<50; %i++)
   {
      %obj = ("MinigameCircle"@%i);
      if(isObject(%obj))
      {
         %obj.animateAlpha(0, 0, 1000, "0 0");         
      }
   }   
   
   cancelSchedulesLCA("MinigameParticle");
   scheduleObjLCA(%this, "MinigameParticle", 1000, "exit", 0); 
}

function MinigameParticleFx::exit(%this)
{
   %this.setVisible(false);
   MinigameCircles.setVisible(false);
   
   if(isObject(WinText))    WinText.setVisible(false);
   if(isObject(HintNumber)) HintNumber.setVisible(false);
   if(isObject(WinText))    WinText.setAlpha(0);
   if(isObject(HintNumber)) HintNumber.setAlpha(0);
}

function MinigameParticleFx::AnimateCircle(%this)
{
   if(%this.aboutToExit)
      return;
      
   %idx = %this.getAvailableCircle();

   %scalex = 1.0 / 800 * getWord(MinigameRefCenter.getExtent(),0);
   %scaley = 1.0 / 600 * getWord(MinigameRefCenter.getExtent(),1);
   
   %sizeX = 300;
   %sizeY = 300;
   
   // Select window side
   %origSide = getRandom(0,3);
   switch$(%origSide)
   {
      // Top
      case 0: %origX = getRandom(0-%sizeX, 800+%sizeX); %origY = -%sizeY;
      // Left
      case 1: %origX = -%sizeX; %origY = getRandom(0-%sizeY, 600+%sizeY);
      // Bottom
      case 2: %origX = getRandom(0-%sizeX, 800+%sizeX); %origY = 600+%sizeY;
      // Right
      case 3: %origX = 800+%sizeX; %origY = getRandom(0-%sizeY, 600+%sizeY);
   }
   
   %destSide = %origSide;
   while(%destSide $= %origSide)
      %destSide = getRandom(0,3);
      
   switch$(%destSide)
   {
      // Top
      case 0: %destX = getRandom(0-%sizeX, 800+%sizeX); %destY = -%sizeY;
      // Left
      case 1: %destX = -%sizeX; %destY = getRandom(0-%sizeY, 600+%sizeY);
      // Bottom
      case 2: %destX = getRandom(0-%sizeX, 800+%sizeX); %destY = 600+%sizeY;
      // Right
      case 3: %destX = 800+%sizeX; %destY = getRandom(0-%sizeY, 600+%sizeY);
   }      
      
   // Determine vector size
   %vecX = %destX - %origX;
   %vecY = %destY - %origY;
   %size = mSqrt(%vecX*%vecX+%vecY*%vecY);
   
   %aniTime = %size / 500;   

   %obj = ("MinigameCircle"@%idx);
   %obj.setVisible(true);
   %obj.setAlpha(1);
   %obj.setPosition(%origX*%scaleX, %origY*%scaleY);
//   %obj.setPosition(400*%scaleX, 300*%scaleY);
   %obj.setExtent(%sizeX*%scaleX, %sizeY*%scaleY);
   %obj.stop();   
   %obj.rewind();
   %obj.useAddBlending = "1";
   
   %path = "scriptsAndAssets/data/ui/minigame/wineffect/light" @ getRandom(1,6) @ ".png";
	%obj.setDML(%path);  
      
   %obj.animateCenter(%destX-(%sizeX/2), %destY-(%sizeY/2), 0, %aniTime*1000, "0 0");
      
   scheduleObjLCA(%this, "MinigameParticle", %aniTime*1000, "onDeathCircle", 1, %idx);
      
   // Re-trigger function call
   scheduleObjLCA(%this, "MinigameParticle", 1000, "AnimateCircle", 0);
}

// Produces a confetti effect ejected from (%x,%y) to the right or left side of the screen
// %aniTime is the total animation time, in seconds
// %scale is a scale factor of the confetti
// %goToRight should be true if particles come from left to right
function MinigameParticleFx::ConfettiFxHelper(%this, %x, %y, %aniTime, %scale, %goToRight)
{   
   %scalex = %scale * (1.0 / 800 * getWord(MinigameRefCenter.getExtent(),0));
   %scaley = %scale * (1.0 / 600 * getWord(MinigameRefCenter.getExtent(),1));
   
   setRandomSeed(getRealTime());   
   
   %this.particleIdxTable++;
   %this.particleIdxNum[%this.particleIdxTable] = 0;   
     
   for(%i=0; %i<70; %i++)
   {
      %idx = %this.getAvailableParticle();
         
      %this.particleTable[%this.particleIdxTable, %this.particleIdxNum[%this.particleIdxTable]] = %idx;
      %this.particleIdxNum[%this.particleIdxTable]++;         
         
      ("MinigameP"@%idx).particleConfetti(%x*%scaleX, %y*%scaleY, %aniTime, %scalex, %scaley, %goToRight);
   }
   
   %particleIdx = %this.particleIdxTable;
   %particleNum = %this.particleIdxNum[%this.particleIdxTable];

   scheduleObjLCA(%this, "MinigameParticle", %aniTime*1000, "onDeathGroup", 2, %particleIdx, %particleNum);
}
