package game
{
   import interf.ISteper;
   import flash.display.Bitmap;
   import flash.utils.setTimeout;
   import flash.display.Sprite;
   import flash.display.DisplayObjectContainer;
   import flash.events.MouseEvent;
   import ui.GameUI;
   import flash.geom.Rectangle;
   import flash.geom.Point;
   import flash.events.Event;
   
   public class Achieve extends Object implements ISteper
   {
      
      public function Achieve(start:Function)
      {
         this.icon = new Bitmap();
         this.bgBmp = new Bitmap();
         this.IntroClass = new <Class>[EpicEscapeIntro,ExpertShooterIntro,MarathonIntro,PieceOfCakeIntro,KungfuPandaIntro,MillionaireIntro];
         this.infro = new Bitmap();
         this.cross = new CloseButton();
         super();
         this.startArea.addChild(new Bitmap(GameUI.btnAreaSrc2));
         this.startFunc = start;
         this.hidePanel();
      }
      
      public static const EpicEscape:Class = Achieve_EpicEscape;
      
      public static const ExpertShooter:Class = Achieve_ExpertShooter;
      
      public static const Marathon:Class = Achieve_Marathon;
      
      public static const PieceOfCake:Class = Achieve_PieceOfCake;
      
      public static const KungfuPanda:Class = Achieve_KungfuPanda;
      
      public static const Millionaire:Class = Achieve_Millionaire;
      
      public static const BgBmp:Class = Achieve_BgBmp;
      
      public static const EpicEscapeIntro:Class = Achieve_EpicEscapeIntro;
      
      public static const ExpertShooterIntro:Class = Achieve_ExpertShooterIntro;
      
      public static const KungfuPandaIntro:Class = Achieve_KungfuPandaIntro;
      
      public static const MarathonIntro:Class = Achieve_MarathonIntro;
      
      public static const MillionaireIntro:Class = Achieve_MillionaireIntro;
      
      public static const PieceOfCakeIntro:Class = Achieve_PieceOfCakeIntro;
      
      public static const CloseButton:Class = Achieve_CloseButton;
      
      public var panda:Panda;
      
      public var data:Data;
      
      public function reset() : void
      {
         this.lastItemLocation = 0;
         this.startLocation = 0;
      }
      
      private var closeStep:Boolean;
      
      public function close() : void
      {
         this.closeStep = true;
      }
      
      public function open() : void
      {
         this.closeStep = false;
         this.setComplete();
      }
      
      private function setComplete() : void
      {
         this.richComplete = (this.data.achievement & 1) == 1;
         this.shooterComplete = (this.data.achievement & 2) == 2;
         this.marathonComplete = (this.data.achievement & 4) == 4;
         this.allEasyComplete = (this.data.achievement & 8) == 8;
         this.gongfuComplete = (this.data.achievement & 16) == 16;
         this.nearMissComplete = (this.data.achievement & 32) == 32;
      }
      
      public function step() : void
      {
         if(this.closeStep)
         {
            return;
         }
         if(!this.richComplete)
         {
            this.rich();
         }
         if(!this.shooterComplete)
         {
            this.shooter();
         }
         if(!this.marathonComplete)
         {
            this.marathon();
         }
         if(!this.allEasyComplete)
         {
            this.easy();
         }
         if(!this.gongfuComplete)
         {
            this.gongfu();
         }
         if(!this.nearMissComplete)
         {
            this.nearMiss();
         }
      }
      
      private var richComplete:Boolean;
      
      private const deposit:int = 20000;
      
      private const totalIncome:int = 50000;
      
      private function rich() : void
      {
         if((this.panda.numBamboo > this.deposit) || (this.panda.numBamboo + this.panda.consumption > this.totalIncome))
         {
            this.richComplete = true;
            this.showCompletePanel("大富翁");
         }
      }
      
      private var shooterComplete:Boolean;
      
      private const numHits:int = 30;
      
      private function shooter() : void
      {
         if(this.panda.numHits > this.numHits)
         {
            this.shooterComplete = true;
            this.showCompletePanel("神枪手");
         }
      }
      
      private var marathonComplete:Boolean;
      
      private const distance:int = 42195;
      
      private function marathon() : void
      {
         if(this.panda.runningDistance > this.distance)
         {
            this.marathonComplete = true;
            this.showCompletePanel("马拉松");
         }
      }
      
      private var allEasyComplete:Boolean;
      
      private const easyJourney:int = 2000;
      
      private var lastItemLocation:int;
      
      private var lastShoots:int;
      
      private var lastMilk:int;
      
      private var lastAmulet:int;
      
      private function easy() : void
      {
         if((this.lastMilk > this.panda.numMilk) || (this.lastAmulet > this.panda.numAmulet) || (this.lastShoots > this.panda.numShoots))
         {
            this.lastItemLocation = this.panda.runningDistance;
         }
         this.lastMilk = this.panda.numMilk;
         this.lastAmulet = this.panda.numAmulet;
         this.lastShoots = this.panda.numShoots;
         if(this.panda.runningDistance - this.lastItemLocation > this.easyJourney)
         {
            this.allEasyComplete = true;
            this.showCompletePanel("小菜一碟");
         }
      }
      
      private var gongfuComplete:Boolean;
      
      private const gongfuJourney:int = 300;
      
      private var startLocation:int;
      
      private var gongfuMilk:int;
      
      private function gongfu() : void
      {
         if((!this.panda.isGongfu) || (this.gongfuMilk > this.panda.numMilk))
         {
            this.startLocation = this.panda.runningDistance;
         }
         if(this.panda.runningDistance - this.startLocation > this.gongfuJourney)
         {
            this.gongfuComplete = true;
            this.showCompletePanel("功夫熊猫");
         }
      }
      
      private var mmmCompelte:Boolean;
      
      private const bambooTarget:int = 500;
      
      private const shootsTarget:int = 80;
      
      private function makingMachine() : void
      {
         if((this.panda.eatBamboo > this.bambooTarget) && (this.panda.eatShoots > this.shootsTarget))
         {
            this.mmmCompelte = true;
            this.showCompletePanel("赚钱机器");
         }
      }
      
      private var nearMissComplete:Boolean;
      
      private const nearMissY:int = 640;
      
      private function nearMiss() : void
      {
         if(this.panda.rect.y > this.nearMissY)
         {
            this.panda.nearMiss = true;
         }
         if((this.panda.nearMiss) && (this.panda.isRunning()))
         {
            this.nearMissComplete = true;
            this.showCompletePanel("九死一生");
         }
      }
      
      private var icon:Bitmap;
      
      private var count:int;
      
      private function showCompletePanel(name:String) : void
      {
         switch(name)
         {
            case "九死一生":
               this.icon.bitmapData = new EpicEscape().bitmapData;
               break;
            case "神枪手":
               this.icon.bitmapData = new ExpertShooter().bitmapData;
               break;
            case "马拉松":
               this.icon.bitmapData = new Marathon().bitmapData;
               break;
            case "小菜一碟":
               this.icon.bitmapData = new PieceOfCake().bitmapData;
               break;
            case "功夫熊猫":
               this.icon.bitmapData = new KungfuPanda().bitmapData;
               break;
            case "大富翁":
               this.icon.bitmapData = new Millionaire().bitmapData;
               break;
         }
         this.icon.x = 800 - this.icon.width >> 1;
         this.icon.y = 480 - this.icon.height;
         this.icon.parent.addChild(this.icon);
         this.count++;
         this.icon.visible = true;
         this.data.achievement = int(this.richComplete) + (int(this.shooterComplete) << 1) + (int(this.marathonComplete) << 2) + (int(this.allEasyComplete) << 3) + (int(this.gongfuComplete) << 4) + (int(this.nearMissComplete) << 5);
         this.data.save();
         setTimeout(this.hideCompletePanel,5000);
      }
      
      private function hideCompletePanel() : void
      {
         if(--this.count == 0)
         {
            this.icon.visible = false;
         }
      }
      
      private var bgBmp:Bitmap;
      
      private var startFunc:Function;
      
      private const startArea:Sprite = new Sprite();
      
      public function showAs(dpoc:DisplayObjectContainer) : void
      {
         dpoc.addChild(this.bgBmp);
         dpoc.addChild(this.infro);
         dpoc.addChild(this.cross);
         dpoc.addChild(this.icon);
         dpoc.addChild(this.startArea);
         this.bgBmp.visible = false;
         this.infro.visible = false;
         this.cross.visible = false;
         this.icon.visible = false;
         this.startArea.y = 394;
         this.startArea.x = 31;
         this.startArea.alpha = 0;
         this.startArea.addEventListener(MouseEvent.MOUSE_DOWN,this.mouseDown);
         this.startArea.addEventListener(MouseEvent.MOUSE_UP,this.start);
      }
      
      private function mouseDown(e:MouseEvent) : void
      {
         e.target.alpha = 0.3;
      }
      
      private function start(e:MouseEvent) : void
      {
         this.hidePanel();
         this.startArea.alpha = 0;
         this.startArea.visible = false;
         this.startFunc();
      }
      
      public function showPanel() : void
      {
         this.setComplete();
         this.startArea.alpha = 0;
         this.startArea.visible = true;
         this.bgBmp.bitmapData = new GameUI.Panel().bitmapData;
         var rect:Rectangle = new Rectangle(0,0,800,480);
         var dest:Point = new Point();
         this.bgBmp.bitmapData.copyPixels(new BgBmp().bitmapData,rect,dest,null,null,true);
         rect.width = 170;
         rect.height = 170;
         if(this.richComplete)
         {
            dest.x = 483;
            dest.y = 273;
            this.bgBmp.bitmapData.copyPixels(new Millionaire().bitmapData,rect,dest,null,null,true);
         }
         if(this.shooterComplete)
         {
            dest.x = 314;
            dest.y = 122;
            this.bgBmp.bitmapData.copyPixels(new ExpertShooter().bitmapData,rect,dest,null,null,true);
         }
         if(this.marathonComplete)
         {
            dest.x = 482;
            dest.y = 126;
            this.bgBmp.bitmapData.copyPixels(new Marathon().bitmapData,rect,dest,null,null,true);
         }
         if(this.allEasyComplete)
         {
            dest.x = 147;
            dest.y = 272;
            this.bgBmp.bitmapData.copyPixels(new PieceOfCake().bitmapData,rect,dest,null,null,true);
         }
         if(this.gongfuComplete)
         {
            dest.x = 314;
            dest.y = 271;
            this.bgBmp.bitmapData.copyPixels(new KungfuPanda().bitmapData,rect,dest,null,null,true);
         }
         if(this.nearMissComplete)
         {
            dest.x = 148;
            dest.y = 130;
            this.bgBmp.bitmapData.copyPixels(new EpicEscape().bitmapData,rect,dest,null,null,true);
         }
         this.bgBmp.visible = true;
         if(this.bgBmp.stage != null)
         {
            this.bgBmp.stage.addEventListener(MouseEvent.MOUSE_DOWN,this.press);
         }
      }
      
      public function hidePanel() : void
      {
         if(this.bgBmp.stage != null)
         {
            this.bgBmp.stage.removeEventListener(MouseEvent.MOUSE_DOWN,this.press);
         }
         if(this.bgBmp.bitmapData != null)
         {
            this.bgBmp.bitmapData.dispose();
         }
         this.bgBmp.visible = false;
         this.infro.visible = false;
         this.cross.visible = false;
         this.icon.visible = false;
      }
      
      private function press(evt:MouseEvent) : void
      {
         var x:int = evt.stageX;
         var y:int = evt.stageY;
         if((x > 154) && (x < 314) && (y > 148) && (y < 298))
         {
            this.showIntro(0);
         }
         else if((x > 340) && (x < 460) && (y > 140) && (y < 275))
         {
            this.showIntro(1);
         }
         else if((x > 490) && (x < 625) && (y > 143) && (y < 278))
         {
            this.showIntro(2);
         }
         else if((x > 172) && (x < 292) && (y > 313) && (y < 423))
         {
            this.showIntro(3);
         }
         else if((x > 330) && (x < 460) && (y > 303) && (y < 433))
         {
            this.showIntro(4);
         }
         else if((x > 503) && (x < 628) && (y > 305) && (y < 425))
         {
            this.showIntro(5);
         }
         else
         {
            return;
         }
         
         
         
         
         
         evt.target.removeEventListener(MouseEvent.MOUSE_DOWN,this.press);
      }
      
      private var IntroClass:Vector.<Class>;
      
      private var infro:Bitmap;
      
      private var cross:Bitmap;
      
      private function showIntro(index:int) : void
      {
         this.infro.bitmapData = new this.IntroClass[index]().bitmapData;
         this.infro.x = 800 - this.infro.width >> 1;
         this.infro.y = (480 - this.infro.height >> 1) + 50;
         this.infro.visible = true;
         this.cross.x = this.infro.x + this.infro.width - this.cross.width / 2;
         this.cross.y = this.infro.y - this.cross.width / 2;
         this.cross.visible = true;
         if(this.infro.stage != null)
         {
            this.infro.stage.addEventListener(MouseEvent.MOUSE_DOWN,this.hideIntro);
         }
      }
      
      private function hideIntro(evt:Event) : void
      {
         evt.target.removeEventListener(MouseEvent.MOUSE_DOWN,this.hideIntro);
         evt.target.addEventListener(MouseEvent.MOUSE_DOWN,this.press);
         this.infro.visible = false;
         this.cross.visible = false;
      }
   }
}
