import Sprite3D = Laya.Sprite3D;
import Transform3D = Laya.Transform3D;
import LevelController from "../GameLogic/LevelController";
import { GlobalVariable } from "../game_module/GlobalVariable";
import MainRoleManager from "./MainRoleManager";
import MateManager from "./MateManager";
import RoleManager from "./RoleManager";
import Vector3 = Laya.Vector3;
import { DispatcherManager, EventType } from "../GameLogic/DispatcherManager";
import GameManager from "../GameLogic/GameManager";
export default 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 airCoinPos: Transform3D;
    // public airEnemyPos: Transform3D;
    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.airCoinPos = (this.FinalBlock.getChildByName("airCoinPos") as Sprite3D).transform;
        // this.airEnemyPos = (this.FinalBlock.getChildByName("airEnemyPos") 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) * GlobalVariable.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();
        // if (isgold) {
        //     this.helicopter.transform.position = this.airCoinPos.position.clone();
        // } else {
        //     this.helicopter.transform.position = this.airEnemyPos.position.clone();
        // }

    }

    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.ArriveFinal();
        }

    }
    ShowAir() {
        this.airisshow = true;
        this.FinalBlock.active = true;
    }
    private ArriveFinal() {
        MainRoleManager.Instance.ArriveFinal();
        if (LevelController.Instance._level.isgold) {
            GameManager.Instance.ToDance(true);
        } else {
            let count = 0;
            let isdesd = MainRoleManager.Instance.ArriveIsdead();
            if (!isdesd) { count++; }
            let num = MateManager.Instance.GetArrive();
            count += num;
            if (count == 0) return;
            MateManager.Instance.ToArrive(count, this.toured.position.z);
            if (!isdesd) MainRoleManager.Instance.ToArrive(count, this.toured.position.z);
            Laya.timer.once(1300, this, this.CreateEnemy);
        }
    }
    CreateEnemy() {
        MainRoleManager.Instance.StartAtkEnemy();
        MateManager.Instance.StartAtkEnemy();
        RoleManager.Instance.CreateEnemy(this.EnemySpawnPoint.position.clone());

    }


    private danceindex = 0;
    public GetDancePos() {
        if (this.danceindex > this.PlayerDancePoint.numChildren - 1) return null;
        let t = (this.PlayerDancePoint.getChildAt(this.danceindex) as Sprite3D).transform;
       // console.log("sp.owner.id:", this.danceindex);
        this.danceindex++;
        return t;
    }


    // public Reset() {
    //     this.airisshow = false;
    //     this.FinalBlock.active = false;
    // }


}