function SliderMinigame::onWake(%this)
{   
   exec("~/data/ui/minigame/slider/"@playMinigameDlg.currentLevel@"/SliderConfig.cs");

   // Set skip button
   if(($CurTake $= 2 && playMinigameDlg.currentLevel $= "SpyLevel") ||
      ($CurTake $= 4 && playMinigameDlg.currentLevel $= "WesternLevel"))
      SliderSkipButton.setBitmap("scriptsAndAssets/data/ui/minigame/quit.png");
   else   
      SliderSkipButton.setBitmap("scriptsAndAssets/data/ui/minigame/skip.png");

   if (SliderMinigame.boardPos $= "")
      return;

   %this.initSpecificGui();

   %this.gameCtrl = new GuiContainer() {
      canSaveDynamicFields = "1";
      Enabled = "1";
      className = "SliderGameCtrl";    
      isContainer = "1";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = SliderMinigame.boardPos;
      Extent = SliderMinigame.boardExt;
      MinExtent = "1 1";
      canSave = "1";
      Visible = "0";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      lockMouse = "0";
      Margin = "0 0 0 0";
      Padding = "0 0 0 0";
      AnchorTop = "1";
      AnchorBottom = "0";
      AnchorLeft = "1";
      AnchorRight = "0";
      useMouseEvents = "1";
   };

   MinigameRefCenter.add(%this.gameCtrl);

   %this.gameCtrl.handCursor = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = "0 0";
      Extent = "50 50";
      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/hand_cursor";
      wrap = "0";
   };
   
   MinigameRefCenter.add(%this.gameCtrl.handCursor);

   MinigameRefCenter.pushToBack(SliderSkipButton);
   MinigameRefCenter.pushToBack(SliderResetButton);

   %endPosX = getWord(SliderMinigame.endPos, 0);
   %endPosY = getWord(SliderMinigame.endPos, 1);

  %grBlkWidth = getWord(SliderMinigame.gridBlockExt, 0);
  %grBlkHeight = getWord(SliderMinigame.gridBlockExt, 1);

   SliderMinigame.endPos = %endPosX*%grBlkWidth SPC %endPosY*%grBlkHeight;

   // Force resizing
   %this.setExtent(getWord(Canvas.extent,0), getWord(Canvas.extent,1));
   Canvas.setWidescreen(MinigameRefCenter, 0, false);
   
   %this.curLevel = 0;
   %this.loadLevel(0);
   
   %this.gameCtrl.setVisible(true);
}

function SliderMinigame::onResize(%this, %width, %height)
{
   Canvas.setWidescreen(MinigameRefCenter, 2000, true);
   
   if (!SliderMinigame.ConfettiIsPlaying)
   {
      onLoseFocus();
      onGainFocus();
      onResumeAfterLosingFocus();
   }   
}

function SliderMinigame::loadLevel(%this, %level)
{
   %this.timeToWait = 0;
   %this.curLevel = %level;

   if (%this.board !$= "")
   {
      %this.board.delete();   
      %this.boardFace.delete();   
   }
   
   %this.board = new GuiContainer() {
      canSaveDynamicFields = "1";
      Enabled = "1";
      isContainer = "1";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = SliderMinigame.boardPos;
      Extent = SliderMinigame.boardExt;
      MinExtent = "1 1";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      lockMouse = "0";
      Margin = "0 0 0 0";
      Padding = "0 0 0 0";
      AnchorTop = "1";
      AnchorBottom = "0";
      AnchorLeft = "1";
      AnchorRight = "0";
      useMouseEvents = "0";
   };

   %this.boardFace = new GuiContainer() {
      canSaveDynamicFields = "1";
      Enabled = "1";
      isContainer = "1";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = SliderMinigame.boardPos;
      Extent = SliderMinigame.boardExt;
      MinExtent = "1 1";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      lockMouse = "0";
      Margin = "0 0 0 0";
      Padding = "0 0 0 0";
      AnchorTop = "1";
      AnchorBottom = "0";
      AnchorLeft = "1";
      AnchorRight = "0";
      useMouseEvents = "0";
   };

  %grBlkWidth = getWord(SliderMinigame.gridBlockExt, 0);
  %grBlkHeight = getWord(SliderMinigame.gridBlockExt, 1);

   %i = -1;
   while (%this.piece[%level,%i++] !$= "")
   {  
      %piece = %this.piece[%level,%i];
      %descInd = getWord(%piece, 0);
      %pieceDesc = %this.pieceDesc[%descInd];
      
      %pieceBitmap = new GuiBitmapCtrl() {
         canSaveDynamicFields = "0";
         Enabled = "1";
         isContainer = "0";
         HorizSizing = "relative";
         VertSizing = "relative";
         Position = getWord(%piece, 1)*%grBlkWidth SPC getWord(%piece, 2)*%grBlkHeight;
         Extent =  getWord(%pieceDesc, 1)*%grBlkWidth SPC getWord(%pieceDesc, 2)*%grBlkHeight;
         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/Slider/"@playMinigameDlg.currentLevel@"/piece"@%descInd;
         wrap = "0";
      };
      
      %this.boardFace.add(%pieceBitmap);

      %piece = new GuiControl() {
         canSaveDynamicFields = "0";
         Enabled = "1";
         isContainer = "0";
         HorizSizing = "relative";
         VertSizing = "relative";
         Position = %pieceBitmap.Position;
         Extent =  %pieceBitmap.Extent;
            pos0 = %pieceBitmap.Position;
            index = %i;
            dir = getWord(%pieceDesc, 0);
            pieceBitmap = %pieceBitmap;
            bitmapFile = %pieceBitmap.bitmap;
         MinExtent = "2 2";
         canSave = "1";
         Visible = "1";
         hovertime = "1000";
         alpha = "1";
         color = "255 255 255 255";
         unitClip = "0 0 1 1";
      };

      %this.board.add(%piece);
   }

   %i = -1;
   while (%this.block[%level,%i++] !$= "")
   {  
      %block = %this.block[%level,%i];
      %pos = getWord(%block, 0)*%grBlkWidth SPC getWord(%block, 1)*%grBlkHeight;      
      
      %blockGC = new GuiControl() {
         canSaveDynamicFields = "0";
         Enabled = "1";
         isContainer = "0";
         HorizSizing = "relative";
         VertSizing = "relative";
         Position = %pos;
            pos0 = %pos;
            dir = "";
         Extent =  getWord(%block, 2)*%grBlkWidth SPC getWord(%block, 3)*%grBlkHeight;
         MinExtent = "2 2";
         canSave = "1";
         Visible = "1";
         hovertime = "1000";
         alpha = "1";
         color = "255 255 255 255";
         unitClip = "0 0 1 1";
      };
      
      %this.board.add(%blockGC);
   }
   
   %winScaleX = 1.0 / 800 * getWord(MinigameRefCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(MinigameRefCenter.getExtent(),1);
   
   %this.boardFace.setPosition(getWord(SliderMinigame.boardPos, 0)*%winScaleX, 
                               getWord(SliderMinigame.boardPos, 1)*%winScaleY);

   %this.boardFace.setExtent(getWord(SliderMinigame.boardExt, 0)*%winScaleX, 
                             getWord(SliderMinigame.boardExt, 1)*%winScaleY);
   
/*
   %this.boardFace.setPosition(getWord(SliderMinigame.boardPos, 0)*getWord(Canvas.extent,0)/800, 
                               getWord(SliderMinigame.boardPos, 1)*getWord(Canvas.extent,1)/600);

   %this.boardFace.setExtent(getWord(SliderMinigame.boardExt, 0)*getWord(Canvas.extent,0)/800, 
                             getWord(SliderMinigame.boardExt, 1)*getWord(Canvas.extent,1)/600);
*/
   MinigameRefCenter.add(%this.boardFace);
   
   MinigameRefCenter.pushToBack(%this.gameCtrl);
   MinigameRefCenter.pushToBack(%this.gameCtrl.handCursor);
}

// Called by miniGame.cs
function SliderMinigame::start(%this)
{   
   %this.slidingSoundIsPlaying = false;
   
   %this.gameFinished = false;
   %this.ConfettiIsPlaying = false;

   //scheduleObjLCA(%this, "Minigame", 1800, "updateTimer", 0);
   
   sfxPlay("Music"@playMinigameDlg.currentLevel);
}

function SliderMinigame::getBoardX(%this, %faceX)
{
   return mRound(%faceX*getWord(%this.board.getExtent(), 0)/ getWord(%this.boardFace.getExtent(), 0));
}

function SliderMinigame::getBoardY(%this, %faceY)
{
   return mRound(%faceY*getWord(%this.board.getExtent(), 1) / getWord(%this.boardFace.getExtent(), 1));
}

function SliderMinigame::getFaceX(%this, %boardX)
{
   return mRound(%boardX*getWord(%this.boardFace.getExtent(), 0)/ getWord(%this.board.getExtent(), 0));
}

function SliderMinigame::getFaceY(%this, %boardY)
{
   return mRound(%boardY*getWord(%this.boardFace.getExtent(), 1) / getWord(%this.board.getExtent(), 1));
}

function SliderMinigame::restartLevel(%this)
{
   %pCount =  %this.board.getCount();
   %pbCount = %this.boardFace.getCount();
   for (%i = 0; %i < %pCount; %i++)
   {
      %piece = %this.board.getObject(%i);
      %posX = getWord(%piece.pos0, 0);
      %posY = getWord(%piece.pos0, 1);
      
      %piece.setPosition(%posX, %posY);
      
      if (%i < %pbCount)
      {
         %pieceBitmap = %this.boardFace.getObject(%i);
         %pieceBitmap.setPosition(%this.getFaceX(%posX), %this.getFaceY(%posY));
      }
   }
}


function SliderGameCtrl::onMouseEnter(%this, %modifier, %worldPosition, %mouseClicks )
{
   echo("enter");
   %this.handCursor.setVisible(true);
   //lockMouse(true);
   Canvas.hideCursor();
}

function SliderGameCtrl::onMouseMove(%this, %modifier, %worldPosition, %mouseClicks )
{
   // Compensate widescreen offset
   %rect2 = MinigameRefCenter.getPosition();
   
   //+1 is to avoid clicking always on the hand
   %this.handCursor.setPosition(getWord(%worldPosition, 0)+1 - getWord(%rect2, 0), getWord(%worldPosition, 1)+1 - getWord(%rect2, 1));
}


function SliderGameCtrl::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)
{
   %this.handCursor.setBitmap("scriptsAndAssets/data/ui/minigame/hand_cursor_clicked");

   if (SliderMinigame.timeToWait > 0)
      return;
      
   // Compensate widescreen offset
   %rect2 = MinigameRefCenter.getPosition();      
      
   %x = SliderMinigame.getBoardX(getWord(%worldPosition, 0) - getWord(%this.Position, 0) - getWord(%rect2, 0));
   %y = SliderMinigame.getBoardY(getWord(%worldPosition, 1) - getWord(%this.Position, 1) - getWord(%rect2, 1));
   
   %this.lastX = %x;   
   %this.lastY = %y;   
   %piece = SliderMinigame.board.getChildAt(%x, %y);
   
   if (%piece.dir !$= "")
   {
	  sfxPlay(SliderMinigame.sfxPickUp);
      %piece.pieceBitmap.setBitmap(%piece.bitmapFile@"_highlight");
      %this.selPiece = %piece;
   }
}

function SliderGameCtrl::onMouseDragged(%this, %modifier, %worldPosition, %mouseClicks )
{
   %this.onMouseMove(%modifier, %worldPosition, %mouseClicks);   
   
   %piece = %this.selPiece;
   
   if (%piece $= "")
      return;   
      
   // Compensate widescreen offset
   %rect2 = MinigameRefCenter.getPosition();      
   
   %x = SliderMinigame.getBoardX(getWord(%worldPosition, 0) - getWord(%this.Position, 0) - getWord(%rect2, 0));
   %y = SliderMinigame.getBoardY(getWord(%worldPosition, 1) - getWord(%this.Position, 1) - getWord(%rect2, 1));
   
   %pX = getWord(%piece.getPosition(), 0);
   %pY = getWord(%piece.getPosition(), 1);
   
   if (%piece.dir $= "h")
   {
      %grBlkWidth = getWord(SliderMinigame.gridBlockExt, 0);

      //Makes the sliding more responsive
      %deltaGR = mAbs(%x - %this.lastX) % %grBlkWidth;
      if (%deltaGR * 2 > %grBlkWidth)
         %x += (%x > %this.lastX? %grBlkWidth - %deltaGR : %deltaGR - %grBlkWidth);
      
      %unitsToMove = mFloor(mAbs(%x - %this.lastX)/%grBlkWidth);
      
      if (%unitsToMove == 0)
         return;            
      
      if (%x < %this.lastX)
         %grBlkWidth = - %grBlkWidth;
      
      %dstX  = %pX + %unitsToMove * %grBlkWidth;
      
      while (%pX != %dstX)
      {
         %pX += %grBlkWidth;
         %col = SliderMinigame.board.getCollision(%piece, %pX, %pY);
         if (%col !$= "")
            break;
      }

      if (%col $= "")
      {
         %piece.setPosition(%pX,%pY);
         %piece.pieceBitmap.setPosition(SliderMinigame.getFaceX(%pX),SliderMinigame.getFaceY(%pY));
      }
      else 
      {
         %piece.setPosition(%pX - %grBlkWidth, %pY);
         %piece.pieceBitmap.setPosition(SliderMinigame.getFaceX(%pX - %grBlkWidth),SliderMinigame.getFaceY(%pY));
      }
   }
   else
   {
      %grBlkHeight = getWord(SliderMinigame.gridBlockExt, 1);

      //Makes the sliding more responsive
      %deltaGR = mAbs(%y - %this.lastY) % %grBlkHeight;
      if (%deltaGR * 2 > %grBlkHeight)
         %y += (%y > %this.lastY? %grBlkHeight - %deltaGR : %deltaGR - %grBlkHeight);

      %unitsToMove = mRound(mAbs(%y - %this.lastY)/%grBlkHeight);

      if (%unitsToMove == 0)
         return;            
      
      if (%y < %this.lastY)
         %grBlkHeight = - %grBlkHeight;
      
      %dstY  = %pY + %unitsToMove * %grBlkHeight;
      
      while (%pY!= %dstY)
      {
         %pY += %grBlkHeight;
         %col = SliderMinigame.board.getCollision(%piece, %pX, %pY);
         if (%col !$= "")
            break;
      }

      if (%col $= "")
         %piece.setPosition(%pX, %pY);
      else
         %piece.setPosition(%pX, %pY - %grBlkHeight);

      if (%col $= "")
      {
         %piece.setPosition(%pX, %pY);
         %piece.pieceBitmap.setPosition(SliderMinigame.getFaceX(%pX),SliderMinigame.getFaceY(%pY));
      }
      else 
      {
         %piece.setPosition(%pX, %pY - %grBlkHeight);
         %piece.pieceBitmap.setPosition(SliderMinigame.getFaceX(%pX),SliderMinigame.getFaceY(%pY - %grBlkHeight));
      }
   }

   if (%piece.index == 0 && %piece.getPosition() $= SliderMinigame.endPos)
   {
      SliderMinigame.slidingSoundIsPlaying = false;

      SliderMinigame.timeToWait = SliderMinigame.animLevelEnd();

      %this.selPiece = "";

      if (SliderMinigame.curLevel + 1 == SliderMinigame.numOfLevels)
      {
         SliderMinigame.gameFinished = true;
         //wait a little to quit in order to avoid performance issues to set the last piece position
         scheduleObjLCA(SliderMinigame, "Minigame", SliderMinigame.timeToWait, "quit", 0);
      }
      else
         scheduleObjLCA(SliderMinigame, "Minigame", SliderMinigame.timeToWait, "loadLevel", 1, SliderMinigame.curLevel + 1);
   }

   //if(!SliderMinigame.slidingSoundIsPlaying){
   //   sfxPlay(SlidingSoundSliderMinigame);
   //   SliderMinigame.slidingSoundIsPlaying = true;
   //}
   
   %this.lastX = %x;   
   %this.lastY = %y;   
}

function SliderGameCtrl::onMouseUp(%this, %modifier, %worldPosition, %mouseClicks )
{
   %this.handCursor.setBitmap("scriptsAndAssets/data/ui/minigame/hand_cursor");

   SliderMinigame.slidingSoundIsPlaying = false;
   if (%this.selPiece !$= "")
   {
	  sfxPlay(SliderMinigame.sfxPickDown);
      %this.selPiece.pieceBitmap.setBitmap(%this.selPiece.bitmapFile);
      %this.selPiece = "";
   }
}

function SliderGameCtrl::onMouseLeave(%this, %modifier, %worldPosition, %mouseClicks )
{
   echo("Leave");
   %this.handCursor.setBitmap("scriptsAndAssets/data/ui/minigame/hand_cursor");

   %this.handCursor.setVisible(false);
   //lockMouse(true);
   Canvas.showCursor();

   SliderMinigame.slidingSoundIsPlaying = false;
   if (%this.selPiece !$= "")
   {
	  sfxPlay(SliderMinigame.sfxPickDown);
      %this.selPiece.pieceBitmap.setBitmap(%this.selPiece.bitmapFile);
      %this.selPiece = "";
   }
}

// Quit minigame
function SliderMinigame::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.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", 14*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();
   }
}

//Functions for the Skip and Reset Buttons
function SliderSkipButton::onMouseEnter(%this){
   if(($CurTake $= 2 && playMinigameDlg.currentLevel $= "SpyLevel") ||
      ($CurTake $= 4 && playMinigameDlg.currentLevel $= "WesternLevel"))
      %this.setBitmap("scriptsAndAssets/data/ui/minigame/quit_rollover.png");
   else
      %this.setBitmap("scriptsAndAssets/data/ui/minigame/skip_rollover.png");
}

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

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

function SliderResetButton::onMouseEnter(%this){
   %this.setBitmap("scriptsAndAssets/data/ui/minigame/reset_rollover.png");
}

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

function SliderResetButton::onMouseDown(%this){
   %this.setBitmap("scriptsAndAssets/data/ui/minigame/reset_pressed.png");
}
