// Initialize hint system
function HintSystem::init(%this)
{
   %this.hintWaitTime = 30;
   %this.hintEvent = -1;
   %this.waitingClick = false;

   %this.playerHintCount = PlayerProfile.getFieldValue("PlayerHints"@$CurPlayerNum);

   %this.SetHintNumber(%this.playerHintCount);
   
   %this.lastHintClick = 0;
   %this.glowAnimationRunning = true;
}

function HintSystem::updateHint(%this)
{
   %this.SetHintNumber(%this.playerHintCount);
}

function HintSystem::glowAnimation(%this, %i)
{

	if (%i $= "") %i = 1;
   if(%this.glowAnimationRunning){
      if(%this.pulseSpeed >= 0){
         
         %this.timerToSpeedUp++;
         
         %alpha = %this.timer / 10;
         TimerGlow.setAlpha(%alpha);
         
         if(%this.timerProgressive){
            %this.timer++;
            if(%this.timer == 10){
               %this.timerProgressive = false;
               sfxPlay("AudioTimerBeep"@%i);
			   %i = ((%i + 1) % 2) + 1;

            }
         }else{
            %this.timer--;
            if(%this.timer == 0)
               %this.timerProgressive = true; 
         }
         
         if(%this.timerToSpeedUp % 6 == 0){
            %this.pulseSpeed -= 1;
         }
	
         scheduleObjLCA(%this, "HO", %this.pulseSpeed, "glowAnimation", 1,  %i);
      
      }
   }
}

function HintSystem::glowAnimationAux(%this, %i)
{
	if (%i $= "") %i = 1;
   if(!%this.glowAnimationRunning){
      if(%this.pulseSpeed >= 0){
         
         %this.timerToSpeedUp++;
         
         %alpha = %this.timer / 10;
         TimerGlow.setAlpha(%alpha);
         
         if(%this.timerProgressive){
            %this.timer++;
            if(%this.timer == 10){
               %this.timerProgressive = false;
               sfxPlay("AudioTimerBeep"@%i);
			   %i = (%i % 2) + 1;
            }
         }else{
            %this.timer--;
            if(%this.timer == 0)
               %this.timerProgressive = true; 
         }
         

         if(%this.timerToSpeedUp % 6 == 0){
            %this.pulseSpeed -= 1;
         }
         scheduleObjLCA(%this, "HO", %this.pulseSpeed, "glowAnimationAux", 1, %i);
      
      }
   }
}

// Callback for resizing events
function HintSystem::onResize(%this, %width, %height)
{   
   %this.SetHintNumber(%this.playerHintCount);
}

// Digits Update and Reposition
function HintSystem::SetHintNumber(%this, %nHints)
{
   // Scale factors when resolution is different than 800x600
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);
   
   if(%nHints < 10){ // 0 - 9 = 1 digit
   
      HintSystemCounterDigit1.setVisible(false);
      HintSystemCounterDigit2.setVisible(true);
      HintSystemCounterDigit3.setVisible(false);
      HintSystemCounterDigit2.setDML("scriptsAndAssets/data/ui/ho/timer/" @ %nHints @ ".png");
      HintSystemCounterDigit2.setPosition(75 * %winScaleX, 59 * %winScaleY);
      
   }else if(%nHints < 100){ // 10 - 99 = 2 digits
      
      HintSystemCounterDigit1.setVisible(true);
      HintSystemCounterDigit2.setVisible(true);
      HintSystemCounterDigit3.setVisible(false);
      
      %digit1 = mFloor(%nHints / 10);
      %digit2 = %nHints - %digit1*10;
      
      HintSystemCounterDigit1.setDML("scriptsAndAssets/data/ui/ho/timer/" @ %digit1 @ ".png");
      HintSystemCounterDigit2.setDML("scriptsAndAssets/data/ui/ho/timer/" @ %digit2 @ ".png");
      
      HintSystemCounterDigit1.setPosition(66 * %winScaleX, 59 * %winScaleY);
      HintSystemCounterDigit2.setPosition(80 * %winScaleX, 59 * %winScaleY);
      
   }else{ // 100 - 999 = 3 digits
      
      HintSystemCounterDigit1.setVisible(true);
      HintSystemCounterDigit2.setVisible(true);
      HintSystemCounterDigit3.setVisible(true);
      
      %digit1 = mFloor(%nHints / 100);
      %digit2 = mFloor((%nHints - %digit1*100) / 10);
      %digit3 = %nHints - %digit1*100 - %digit2*10;
      
      HintSystemCounterDigit1.setDML("scriptsAndAssets/data/ui/ho/timer/" @ %digit1 @ ".png");
      HintSystemCounterDigit2.setDML("scriptsAndAssets/data/ui/ho/timer/" @ %digit2 @ ".png");
      HintSystemCounterDigit3.setDML("scriptsAndAssets/data/ui/ho/timer/" @ %digit3 @ ".png");
      
      HintSystemCounterDigit1.setPosition(61 * %winScaleX, 59 * %winScaleY);
      HintSystemCounterDigit2.setPosition(75 * %winScaleX, 59 * %winScaleY);
      HintSystemCounterDigit3.setPosition(89 * %winScaleX, 59 * %winScaleY);
   }
}

// %i is the number of the shopping list panel (between 1 and 5)
function HintSystem::disableHint(%this, %perTime)
{
   if(%perTime){
      if(TriggerCtrl.getCurTime() - %this.lastHintClick >= %this.hintWaitTime*1000){
         //ShoppingList.glowItems(false);
         ShopItemHint.animateAlpha(0, 0, 500, "0 0");
         //HintActivePanel.setVisible(false);
         %this.hintEvent = -1;
         %this.curItem = "";
         
         HighlightHintEffect.animateAlpha(0, 0, 500, "0 0");
         
         DirectorChairHint.animateAlpha(0, 0, 500, "0 0");
         SceneGui.showDirectorChair();
      }
   }else{
      ShopItemHint.animateAlpha(0, 0, 500, "0 0");
      //HintActivePanel.setVisible(false);
      %this.hintEvent = -1;
      %this.curItem = "";
      
      HighlightHintEffect.animateAlpha(0, 0, 500, "0 0");
      
      DirectorChairHint.animateAlpha(0, 0, 500, "0 0");
      SceneGui.showDirectorChair();
      
      %this.glowAnimationRunning = !%this.glowAnimationRunning;
      TimerGlow.setAlpha(0);
   }
}

function HintSystem::useHint(%this)
{
   sfxPlay(AudioButtonPress); //Button Press Sound
   
   SceneGui.usedAnyHint = true;
   
   if(%this.playerHintCount <= 0 || %this.waitingClick)
      return;

   if(%this.hintEvent >= 0)
   {
     %this.disableHint(false);
      cancelScheduleLCA(%this.hintEvent);
   }

   %this.glow = true;   

   %this.waitingClick = true;
   
   //scheduleObjLCA(%this, "HO", 500, "blinkGlow", 0);
}

function HintSystem::blinkGlow(%this)
{
   if (!%this.waitingClick)
      return;   
   
   //ShoppingList.glowItems(%this.glow);
   %this.glow = !%this.glow;

   scheduleObjLCA(%this, "HO", 500, "blinkGlow", 0);
}

// Called when the hint button is clicked
function HintSystem::onShopItemClicked(%this, %itemInd)
{
   // Scale factors when resolution is different than 800x600
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);
   
   if(%this.playerHintCount>0){
      
      sfxPlay(AudioHintUsed); 
      
      HighlightHintEffect.setAlpha(1);
      HighlightHintEffect.setVisible(true);

      %obj = ("ShopItem"@%itemInd);
      %shopItemX = getWord(%obj.getPosition(),0);
      %shopItemY = getWord(%obj.getPosition(),1);
      %shopItemW = getWord(%obj.getExtent(),0);
      %shopItemH = getWord(%obj.getExtent(),1);
      
      HighlightHintEffect.setPosition(%shopItemX - (%shopItemW/5), %shopItemY + (%shopItemH/5.5));

      HighlightHintEffect.rewind();
      HighlightHintEffect.play(true);
      
      %this.lastHintClick = TriggerCtrl.getCurTime();
      
      //timer is used to do the glow animation
      TimerGlow.setAlpha(0);
      %this.timer = 0;
      %this.timerProgressive = true;
      %this.timerToSpeedUp = 0;
      %this.pulseSpeed = 100;
      
      if(%this.glowAnimationRunning){
         scheduleObjLCA(%this, "HO", %this.pulseSpeed, "glowAnimationAux", 0);
      }else{
         scheduleObjLCA(%this, "HO", %this.pulseSpeed, "glowAnimation", 0);
      }
      
      %this.glowAnimationRunning = !%this.glowAnimationRunning;

      %item = "ShopItem"@%itemInd;   
      
      %shopListNumber = %item.shopListNumber;
      
      %hintFile = ShoppingList.shopList[%shopListNumber,"hintFile"]; 
      if(%hintFile $= "")
         return;
         
      if(!$PlayTutorial)
         %this.playerHintCount--;

      SceneGui.hideDirectorChair();
      DirectorChairHint.animateAlpha(1, 0, 500, "0 0");
      
      ShopItemHint.setDML("scriptsAndAssets/data/ui/ho/hints/"@%hintFile@".jpg");
      ShopItemHint.animateAlpha(1, 0, 500, "0 0");
      
      %this.hintEvent = scheduleObjLCA(%this, "Hints", %this.hintWaitTime*1000, "disableHint", 1, true);

      PlayerProfile.setFieldValue("PlayerHints"@$CurPlayerNum, %this.playerHintCount);
      SavePlayerProfile();

      %this.curItem = %item;

      %this.SetHintNumber(%this.playerHintCount);
   }
}

// Called when the player has gained hints
// %numHints is the number of hints gained
function HintSystem::addHints(%this, %numHints)
{
   %this.playerHintCount += mRound(%numHints);

   PlayerProfile.setFieldValue("PlayerHints"@$CurPlayerNum, %this.playerHintCount);
   SavePlayerProfile();

   %this.SetHintNumber(%this.playerHintCount);
}

function HintSystem::onShopItemRemoved(%this, %item)
{
   if (%item !$= %this.curItem)
      return;
      
   if(%this.hintEvent >= 0)
      cancelScheduleLCA(%this.hintEvent);

   %this.disableHint(false);
}
