function PicResortMinigame::onWake(%this)
{
   exec("~/data/ui/minigame/pic_resort/"@playMinigameDlg.currentLevel@"/PicResortConfig.cs");
   
   // Set skip button
   //if($CurTake $= 4 && playMinigameDlg.currentLevel $= "SciFiLevel")
   //   PicResortSkipButton.setBitmap("scriptsAndAssets/data/ui/minigame/quit.png");
   //else   
      PicResortSkipButton.setBitmap("scriptsAndAssets/data/ui/minigame/skip.png");      

   %this.onResizeHelper();   
   
   %x0 = %this.x0;
   %y0 = %this.y0;
   %pieceWidth = %this.pieceWidth;
   %pieceHeight = %this.pieceHeight;

   %extent = mRound(%pieceWidth) SPC mRound(%pieceHeight);

   for (%i = 0; %i < %this.numOfRows; %i++)
      for (%j = 0; %j < %this.numOfCols; %j++)
      {
         %pieceBitmap = new GuiBitmapCtrl() {
            canSaveDynamicFields = "0";
            Enabled = "1";
            isContainer = "0";
            HorizSizing = "relative";
            VertSizing = "relative";
            Position = (%x0 + mRound(%j*%pieceWidth)) SPC (%y0 + %i*%pieceHeight);
            Extent =  %extent;
            MinExtent = "2 2";
            canSave = "1";
            Visible = "1";
            hovertime = "1000";
            alpha = "1";
            color = "255 255 255 255";
            unitClip = "0 0 1 1";
            bitmap = "scriptsAndAssets/data/ui/minigame/pic_resort/"@playMinigameDlg.currentLevel@"/pieces/"@(%i*%this.numOfCols + %j);
            wrap = "0";
            index = (%i*%this.numOfCols + %j);
      };
   
      %this.pieceMat[%i, %j] = %pieceBitmap;   
      MinigameRefCenter.add(%pieceBitmap);
   }
   
   for (%k = 0; %k < PicResortMinigame.minPerfectSwaps; %k++)
   {
      while (true)
      {
         %i = getRandom(3);
         %j = getRandom(3);
         %iS = getRandom(3);
         %jS = getRandom(3);
         
         if ((%i != %iS || %j != %jS) 
             && %this.pieceMat[%iS, %jS].visible == 1  
             && %this.pieceMat[%i, %j].visible == 1)  
             break;
      }

      //"signal" to say they are our perfect swaps
      %this.pieceMat[%i, %j].visible = 0;
      %this.pieceMat[%iS, %jS].visible = 0;      
      
      %this.swap(%i, %j, %iS, %jS);
   }


   for (%i = 0; %i < %this.numOfRows; %i++)
      for (%j = 0; %j < %this.numOfCols; %j++)
      {
         if (%this.pieceMat[%i, %j].visible == 0)
            continue;
            
         while (true)
         {
            %iS = getRandom(3);
            %jS = getRandom(3);
            
            if (%this.pieceMat[%iS, %jS].visible == 1  //not meant to be a perfect swap
                &&(%i*%this.numOfCols + %j) != %this.pieceMat[%iS, %jS].index 
                && (%iS*%this.numOfCols + %jS) != %this.pieceMat[%i, %j].index)
                break;
         }
         
         %this.swap(%i, %j, %iS, %jS);
      }

   //no need to signilize the perfect swaps anymore
   for (%i = 0; %i < %this.numOfRows; %i++)
      for (%j = 0; %j < %this.numOfCols; %j++)
         %this.pieceMat[%i, %j].visible = 1;


   for (%i = 0; %i < %this.numOfRows; %i++)
      for (%j = 0; %j < %this.numOfCols; %j++)
      {
         %pieceButton = new GuiBitmapButtonCtrl() {
            canSaveDynamicFields = "0";
            Enabled = "1";
            isContainer = "0";
            HorizSizing = "relative";
            VertSizing = "relative";
            Position = (%x0 + mRound(%j*%pieceWidth)) SPC (%y0 + mRound(%i*%pieceHeight));
            Extent = %extent;
            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 = "PicResortMinigame.clickedAt("@%i@","@%j@");";
         };
         
         MinigameRefCenter.add(%pieceButton);
      }
         
   %this.swaper = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = %x0 SPC %y0;
      Extent =  %extent;
      MinExtent = "2 2";
      canSave = "1";
      Visible = 0;
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      bitmap = "scriptsAndAssets/data/ui/minigame/pic_resort/swaper";
      wrap = "0";
   };

   MinigameRefCenter.add(%this.swaper);

   %this.photoGlow = new GuiPlasticBitmapCtrl(){
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      Profile = "GuiContentProfile";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = PicResortMinigame.miniPicGlowPos;
      Extent = PicResortMinigame.miniPicGlowExt;
      MinExtent = "2 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "0.5";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      useVariable = "0";
      tile = "0";      
      rotation = "0";
      deltaRotation = "0";
      noClip = "0";
      DML = "scriptsAndAssets/data/ui/minigame/pic_resort/"@playMinigameDlg.currentLevel@"/photo_glow.png";
   };

   MinigameRefCenter.add(%this.photoGlow);
   
   %arrow = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = PicResortMinigame.ArrowPos;
      Extent = PicResortMinigame.ArrowExt;
      MinExtent = "2 2";
      canSave = "1";
      Visible = 1;
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      bitmap = "scriptsAndAssets/data/ui/minigame/pic_resort/"@playMinigameDlg.currentLevel@"/arrow";
      wrap = "0";
   };
   
   MinigameRefCenter.add(%arrow);

   %this.arrowGlow = new GuiPlasticBitmapCtrl(){
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      Profile = "GuiContentProfile";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = PicResortMinigame.ArrowPos;
      Extent = PicResortMinigame.ArrowExt;
      MinExtent = "2 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "0.5";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      useVariable = "0";
      tile = "0";      
      rotation = "0";
      deltaRotation = "0";
      noClip = "0";
      DML = "scriptsAndAssets/data/ui/minigame/pic_resort/"@playMinigameDlg.currentLevel@"/arrow_glow.png";
   };

   MinigameRefCenter.add(%this.arrowGlow);
   
   %this.lockEffect1 = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = "0 0";
      Extent = mRound(%pieceWidth * 1.621) SPC mRound(%pieceHeight * 1.692); //these calculus standardize the lock effect for different piece sizes
      MinExtent = "2 2";
      canSave = "1";
      Visible = 0;
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      bitmap = "scriptsAndAssets/data/ui/minigame/pic_resort/lock_effect/correct1";
      wrap = "0";
   };
   
   MinigameRefCenter.add(%this.lockEffect1);

   %this.lockEffect2 = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = "0 0";
      Extent = mRound(%pieceWidth * 1.621) SPC mRound(%pieceHeight * 1.692); //these calculus standardize the lock effect for different piece sizes
      MinExtent = "2 2";
      canSave = "1";
      Visible = 0;
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      bitmap = "scriptsAndAssets/data/ui/minigame/pic_resort/lock_effect/correct1";
      wrap = "0";
   };
   
   MinigameRefCenter.add(%this.lockEffect2);

   MinigameRefCenter.pushToBack(PicResortSkipButton);

   %this.lineIter1 = LinePixelIterator::NewLinePixelIterator();
   %this.lineIter2 = LinePixelIterator::NewLinePixelIterator();
   
   %this.pixelsPerSwapStep = mFloor(PicResortMinigame.pixelsPerSecOnSwap*0.15);
   
   %this.gameFinished = false;

   %this.initSpecificGui();   
   
   // Force resizing
   %this.setExtent(getWord(Canvas.extent,0), getWord(Canvas.extent,1));
   Canvas.setWidescreen(MinigameRefCenter, 0, false);
}

function PicResortMinigame::start(%this)
{
   %this.moveSound = new SFXProfile()
   {
      filename = "scriptsAndAssets/data/ui/minigame/pic_resort/move_image";
      description = "AudioGui";
      preload = true;
   };
   
   %this.lockSound = new SFXProfile()
   {
      filename = "scriptsAndAssets/data/ui/minigame/pic_resort/lock_sound";
      description = "AudioGui";
      preload = true;
   };

   %this.selectSound = new SFXProfile()
   {
      filename = "scriptsAndAssets/data/ui/minigame/pic_resort/select_image";
      description = "AudioGui";
      preload = true;
   };

   sfxPlay(MusicKungFuLevel);  
   
   //timer is used to do the glow animation
   %this.timer = 5;
   %this.timerProgressive = true;
   scheduleObjLCA(%this, "Minigame", 100, "glowAnimation", 0);
}

function PicResortMinigame::glowAnimation(%this)
{
   %alpha = %this.timer / 10;
   %this.photoGlow.setAlpha(%alpha);
   %this.arrowGlow.setAlpha(%alpha);
   
   if(%this.timerProgressive){
      %this.timer++;
      if(%this.timer == 10)
         %this.timerProgressive = false;   
   }else{
      %this.timer--;
      if(%this.timer == 5)
         %this.timerProgressive = true; 
   }

   scheduleObjLCA(%this, "Minigame", 100, "glowAnimation", 0);
}

function PicResortMinigame::onResize(%this)
{
   %this.onResizeHelper();
   
   Canvas.setWidescreen(MinigameRefCenter, 2000, true);
}

function PicResortMinigame::onResizeHelper(%this)
{
   // PicResortMinigame
   %this.xScale = getWord(MinigameRefCenter.getExtent(), 0)/800;
   %this.yScale =  getWord(MinigameRefCenter.getExtent(), 1)/600; 
   %this.x0 = mRound(getWord(PicResortMinigame.boardPos, 0) * %this.xScale);
   %this.y0 = mRound(getWord(PicResortMinigame.boardPos, 1) * %this.yScale);
   %this.pieceWidth = mRound(getWord(PicResortMinigame.pieceExt, 0) * %this.xScale);
   %this.pieceHeight = mRound(getWord(PicResortMinigame.pieceExt, 1) * %this.yScale);
}

function PicResortMinigame::ClickedAt(%this, %i, %j)
{
   if (%this.curAnim !$= "")
      return;   
      
   if (%this.pieceMat[%i, %j].index == (%i*%this.numOfCols + %j))
      return;
   
   //Temporary, until onResize support be activated
   %this.onResizeHelper();  

   if (%this.selected $= "")
   {
      sfxPlay(%this.selectSound);
      %this.selected = %i SPC %j;
      %this.swaper.setPosition((%this.x0 + mRound(%j*%this.pieceWidth)), 
                               (%this.y0 + mRound(%i*%this.pieceHeight)));
      %this.swaper.visible = "1";
      return;
   }

   %iS = getWord(%this.selected, 0);
   %jS = getWord(%this.selected, 1);

   //if (mAbs(%i - %iS) > 1 || mAbs(%j - %jS) > 1)
      //return;

   %this.selected = "";
   %this.swaper.visible = "0";

   %this.swap(%i, %j, %iS, %jS);

   %this.animSwap(%i, %j, %iS, %jS);
}

function PicResortMinigame::setPiece(%this, %piece, %i, %j)
{
   %pieceBitmap = %this.pieceMat[%i, %j];
   %pieceBitmap.index = %piece;
   
   if (isObject(%pieceBitmap))
      %pieceBitmap.setBitmap("scriptsAndAssets/data/ui/minigame/pic_resort/"@playMinigameDlg.currentLevel@"/pieces/"@%piece);
}

function PicResortMinigame::lockPiece(%this, %i, %j)
{
   if (%this.pieceMat[%i, %j].index != (%i*%this.numOfCols + %j))
      return false;   
   
   if (%this.lockEffect1.visible)
   {
      %this.lockEffect2.setCenterPos(%this.pieceMat[%i, %j].getCenterX(),
                                     %this.pieceMat[%i, %j].getCenterY());
      %this.lockEffect2.setVisible(true);
   }
   else 
   {
      %this.lockEffect1.setCenterPos(%this.pieceMat[%i, %j].getCenterX(),
                                     %this.pieceMat[%i, %j].getCenterY());
      %this.lockEffect1.setVisible(true);
   }
   
   %this.pieceMat[%i, %j].setBitmap("scriptsAndAssets/data/ui/minigame/pic_resort/"@playMinigameDlg.currentLevel@"/locked_pieces/"@(%i*%this.numOfCols + %j));
   
   %this.curAnim = "0";
   scheduleObjLCA(PicResortMinigame, "Minigame", 10, "animLock", 0);
   
   return true;
}

function PicResortMinigame::animLock(%this)
{
   %step = %this.curAnim + 1;

   %this.lockEffect1.setBitmap("scriptsAndAssets/data/ui/minigame/pic_resort/lock_effect/correct"@%step);

   if (%this.lockEffect2.visible)   
      %this.lockEffect2.setBitmap(%this.lockEffect1.bitmap);
   
   if (%step < 5)
   {
      %this.curAnim = %step;
      scheduleObjLCA(PicResortMinigame, "Minigame", mFloor(%this.lockAnimDuration/5), "animLock", 0);
   }
   else
   {
      %this.curAnim = "";
      %this.lockEffect1.setVisible(false);
      %this.lockEffect2.setVisible(false);

      //prepare for the next one
      %this.lockEffect1.setBitmap("scriptsAndAssets/data/ui/minigame/pic_resort/lock_effect/correct1");
      
      if (%this.lockEffect2.visible)   
         %this.lockEffect2.setBitmap(%this.lockEffect1.bitmap);
      
   }
}

function PicResortMinigame::swap(%this, %i1, %j1, %i2, %j2)
{
   sfxPlay(%this.moveSound);   
   
   %ind1 =  %this.pieceMat[%i1, %j1].index;
   %ind2 =  %this.pieceMat[%i2, %j2].index;

   %this.setPiece(%ind1, %i2, %j2);
   %this.setPiece(%ind2, %i1, %j1);
}

function PicResortMinigame::animSwap(%this, %i1, %j1, %i2, %j2)
{
   %pos1 =  %this.pieceMat[%i1, %j1].getPosition();
   %pos2 =  %this.pieceMat[%i2, %j2].getPosition();

   %x1 = getWord(%pos1, 0);
   %y1 = getWord(%pos1, 1);
   %x2 = getWord(%pos2, 0);
   %y2 = getWord(%pos2, 1);

   %this.lineIter1.init(%x2, %y2, %x1, %y1);
   %this.lineIter2.init(%x1, %y1, %x2, %y2);
   
   %this.pieceMat[%i1, %j1].setPosition(%x2, %y2);
   %this.pieceMat[%i2, %j2].setPosition(%x1, %y1);

   %this.curAnim = %i1 SPC %j1 SPC %i2 SPC %j2;

   scheduleObjLCA(%this, "Minigame", 10, "animSwapAux");
}

function PicResortMinigame::animSwapAux(%this)
{
   //Temporary, until onResize support be activated
   %this.onResizeHelper();   
   
   %i1 = getWord(%this.curAnim, 0);
   %j1 = getWord(%this.curAnim, 1);
   %i2 = getWord(%this.curAnim, 2);
   %j2 = getWord(%this.curAnim, 3);
   
   %lineIter1 = %this.lineIter1;
   %lineIter2 = %this.lineIter2;

   %n = %this.pixelsPerSwapStep;

   %x1 = %lineIter1.x;
   %y1 = %lineIter1.y;
   
   for (%i = 0; %i < %n; %i++)
   {
      if (!%lineIter1.next())
         break;
      
      %x1 = %lineIter1.x;
      %y1 = %lineIter1.y;
   }

   %x2 = %lineIter2.x;
   %y2 = %lineIter2.y;

   for (%i = 0; %i < %n; %i++)
   {
      if (!%lineIter2.next())
         break;
      
      %x2 = %lineIter2.x;
      %y2 = %lineIter2.y;
   }
   %this.pieceMat[%i1, %j1].setPosition(%x1, %y1);
   %this.pieceMat[%i2, %j2].setPosition(%x2, %y2);

   if (%i == %n)
   {
      scheduleObjLCA(%this, "Minigame", 150, "animSwapAux");
   }
   else
   {
      %this.curAnim = "";

      %this.onMoveDone();

      //Check if pieces are their right position to lock them
      if (%this.lockPiece(%i1, %j1))
         sfxPlay(%this.lockSound);   
      
      if (%this.lockPiece(%i2, %j2))
         sfxPlay(%this.lockSound);   

      //Check if the game as finished
      %k = 0;   
      for (%i = 0; %i < %this.numOfRows; %i++)
         for (%j = 0; %j < %this.numOfCols; %j++)
            if (%this.pieceMat[%i, %j].index == (%i*%this.numOfCols + %j))
               %k++;
               
      if (%k == %this.numOfRows*%this.numOfCols)
      {
         %this.gameFinished = true;         
         scheduleObjLCA(%this, "Minigame", 10, "quit", 0);
      }
   }
}

// Quit minigame
function PicResortMinigame::quit(%this)
{
   sfxStopAll();
   
   lockMouse(false);
   Canvas.showCursor();               
   
   // Widescreen support
   if(isObject(MinigameCenter))
   {
      Minigame.bringToFront(%this);
      MinigameCenter.setVisible(true);
      Minigame.pushToBack(MinigameCenter);   
   }
   
   // Number of hints won
   Minigame.hintsGained = %this.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 PicResortSkipButton::onMouseEnter(%this){
   //if($CurTake $= 4 && playMinigameDlg.currentLevel $= "SciFiLevel")
   //   %this.setBitmap("scriptsAndAssets/data/ui/minigame/quit_rollover.png");
   //else   
      %this.setBitmap("scriptsAndAssets/data/ui/minigame/skip_rollover.png");      
}

function PicResortSkipButton::onMouseLeave(%this){
   //if($CurTake $= 4 && playMinigameDlg.currentLevel $= "SciFiLevel")
   //   %this.setBitmap("scriptsAndAssets/data/ui/minigame/quit.png");
   //else   
      %this.setBitmap("scriptsAndAssets/data/ui/minigame/skip.png");      
}

function PicResortSkipButton::onMouseDown(%this){
   //if($CurTake $= 4 && playMinigameDlg.currentLevel $= "SciFiLevel")
   //   %this.setBitmap("scriptsAndAssets/data/ui/minigame/quit_pressed.png");
   //else   
      %this.setBitmap("scriptsAndAssets/data/ui/minigame/skip_pressed.png");      
}
