// Get X in default coordinates
function ShoppingList::X2Def(%this, %x)
{
   return mRound(%x / getWord(GuiCenter.Extent,0) * 800);
}

// Get Y in default coordinates
function ShoppingList::Y2Def(%this, %y)
{
   return mRound(%y / getWord(GuiCenter.Extent,1) * 600);
}

// Get X in canvas coordinates
function ShoppingList::X2Cur(%this, %x)
{
   return mRound(%x / 800 * getWord(GuiCenter.Extent,0));
}

// Get Y in canvas coordinates
function ShoppingList::Y2Cur(%this, %y)
{
   return mRound(%y / 600 * getWord(GuiCenter.Extent,1));
}

// Set shopping list font size
function ShoppingList::setFontSize(%this, %size)
{
   for(%i=1; %i<=%this.numPanels; %i++)
   {
      %obj = ("ShopItem"@%i);
      %obj.fixedscalefactor = %size;
   }
}

// Return shopping list font size
function ShoppingList::getFontSize(%this)
{
   return ShopItem1.fixedscalefactor;
}

// Initialize and populate the shopping list
//function ShoppingList::initialize(%this)
function ShoppingList::onWake(%this)
{
   // Number of shopping list panels shown on screen
   
   if($pref::NShoppingListItems5 $= "true" || $pref::NShoppingListItems5 $= 1){
      %this.numPanels = 5;
      
      // Y-position of each item
      %y = 50;
      for(%i=1; %i<=%this.numPanels; %i++)
      {
         %this.itemy[%i] = %y;
         %y += 46;
      }
      
      %xText = 692;
      %xButton = 673;
      
      ShopItem1.setExtent(90,26);
      ShopItem2.setExtent(90,26);
      ShopItem3.setExtent(90,26);
      ShopItem4.setExtent(90,26);
      ShopItem5.setExtent(90,26);
      
      ShopButton1.setExtent(127,25);
      ShopButton2.setExtent(127,25);
      ShopButton3.setExtent(127,25);
      ShopButton4.setExtent(127,25);
      ShopButton5.setExtent(127,25);      
      
      HighlightHintEffect.setExtent(127,16);
      
      ShopItem6.setVisible(false);
      ShopButton6.setVisible(false);
      ShopItem7.setVisible(false);
      ShopButton7.setVisible(false);
      
   }else{
      if($pref::MeterPositionRight $= "true" || $pref::MeterPositionRight $= 1){
         %this.numPanels = 7;
         
         // Y-position of each item
         %y = 30;
         for(%i=1; %i<=%this.numPanels; %i++)
         {
            %this.itemy[%i] = %y;
            %y += 36;
         }
         
         %xText = 692;
         %xButton = 674;

         ShopItem1.setExtent(90,24);
         ShopItem2.setExtent(90,24);
         ShopItem3.setExtent(90,24);
         ShopItem4.setExtent(90,24);
         ShopItem5.setExtent(90,24);
         ShopItem6.setExtent(90,24);
         ShopItem7.setExtent(90,24);
         
         ShopButton1.setExtent(126,14);
         ShopButton2.setExtent(126,14);
         ShopButton3.setExtent(126,14);
         ShopButton4.setExtent(126,14);
         ShopButton5.setExtent(126,14);
         ShopButton6.setExtent(126,14);
         ShopButton7.setExtent(126,14);
         
         HighlightHintEffect.setExtent(127,13);
         
         ShopItem6.setVisible(true);
         ShopButton6.setVisible(true);
         ShopItem7.setVisible(true);
         ShopButton7.setVisible(true);
         
      }else{
         %this.numPanels = 5;
         
         // Y-position of each item
         %y = 50;
         for(%i=1; %i<=%this.numPanels; %i++)
         {
            %this.itemy[%i] = %y;
            %y += 46;
         }

         %xText = 692;
         %xButton = 673;
         
         ShopItem1.setExtent(90,26);
         ShopItem2.setExtent(90,26);
         ShopItem3.setExtent(90,26);
         ShopItem4.setExtent(90,26);
         ShopItem5.setExtent(90,26);
         
         ShopButton1.setExtent(127,25);
         ShopButton2.setExtent(127,25);
         ShopButton3.setExtent(127,25);
         ShopButton4.setExtent(127,25);
         ShopButton5.setExtent(127,25);
         
         HighlightHintEffect.setExtent(127,16);
         
         ShopItem6.setVisible(false);
         ShopButton6.setVisible(false);
         ShopItem7.setVisible(false);
         ShopButton7.setVisible(false);
      }
   }
   
   if(%this.numPanels $= 5)
      %this.setFontSize(0.45); //0.55
   else
      %this.setFontSize(0.368); //0.45

   %this.textGlowOffset = 13;
   //%this.textPanelOffset = 7;
   
   %this.panelSlideTime = 750;

   
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);
   
   // Reset GUI shopping list
   for(%i = 1; %i <= %this.numPanels; %i++)
   {
      %obj = ("ShopItem"@%i);
      //%itemGlow = ("ShopItemGlow"@%i);
      %shopButton = ("ShopButton"@%i);
      //%shopPanel = ("ShopPanel"@%i);
      %obj.itemName       = "";
      %obj.shopListNumber = 0;
      %obj.setText("");
      %obj.Visible        = false;
      %obj.isAnimating    = false;
      
      %obj.setPosition(%xText, %this.itemy[%i]);
      //%itemGlow.setPosition(665, %this.itemy[%i] - %this.textGlowOffset);
      %shopButton.setPosition(%xButton, %this.itemy[%i]);
      //%shopPanel.setPosition(674, (%this.itemy[%i]-%this.textPanelOffset));
      %obj.setVisible(false);
      //%itemGlow.setVisible(false);
      %shopButton.setVisible(true);
      //%shopPanel.setVisible(false);
   }
   
   // Erase contents of $ShopList that may have remained from other takes
   for(%i=0;%i<2048;%i++)
   {
      %this.shopList[%i,"intName"]  = "";
      %this.shopList[%i,"shopName"] = "";
      %this.shopList[%i,"hintFile"] = "";
      %this.shopList[%i,"visible"]  = false;
   }

   %group = MissionGroup;   
   if(!isObject(%group))  
      return;

   //Particular case of an object that should appear just at the Tutorial
   if(!$PlayTutorial && $CurSceneName $= "SpyLevel" && $CurTake $= 1 && isObject(Ladder))
      Ladder.delete();      
      
   // Each row of %this.shopList (1st array index) describes an item that will 
   // be shown in the shopping list at some time during the game.
   // Each column of %this.$ShopList (2nd array index) describes an attribute
   // of the item, as follows:
   // - "intName" : Internal name. This should be the name of the 3D object it 
   //             refers to.
   // - "shopName": Name of the object as it will be shown in the GUI.
   // - "hintFile": File name of the hint image.
   // - "visible" : Whether this item is initially visible in the game level.
   //             Only the first 5 items with this flag set will be shown on 
   //             the 2D shopping list.
   // - "quantity" : How much items of the same name are currently in the scene.
   // - "maxquantity" : Total number of items of the same name in the scene.   
   // - "showqt" : Whether to show N1/N2 next the shopping list text, where
   //              N1=quantity, N2=maxquantity.     
   
   %this.shopListSize = 0;
   
   // Look for objects with shopItem="1"      
   // Objects with priority=-1 are items that shouldn't be
   // added to the shopping list, but may be clicked
   // Objects with priority=-2 are similar to equal to objs 
   // with priority=-1, but they may be clicked as correct objs
   for(%i = %group.next(""); %i !$= ""; %i = %group.next(%i))
   {  
      %obj = %group.getObjectAtIterPos(%i);  
      if(isObject(%obj))
      {
         // Look for shopping list item
         if(%obj.shopItem $= true && %obj.priority >= 0)
         {
            %j = %obj.priority;
            
            if(%this.shopList[%j,"intName"] $= "")
            {
               if(%j > %this.shopListSize)
                  %this.shopListSize = %j;
            
               %this.shopList[%j,"intName"]     = %obj.getName();
               %this.shopList[%j,"shopName"]    = %obj.itemName;
               %this.shopList[%j,"hintFile"]    = %obj.hintFile;
               %this.shopList[%j,"visible"]     = true;
               %this.shopList[%j,"quantity"]    = 1;
               %this.shopList[%j,"maxquantity"] = 1;
               %this.shopList[%j,"showqt"]      = false;
            }
            else
            {
               %this.shopList[%j,"intName"]     = %this.shopList[%j,"intName"] SPC %obj.getName();
               %names = %this.shopList[%j,"intName"];
               
               //%this.shopList[%j,"shopName"]    = %obj.itemName;
               //%this.shopList[%j,"hintFile"]    = %obj.hintFile;               
               %this.shopList[%j,"visible"]     = true;
               %this.shopList[%j,"quantity"]    = getWordCount(%names);
               %this.shopList[%j,"maxquantity"] = getWordCount(%names);
               %this.shopList[%j,"showqt"]      = true;
            }
         }
      }
   }

   // Populate shopping list   
   for(%i=1; %i<=%this.numPanels; %i++)
      %this.populateSlot(%i);  
}

// Finalize the shopping list variables
function ShoppingList::finalize(%this)
{
   cancelScheduleLCA(%this.idleEventID);
}

// Callback of resizing events
function ShoppingList::onResize(%this, %width, %height)
{
   for(%i=1; %i<=%this.numPanels; %i++)
      %this.updateItemText(%i);
}

// Update text of a shopping list text control
function ShoppingList::updateItemText(%this, %i)
{
   %obj = ("ShopItem"@%i);
   %j = %obj.shopListNumber;
   
   if(%this.shopList[%j,"showqt"])
   {
      %text = %this.shopList[%j,"shopName"] SPC 
         %this.shopList[%j,"quantity"] @ "/" @ 
         %this.shopList[%j,"maxquantity"];
      %obj.setText(%text);
   }
   else
   {
      %text = %this.shopList[%j,"shopName"];
      %obj.setText(%text);
   }
}

// Populate slot %i with next available item
// Return true if the slot was populated, false if there are no available
// shopping items
function ShoppingList::populateSlot(%this, %i)
{
   for(%j=1; %j<=%this.shopListSize; %j++)
   {
      if(%this.shopList[%j,"visible"] $= true)
      {
         %obj     = ("ShopItem"@%i);
         //%objGlow = ("ShopItemGlow"@%i);
         %obj.itemName       = %this.shopList[%j,"intName"];
         %obj.shopListNumber = %j;
         //("ShopItem"@%i).shopName         = $ShopList[%j,"shopName"];
         
         %this.updateItemText(%i);

         %obj.setVisible(true);
         %obj.alpha = "1";
         //%objGlow.alpha = "0";
         //%objGlow.setVisible(true);         
         //("ShopPanel"@%i).setVisible(true);
      
         %this.shopList[%j,"visible"] = false;
         
         return true;
      }
   }
   return false;
}

// Remove item %obj from the shopping list and update the list with
// next object of %this.shopList with visible="1".
// Return true if object was removed from the list, false otherwise (wrong obj).
function ShoppingList::remove(%this, %obj)
{    
   if(%obj.priority >= 0)
   {
      for(%i=1; %i<=%this.numPanels; %i++)     
      { 
         %item = ("ShopItem"@%i);
         for(%j=0; %j<getWordCount(%item.itemName); %j++)
         {
            if(getWord(%item.itemName, %j) $= %obj.getName())
            {
               %shopListNumber = %item.shopListNumber;
                           
               %this.shopList[%shopListNumber,"quantity"]--;
               if(%this.shopList[%shopListNumber,"quantity"] < 1)
               {                                    
                  // Signalizes that the object should not be shown on the GUI 
                  // again
                  %this.shopList[%shopListNumber,"visible"] = false;         

                  // Pop item
                  //%item.setVisible(false);
                  //("ShopPanel"@%i).setVisible(false);//animateAlpha(0, 0, 250, "0 0");
                  
                  // Change position to 5th panel, but hide to the right
                  %offset = 133;
                  
                  if($pref::NShoppingListItems5 $= "true" || $pref::NShoppingListItems5 $= 1){
                     %item.animateCenter(%this.X2Cur(692+%offset+45), %this.Y2Cur(%this.itemy[%i]+14), 0, %this.panelSlideTime, "2 2");
                  }else{
                     if($pref::MeterPositionRight $= "true" || $pref::MeterPositionRight $= 1){
                        %item.animateCenter(%this.X2Cur(692+%offset+45), %this.Y2Cur(%this.itemy[%i]+11), 0, %this.panelSlideTime, "2 2");
                     }else{
                        %item.animateCenter(%this.X2Cur(692+%offset+45), %this.Y2Cur(%this.itemy[%i]+14), 0, %this.panelSlideTime, "2 2");
                     }
                  }                  

                  //("ShopItemGlow"@%i).animateCenter(%this.X2Cur(678+%offset+52.5), %this.Y2Cur(%this.itemy[%i]+16.5), 0, %this.panelSlideTime, "2 2");
                  //("ShopPanel"@%i).animateCenter(%this.X2Cur(674+%offset+62.5), %this.Y2Cur(%this.itemy[%i]+24-%this.textPanelOffset), 0, %this.panelSlideTime, "2 2");
                  %item.isAnimating = true;
                  %this.eventShopItem[%i] = scheduleObjLCA(%this, "HO", 500, "showNewPanel", 1, %i);
               }
               else
               if(%this.shopList[%shopListNumber,"showqt"])
                  %this.updateItemText(%i);

               HintSystem.onShopItemRemoved(%item);         
         
               return true;
            }
         }
      }
   }
   return false;
}

// Animate shopping panel coming from the right into the screen
function ShoppingList::showNewPanel(%this, %i)
{ 
   %item  = ("ShopItem"@%i);
   //%glow  = ("ShopItemGlow"@%i);
   //%panel = ("ShopPanel"@%i);
   
   if(!%this.populateSlot(%i))
   {
      %item.setVisible(false);
      //%glow.setVisible(false);
      //%panel.setVisible(false);      
   }
   else
   {
      //sfxPlay(AudioNewShoppingListItem);
      
      if($pref::NShoppingListItems5 $= "true" || $pref::NShoppingListItems5 $= 1){
         %item.animateCenter(%this.X2Cur(692+45), %this.Y2Cur(%this.itemy[%i]+14), 0, %this.panelSlideTime, "1 2");
      }else{
         if($pref::MeterPositionRight $= "true" || $pref::MeterPositionRight $= 1){
            %item.animateCenter(%this.X2Cur(692+45), %this.Y2Cur(%this.itemy[%i]+11), 0, %this.panelSlideTime, "1 2");
         }else{
            %item.animateCenter(%this.X2Cur(692+45), %this.Y2Cur(%this.itemy[%i]+14), 0, %this.panelSlideTime, "1 2");
         }
      } 

      //%item.animateCenter(%this.X2Cur(678+52.5), %this.Y2Cur(%this.itemy[%i]+16.5), 0, %this.panelSlideTime, "1 2");
      //%glow.animateCenter(%this.X2Cur(678+52.5), %this.Y2Cur(%this.itemy[%i]+16.5), 0, %this.panelSlideTime, "1 2");   
      //%panel.animateCenter(%this.X2Cur(674+62.5), %this.Y2Cur(%this.itemy[%i]+24-%this.textPanelOffset), 0, %this.panelSlideTime, "1 2");   
   }
}

function ShoppingList::glowItem(%this, %i, %glow)
{
   if (%glow)
      ("ShopItemGlow"@%i).animateAlpha(1, 0, 500, "0 0");
   else
      ("ShopItemGlow"@%i).animateAlpha(0, 0, 500, "0 0");
}

function ShoppingList::glowItems(%this, %glow)
{
   for (%i = 1; %i <= %this.numPanels; %i++)
      %this.glowItem(%i, %glow);
}

function ShoppingList::shopItemClicked(%this, %i)
{
   //check if this still a valid shopping list item
   if (ShoppingList.shopList[("ShopItem"@%i).shopListNumber,"quantity"] <= 0)
      return;

   sfxPlay(AudioButtonPress); //Button Press Sound
   
   HintSystem.onShopItemClicked(%i);
}