import Sprite3D = Laya.Sprite3D;
import Transform3D = Laya.Transform3D;
import {LevelController} from "../Game/LevelController";
import { GlobalParam } from "../Net/GlobalParam";
import {MainRoleManager} from "./MainRoleManager";
import {MateManager} from "./MateManager";
import {RoleManager} from "./RoleManager";
import Vector3 = Laya.Vector3;
import { DispatcherManager, EventType } from "../Game/DispatcherManager";
import {GameManager} from "../Game/GameManager";

import Sprite = Laya.Sprite;
import Stage = Laya.Stage;
import Text = Laya.Text;
import Event = Laya.Event;
import Browser = Laya.Browser;
import WebGL = Laya.WebGL;

export  class PlaneManager {
    private static instance: PlaneManager;
    public static get Instance(): PlaneManager {
        return this.instance || (this.instance = new PlaneManager());
    }

    public finalTra: Transform3D;
    public jumpCamTra: Transform3D;
    public FinalBlock: Sprite3D;
    public EnemySpawnPoint: Transform3D;
    public PlayerDancePoint: Sprite3D;
    public helicopter: Sprite3D;
    public toured: Transform3D;
    public toured1: Vector3;
    public toured2: Vector3;
    public Init(finalP: Sprite3D) {
        if (!finalP) {
            console.error("飞机不存在"); return;
        }
        this.finalTra = finalP.transform;
        this.jumpCamTra = (finalP.getChildByName("jumpCamPos") as Sprite3D).transform;
        this.FinalBlock = finalP.getChildAt(0) as Sprite3D;
        this.EnemySpawnPoint = (this.FinalBlock.getChildByName("EnemySpawnPoint") as Sprite3D).transform;
        this.PlayerDancePoint = (this.FinalBlock.getChildByName("PlayerDancePoint") as Sprite3D);
        this.helicopter = (this.FinalBlock.getChildByName("helicopter") as Sprite3D);
        this.toured = (this.FinalBlock.getChildByName("toured") as Sprite3D).transform;
        this.FinalBlock.active = false;
    }

    private winPos: number;
    private airisshow = false;
    private stageProLen: number;
    SetAir() {
        this.danceindex = 0;
        this.airisshow = false;
        this.FinalBlock.active = false;
        let count = LevelController.Instance._level.road_list.length;
        this.winPos = (count) * GlobalParam.barrierRoadLength;
        this.finalTra.localPositionZ = this.winPos;
        this.stageProLen = this.winPos - MainRoleManager.Instance.playerStartTra.localPositionZ;
        let isgold = LevelController.Instance._level.isgold;
        this.helicopter.active = isgold;//todo
        this.toured1 = (this.FinalBlock.getChildByName("toured1") as Sprite3D).transform.position.clone();
        this.toured2 = (this.FinalBlock.getChildByName("toured2") as Sprite3D).transform.position.clone();
    }
/************************************************************************** */
    private logger: Text;
    private createDarkOrchidRect(): void {
        var darkOrchidRect: Sprite = new Sprite();
        darkOrchidRect.name = "暗紫色矩形容器";
        darkOrchidRect.graphics.drawRect(-100, -100, 200, 200, "#9932CC");

        darkOrchidRect.pos(Laya.stage.width / 2, Laya.stage.height / 2);
        Laya.stage.addChild(darkOrchidRect);
        // 为true时，碰撞区域会被修正为实际显示边界
        // mouseThrough命名真是具有强烈的误导性
        darkOrchidRect.mouseThrough = true;
    }
    private createLogger(): void {
        this.logger = new Text();
        this.logger.size(Laya.stage.width, Laya.stage.height);
        this.logger.align = 'right';
        this.logger.fontSize = 20;
        this.logger.color = "#FFFFFF";
        Laya.stage.addChild(this.logger);
    }
/************************************************************************** */


    public CheckPlayerRoadPos(_zp: number) {
        let d = this.winPos - _zp;
        if (d <= 60 && !this.airisshow) {
            this.ShowAir();
            return;
        }
        DispatcherManager.event(EventType.Event.SetStagePro, (this.stageProLen - d) / this.stageProLen);
        if (d <= 0) {
            this.NearDestination();
        }
    }
    ShowAir() {
        this.airisshow = true;
        this.FinalBlock.active = true;
    }
    private NearDestination () {
        MainRoleManager.Instance.Destination();
        if (LevelController.Instance._level.isgold) {
            GameManager.Instance.Congratulate(true);
        } else {
            let count = 0;
            let isdesd = MainRoleManager.Instance.NearIsdead();
            if (!isdesd) { count++; }
            let num = MateManager.Instance.NearDestination();
            count += num;
            if (count == 0) return;
            MateManager.Instance.Near(count, this.toured.position.z);
            if (!isdesd) MainRoleManager.Instance.Near(count, this.toured.position.z);
            Laya.timer.once(1300, this, this.CreateOpponent);
        }
    }

    CreateOpponent() {
        MainRoleManager.Instance.Attack();
        MateManager.Instance.Attack();
        RoleManager.Instance.CreateOpponent(this.EnemySpawnPoint.position.clone());
    }

    private danceindex = 0;
    public GetWuDaoPosition() {
        if (this.danceindex > this.PlayerDancePoint.numChildren - 1) return null;
        let t = (this.PlayerDancePoint.getChildAt(this.danceindex) as Sprite3D).transform;
        this.danceindex++;
        return t;
    }

}