package game
{
   import interf.ISteper;
   import flash.display.BitmapData;
   import flash.geom.Rectangle;
   import flash.geom.Point;
   import interf.FpsGame;
   import flash.display.Sprite;
   import flash.display.Bitmap;
   import flash.display.DisplayObjectContainer;
   import flash.events.MouseEvent;
   
   public class Help extends Object implements ISteper
   {
      
      public function Help(goStart:Function)
      {
         this.bitmap = new Bitmap();
         super();
         this.startFunc = goStart;
      }
      
      private static var HelpBmp1:Class = Help_HelpBmp1;
      
      private static var HelpBmp2:Class = Help_HelpBmp2;
      
      private static var BtnBackBmp:Class = Help_BtnBackBmp;
      
      public static const src3:BitmapData = new BtnBackBmp().bitmapData;
      
      private static var bmpds:Vector.<BitmapData> = new Vector.<BitmapData>(12);
      
      public static function init() : void
      {
         var bmpd:BitmapData = null;
         var src1:BitmapData = new HelpBmp1().bitmapData;
         var src2:BitmapData = new HelpBmp2().bitmapData;
         var rect:Rectangle = new Rectangle(0,0,src1.width,src1.height / 6);
         var dest:Point = new Point();
         var i:int = 0;
         while(i < 6)
         {
            bmpd = new BitmapData(rect.width,rect.height);
            rect.y = rect.height * i;
            bmpd.copyPixels(src1,rect,dest);
            bmpds[i] = bmpd;
            bmpd = new BitmapData(rect.width,rect.height);
            bmpd.copyPixels(src2,rect,dest);
            bmpds[i + 6] = bmpd;
            i++;
         }
         src1.dispose();
         src2.dispose();
         src1 = null;
         src2 = null;
      }
      
      public var panda:Panda;
      
      public var scene:Scene;
      
      public var engine:FpsGame;
      
      private var btnBack:Sprite;
      
      private var startFunc:Function;
      
      private var bitmap:Bitmap;
      
      public function get helping() : Boolean
      {
         return this.bitmap.visible;
      }
      
      public function showAs(dpoc:DisplayObjectContainer) : void
      {
         this.bitmap.visible = false;
         dpoc.addChild(this.bitmap);
         var tmpBMP:Bitmap = new Bitmap(new BitmapData(41,48,true,0));
         tmpBMP.bitmapData = src3;
         tmpBMP.x = 20;
         tmpBMP.y = 10;
         this.btnBack = new Sprite();
         this.btnBack.addChild(tmpBMP);
         dpoc.addChild(this.btnBack);
         this.btnBack.addEventListener(MouseEvent.CLICK,this.backHandler);
         this.btnBack.visible = false;
      }
      
      private function backHandler(event:MouseEvent) : void
      {
         this.btnBack.visible = false;
         this.bitmap.visible = false;
         this.panda.disctrl = false;
         this.stepFunc = null;
         this.engine.stop();
         this.startFunc();
      }
      
      private var stepFunc:Function;
      
      private var nextStep:Function;
      
      private var passFunc:Function;
      
      private var retryFunc:Function;
      
      private var retryStep:Function;
      
      public function step() : void
      {
         if(this.stepFunc != null)
         {
            this.stepFunc();
         }
      }
      
      public function start() : void
      {
         init();
         this.step1();
         this.btnBack.visible = true;
         this.bitmap.visible = true;
         this.panda.disctrl = true;
         this.bitmap.y = 480 - this.bitmap.height;
         this.bitmap.x = 800 - this.bitmap.width >> 1;
      }
      
      public function close() : void
      {
         this.bitmap.visible = false;
      }
      
      private var nextLock:Boolean;
      
      public function next(e:MouseEvent) : void
      {
         if((this.helping) && (!this.nextLock))
         {
            this.nextStep();
         }
      }
      
      private function step1() : void
      {
         this.nextLock = false;
         this.bitmap.bitmapData = bmpds[3];
         this.stepFunc = this.auto_step1;
         this.nextStep = this.step2;
         this.board = this.panda.board;
         this.scene.updateForHelp(2);
      }
      
      private function auto_step1() : void
      {
         if((this.panda.board.rect.right - this.panda.rect.right < 500) && (this.panda.isRunning()))
         {
            this.engine.pause();
         }
      }
      
      private function step2() : void
      {
         this.retryFunc = this.step2;
         this.bitmap.bitmapData = bmpds[4];
         this.retryStep = this.stepFunc = this.auto_step2;
         this.passFunc = this.pass_step2;
         this.nextStep = this.nextForYou;
         this.scene.updateForHelp(this.sceneLevel = 2);
         this.board = null;
         this.nextLock = true;
         this.engine.play();
      }
      
      private function auto_step2() : void
      {
         this.checkUnlock1();
         if((this.panda.board.rect.right - this.panda.rect.right < 150) && (this.panda.isRunning()))
         {
            this.panda.jump();
         }
      }
      
      private var board:Board;
      
      private function pass_step2() : void
      {
         if(this.board == null)
         {
            this.board = this.panda.board;
         }
         if(this.board != this.panda.board)
         {
            this.goodJob(this.step3);
         }
      }
      
      private function step3() : void
      {
         this.retryFunc = this.step3;
         this.bitmap.bitmapData = bmpds[5];
         this.retryStep = this.stepFunc = this.auto_step3;
         this.passFunc = this.pass_step3;
         this.nextStep = this.nextForYou;
         this.scene.updateForHelp(this.sceneLevel = 3);
         this.board = null;
         this.nextLock = true;
         this.engine.play();
      }
      
      private function auto_step3() : void
      {
         this.checkUnlock1();
         if(this.panda.willFly)
         {
            return;
         }
         if(this.panda.isRunning())
         {
            if(this.panda.board.rect.right - this.panda.rect.right < 100)
            {
               this.panda.jump();
            }
         }
         else if(this.panda.board.rect.right - this.panda.rect.right < -150)
         {
            this.panda.somersault();
         }
         
      }
      
      private function pass_step3() : void
      {
         if(this.board == null)
         {
            this.board = this.panda.board;
         }
         if(this.board != this.panda.board)
         {
            this.goodJob(this.step4);
         }
      }
      
      private function step4() : void
      {
         this.bitmap.bitmapData = bmpds[6];
         this.nextStep = this.step4_1;
      }
      
      private function step4_1() : void
      {
         this.bitmap.bitmapData = bmpds[7];
         this.nextStep = this.step4_2;
      }
      
      private function step4_2() : void
      {
         this.retryFunc = this.step4_2;
         this.bitmap.bitmapData = bmpds[8];
         this.retryStep = this.stepFunc = this.auto_step4;
         this.passFunc = this.pass_step4;
         this.nextStep = this.nextForYou;
         this.scene.updateForHelp(this.sceneLevel = 4);
         this.board = null;
         this.nextLock = true;
         this.panda.sp = Panda.sp;
         this.engine.play();
      }
      
      private function auto_step4() : void
      {
         this.checkUnlock1();
         if(this.panda.isRunning())
         {
            if(this.panda.board.rect.right - this.panda.rect.right < 100)
            {
               this.panda.jump();
            }
         }
         else if(this.panda.rect.x - this.panda.board.rect.right > 1000)
         {
            this.panda.unfly();
         }
         else if(this.panda.rect.x - this.panda.board.rect.right > 500)
         {
            if(this.panda.willFly)
            {
               this.panda.fly();
            }
         }
         else if(this.panda.rect.x - this.panda.board.rect.right > 150)
         {
            if(!this.panda.willFly)
            {
               this.panda.somersault();
            }
         }
         
         
         
      }
      
      private function pass_step4() : void
      {
         if(this.board == null)
         {
            this.board = this.panda.board;
         }
         if(this.board != this.panda.board)
         {
            this.goodJob(this.step5);
         }
      }
      
      private function step5() : void
      {
         this.bitmap.bitmapData = bmpds[9];
         this.nextStep = this.step5_1;
      }
      
      private function step5_1() : void
      {
         this.retryFunc = this.step5_1;
         this.bitmap.bitmapData = bmpds[10];
         this.nextStep = this.nextForYou;
         this.retryStep = this.stepFunc = this.auto_step5;
         this.passFunc = this.pass_step5;
         this.scene.updateForHelp(this.sceneLevel = 5);
         this.board = null;
         this.nextLock = true;
         this.panda.numShoots = 3;
         this.engine.play();
      }
      
      private function auto_step5() : void
      {
         this.checkUnlock1();
         if((this.panda.board.rect.right - this.panda.rect.right < 180) && (this.panda.isRunning()))
         {
            this.board = this.panda.board;
            this.panda.jump();
         }
         if(this.panda.board.enemy != null)
         {
            if(this.panda.board.enemy.rect.x - this.panda.rect.right < 500)
            {
               if(!this.panda.shooting)
               {
                  this.panda.shoot();
               }
            }
         }
      }
      
      private function pass_step5() : void
      {
         if((this.panda.board.enemy.rect.x > this.panda.rect.x) && (this.panda.board.enemy.isBruised))
         {
            this.congratulation();
         }
      }
      
      private function congratulation() : void
      {
         this.bitmap.bitmapData = bmpds[11];
         this.nextStep = this.quit;
         this.stepFunc = this.autoStep;
         this.panda.disctrl = true;
      }
      
      private function autoStep() : void
      {
         if((this.panda.board.rect.right - this.panda.rect.right < 180) && (this.panda.isRunning()))
         {
            this.board = this.panda.board;
            this.panda.jump();
         }
         if(this.panda.board.enemy != null)
         {
            if((!this.panda.board.enemy.isBruised) && (this.panda.board.enemy.rect.x - this.panda.rect.right < 500))
            {
               if(!this.panda.shooting)
               {
                  this.panda.shoot();
               }
            }
         }
      }
      
      public function quit() : void
      {
         if(this.bitmap.stage != null)
         {
            this.bitmap.stage.removeEventListener(MouseEvent.MOUSE_UP,this.next);
         }
         this.bitmap.visible = false;
         this.panda.disctrl = false;
         this.stepFunc = null;
         this.engine.stop();
         this.startFunc();
      }
      
      private var sceneLevel:int;
      
      private function nextForYou() : void
      {
         this.bitmap.bitmapData = bmpds[0];
         this.stepFunc = null;
         this.nextStep = this.ctrlForYou;
      }
      
      private function ctrlForYou() : void
      {
         this.bitmap.bitmapData = null;
         this.panda.disctrl = false;
         this.stepFunc = this.passFunc;
         this.board = null;
         this.engine.play();
      }
      
      private function goodJob(next:Function) : void
      {
         this.bitmap.bitmapData = bmpds[1];
         this.nextStep = next;
         this.stepFunc = this.goodJobStep;
         this.panda.disctrl = true;
         this.nextLock = true;
      }
      
      private function goodJobStep() : void
      {
         this.checkUnlock1();
      }
      
      private function checkUnlock1() : void
      {
         if(this.board == null)
         {
            this.board = this.panda.board;
         }
         if((this.panda.rect.x - this.panda.board.rect.x > 1000) && (!(this.board == this.panda.board)))
         {
            this.engine.pause();
            this.nextLock = false;
            this.board = null;
         }
      }
      
      public function retry() : void
      {
         this.bitmap.bitmapData = bmpds[2];
         this.nextStep = this.stepAgain;
         this.nextLock = false;
      }
      
      private function stepAgain() : void
      {
         this.bitmap.bitmapData = null;
         this.retryFunc();
         this.engine.start();
         this.panda.start();
         this.panda.disctrl = true;
         this.nextLock = true;
      }
   }
}
