package ui
{
   import game.Panda;
   import flash.display.Bitmap;
   import flash.display.Sprite;
   import flash.display.DisplayObjectContainer;
   import flash.events.MouseEvent;
   import tdh.utils.BitmapInt;
   
   public class Store extends Object
   {
      
      public function Store(start:Function, welcomeFunc:Function)
      {
         this.dpo = new StoreBmp();
         super();
         this.back.addChild(new Bitmap(GameUI.btnAreaSrc2));
         this.amuletBuy.graphics.beginFill(16777215);
         this.amuletBuy.graphics.drawRoundRect(0,0,218,67,20,20);
         this.shootBuy.graphics.beginFill(16777215);
         this.shootBuy.graphics.drawRoundRect(0,0,218,67,20,20);
         this.milkBuy.graphics.beginFill(16777215);
         this.milkBuy.graphics.drawRoundRect(0,0,218,67,20,20);
         this.startFunc = start;
         this.welcomeFunc = welcomeFunc;
         this.priceList.push(198,26,99);
      }
      
      private static var StoreBmp:Class = Store_StoreBmp;
      
      public var panda:Panda;
      
      public var data:Data;
      
      private var dpo:Bitmap;
      
      private const back:Sprite = new Sprite();
      
      private const amuletBuy:Sprite = new Sprite();
      
      private const shootBuy:Sprite = new Sprite();
      
      private const milkBuy:Sprite = new Sprite();
      
      private var startFunc:Function;
      
      private var welcomeFunc:Function;
      
      public function showAs(dpoc:DisplayObjectContainer) : void
      {
         this.dpo.visible = false;
         this.dpo.x = (800 - this.dpo.width) / 2;
         this.dpo.y = (480 - this.dpo.height) / 2;
         dpoc.addChild(this.dpo);
         this.amuletPrice.showAs(dpoc);
         this.amuletPrice.value = this.priceList[0];
         this.amuletPrice.x = 410 - this.amuletPrice.width;
         this.amuletPrice.y = 170;
         this.shootPrice.showAs(dpoc);
         this.shootPrice.value = this.priceList[1];
         this.shootPrice.x = 410 - this.shootPrice.width;
         this.shootPrice.y = 247;
         this.milkPrice.showAs(dpoc);
         this.milkPrice.value = this.priceList[2];
         this.milkPrice.x = 410 - this.milkPrice.width;
         this.milkPrice.y = 327;
         this.bambooNum.y = 20;
         this.bambooNum.showAs(dpoc);
         this.amuletNum.x = this.shootNum.x = this.milkNum.x = 675;
         this.amuletNum.y = 170;
         this.shootNum.y = 247;
         this.milkNum.y = 327;
         this.amuletNum.showAs(dpoc);
         this.shootNum.showAs(dpoc);
         this.milkNum.showAs(dpoc);
         this.amuletPrice.visible = this.shootPrice.visible = this.milkPrice.visible = this.amuletNum.visible = this.shootNum.visible = this.milkNum.visible = this.bambooNum.visible = false;
         this.bambooNum.value = 0;
         this.amuletNum.value = 0;
         this.shootNum.value = 0;
         this.milkNum.value = 0;
         this.back.alpha = 0;
         this.back.x = 16;
         this.back.y = 403;
         dpoc.addChild(this.back);
         this.amuletBuy.x = 90;
         this.amuletBuy.y = 150;
         this.amuletBuy.alpha = 0;
         dpoc.addChild(this.amuletBuy);
         this.shootBuy.x = 90;
         this.shootBuy.y = 227;
         this.shootBuy.alpha = 0;
         dpoc.addChild(this.shootBuy);
         this.milkBuy.x = 90;
         this.milkBuy.y = 305;
         this.milkBuy.alpha = 0;
         dpoc.addChild(this.milkBuy);
         this.back.visible = this.amuletBuy.visible = this.shootBuy.visible = this.milkBuy.visible = false;
         this.back.addEventListener(MouseEvent.MOUSE_DOWN,this.mosueDown);
         this.back.addEventListener(MouseEvent.MOUSE_UP,this.returnWelcome);
      }
      
      private function mosueDown(e:MouseEvent) : void
      {
         e.target.alpha = 0.3;
         (e.currentTarget as Sprite).addEventListener(MouseEvent.MOUSE_UP,this.mouseUp);
      }
      
      private function mouseUp(e:MouseEvent) : void
      {
         e.currentTarget.alpha = 0;
         (e.currentTarget as Sprite).removeEventListener(MouseEvent.MOUSE_UP,this.mouseUp);
      }
      
      private function returnWelcome(e:MouseEvent) : void
      {
         this.dpo.visible = this.back.visible = false;
         this.amuletPrice.visible = this.shootPrice.visible = this.milkPrice.visible = this.amuletNum.visible = this.shootNum.visible = this.milkNum.visible = this.bambooNum.visible = this.amuletBuy.visible = this.shootBuy.visible = this.milkBuy.visible = false;
         this.dpo.bitmapData.dispose();
         this.dpo.bitmapData = null;
         this.welcomeFunc();
      }
      
      private function buyProps(e:MouseEvent) : void
      {
         var tmpBambooNum:* = 0;
         switch(e.currentTarget)
         {
            case this.amuletBuy:
               tmpBambooNum = this.panda.numBamboo;
               if((tmpBambooNum = tmpBambooNum - this.priceList[0]) >= 0)
               {
                  this.panda.numAmulet++;
                  this.panda.numBamboo = this.panda.numBamboo - this.priceList[0];
                  this.panda.consumption = this.panda.consumption + this.priceList[0];
                  this.amuletNum.value = this.data.amulet = this.panda.numAmulet;
               }
               break;
            case this.shootBuy:
               tmpBambooNum = this.panda.numBamboo;
               if((tmpBambooNum = tmpBambooNum - this.priceList[1]) >= 0)
               {
                  this.panda.numShoots++;
                  this.panda.numBamboo = this.panda.numBamboo - this.priceList[1];
                  this.panda.consumption = this.panda.consumption + this.priceList[1];
                  this.shootNum.value = this.data.shoots = this.panda.numShoots;
               }
               break;
            case this.milkBuy:
               tmpBambooNum = this.panda.numBamboo;
               if((tmpBambooNum = tmpBambooNum - this.priceList[2]) >= 0)
               {
                  this.panda.numMilk++;
                  this.panda.numBamboo = this.panda.numBamboo - this.priceList[2];
                  this.panda.consumption = this.panda.consumption + this.priceList[2];
                  this.milkNum.value = this.data.milk = this.panda.numMilk;
               }
               break;
         }
         this.bambooNum.value = this.data.bamboo = this.panda.numBamboo;
         this.bambooNum.x = 750 - this.bambooNum.width;
         this.data.save();
      }
      
      public function show() : void
      {
         this.dpo.bitmapData = new StoreBmp().bitmapData;
         this.back.alpha = 0;
         this.dpo.visible = true;
         this.back.visible = this.amuletBuy.visible = this.shootBuy.visible = this.milkBuy.visible = true;
         this.bambooNum.value = this.panda.numBamboo;
         this.bambooNum.x = 750 - this.bambooNum.width;
         this.amuletNum.value = this.panda.numAmulet;
         this.shootNum.value = this.panda.numShoots;
         this.milkNum.value = this.panda.numMilk;
         this.amuletPrice.visible = this.shootPrice.visible = this.milkPrice.visible = this.amuletNum.visible = this.shootNum.visible = this.milkNum.visible = this.bambooNum.visible = true;
         this.data.bamboo = this.panda.numBamboo;
         this.data.amulet = this.panda.numAmulet;
         this.data.shoots = this.panda.numShoots;
         this.data.milk = this.panda.numMilk;
         this.data.save();
      }
      
      private const amuletPrice:BitmapInt = new BitmapInt(GameUI.numSrc3);
      
      private const shootPrice:BitmapInt = new BitmapInt(GameUI.numSrc3);
      
      private const milkPrice:BitmapInt = new BitmapInt(GameUI.numSrc3);
      
      private const priceList:Vector.<Number> = new Vector.<Number>();
      
      private const bambooNum:BitmapInt = new BitmapInt(GameUI.numSrc1);
      
      private const amuletNum:BitmapInt = new BitmapInt(GameUI.numSrc3);
      
      private const shootNum:BitmapInt = new BitmapInt(GameUI.numSrc3);
      
      private const milkNum:BitmapInt = new BitmapInt(GameUI.numSrc3);
   }
}
