import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import Transform3D = Laya.Transform3D;
import {LevelController} from "../Game/LevelController";
import Dictionary from "../Util/Dictionary";
import { GamePool, PoolType } from "../Game/GamePool";
import {MainRoleManager} from "./MainRoleManager";
import {MateManager} from "./MateManager";
import { Opponent } from "../Game/Opponent";
import {GameManager} from "../Game/GameManager";
import {PlaneManager} from "./PlaneManager";
import {MouseEvent} from "../Game/MouseEvent";

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 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;//几针创建一个敌人
    
    Create(hp, texture) {
        let e = GamePool.Instance.GetModel(GamePool.modeltype.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.InitModel(sp, hp, texture);
        this.enemyDic.set(e.id, ps);
    }
    
    public CheckGameOver() {
        if (this.enemyDic.size == 0) {
            MouseEvent.Instance.OnClickUp();
            MainRoleManager.Instance.CreateOver();
            GameManager.Instance.Congratulate();
        }
    }

    GetOpponent(): 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.Recovery();
            }
        }
    }
    public CreateOpponent(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.Create, [hp, texture], false);
        }
    }

    /****************************************************************************** */
    private createCoralRect(): void {
        var coralRect: Sprite = new Sprite();
        coralRect.graphics.drawRect(0, 0, Laya.stage.width, Laya.stage.height / 2, "#FF7F50");

        //设置名称
        coralRect.name = "珊瑚色容器";
        coralRect.size(Laya.stage.width, Laya.stage.height / 2);
        Laya.stage.addChild(coralRect);
    }

    private createDeepSkyblueRect(): void {
        var deepSkyblueRect: Sprite = new Sprite();
        deepSkyblueRect.graphics.drawRect(0, 0, 100, 100, "#00BFFF");
        //设置名称
        deepSkyblueRect.name = "天蓝色矩形";
        //设置宽高（要接收鼠标事件必须设置宽高，否则不会被命中）  
        deepSkyblueRect.size(100, 100);
        deepSkyblueRect.pos(10, 10);
        Laya.stage.addChild(deepSkyblueRect);
    }
    /****************************************************************************** */


    public GetTar(): Sprite3D {
        let p = MainRoleManager.Instance.NearIsdead();
        if (!p) {
            if (Math.random() > 0.5) {
                return MainRoleManager.Instance.playerScript.body;
            } else {
                let s = MateManager.Instance.GetSprite();

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

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


}