import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import Transform3D = Laya.Transform3D;
import LevelController from "../GameLogic/LevelController";
import Dictionary from "../Util/Dictionary";
import { GamePool, PoolType } from "../GameLogic/GamePool";
import MainRoleManager from "./MainRoleManager";
import MateManager from "./MateManager";
import { Opponent } from "../GameLogic/Opponent";
import GameManager from "../GameLogic/GameManager";
import PlaneManager from "./PlaneManager";
import MouseEvent from "../GameLogic/MouseEvent";
export default class RoleManager {
    private static instance: RoleManager;
    public static get Instance(): RoleManager {
        return this.instance || (this.instance = new RoleManager());
    }
    constructor() {
        this.enemyDic = new Dictionary();
    }


    public tarTran: Transform3D;

    private startPos: Vector3;

    private enemyDic: Dictionary;

    private frameCount = 10;//几针创建一个敌人
    public CreateEnemy(startPos: Vector3) {
        this.startPos = startPos;
        let count = LevelController.Instance._level.enemy;
        let hp = LevelController.Instance._level.enemy_hp;
        let texture =LevelController.Instance._level.enemy_cloth;
        for (let index = 0; index < count; index++) {
            let a = index;
            Laya.timer.frameOnce(a * this.frameCount, this, this._crete, [hp, texture], false);
        }

    }
    _crete(hp, texture) {
        let e = GamePool.Instance.SetActiveObject(GamePool.pooltype.enemy);
        if (!e) return;
        MainRoleManager.Instance.playerContainer.addChild(e);
        e.transform.position = this.startPos.clone();
        let ps: Opponent = e.getComponent(Opponent);
        if (!ps) ps = e.addComponent(Opponent);
        let sp = this.GetTar();
        ps.SetTar(sp, hp, texture);
        this.enemyDic.set(e.id, ps);
    }
    public GetTar(): Sprite3D {
        let p = MainRoleManager.Instance.ArriveIsdead();
        if (!p) {
            if (Math.random() > 0.5) {
                return MainRoleManager.Instance.playerBehavior.body;
            } else {
                let s = MateManager.Instance.GetGetSp();

                if (!s) return MainRoleManager.Instance.playerBehavior.body;
                else return s;
            }
        } else {
            let s = MateManager.Instance.GetGetSp();
            if (!s) return null;
            else return s;
        }
    }

    public RemoveEnemy(sp) {
        this.enemyDic.delete(sp.id);
        // console.log("sp.id",sp.id)
    }

    public CheackGame() {
        if (this.enemyDic.size == 0) {
            MouseEvent.Instance.OnClickUp();
            MainRoleManager.Instance.StopCreate();

            GameManager.Instance.ToDance();
        }
    }

    PlayerGetEnemy(): Sprite3D {

        for (const key in this.enemyDic.container) {
            const element = this.enemyDic.get(key) as Opponent;
            return element.selfobj;
        }
        return null;


    }

    Reset() {
        let arr = [];
        for (const key in this.enemyDic.container) {
            arr.push(key);
        }
        if (arr.length > 0) {
            for (let index = 0; index < arr.length; index++) {
                const element = arr[index];
                let s = this.enemyDic.get(element) as Opponent;
                if (s) s.Des();
            }
        }
        console.log("this.enemyDic.size", this.enemyDic.size);
    }


}