import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import Transform3D = Laya.Transform3D;
import { GamePool } from "../GameLogic/GamePool";
import Dictionary from "../Util/Dictionary";
import MainRoleManager from "./MainRoleManager";
import Mate from "../GameLogic/Mate";
import { PlayerManager } from "../GameLogic/PlayerManager";
import { GlobalVariable } from "../game_module/GlobalVariable";
import { Mathf } from "../Util/Mathf";
import GameTool from "../Util/GameTool";
import PlaneManager from "./PlaneManager";
import LevelController from "../GameLogic/LevelController";
import {  MusicType } from "../GameLogic/ColliderAndMusicType";
export default class MateManager {
    private static instance: MateManager;
    public static get Instance(): MateManager {
        return this.instance || (this.instance = new MateManager());
    }
    constructor() {
        this.friendDic = new Dictionary();
        this.getDic = new Dictionary();
    }
    private getDic: Dictionary;
    private friendDic: Dictionary;
    // static bulletAttack = 10;//攻击力
    //    static bulletTime = 0.5;//子弹的间隔时间
    /**
     * 创建friend
     */
    CreateFriend(tra: Transform3D) {
        // let dataPar = MainRoleManager.Instance.GetFrindPar();
        // if (!dataPar) { console.warn("friendPar没有位置了"); return; }
        let p = GamePool.Instance.SetActiveObject(GamePool.pooltype.player + PlayerManager.Instance.equipId);
        if (p) {
            p.transform.position = tra.position.clone();
            p.transform.rotationEuler = tra.rotationEuler.clone();//new Vector3(0,90,0);
            MainRoleManager.Instance.playerContainer.addChild(p);

            let ps: Mate = p.getComponent(Mate);
            if (!ps) ps = p.addComponent(Mate);
            ps.SetTar();
            this.friendDic.set(p.id, ps);
            return ps;
        }
        return null;
    }

    /**
     * 试用
     */
    CreateTryOut() {
        for (let index = 0; index <3; index++) {
           Laya.timer.frameOnce(index,this,this._createtryout,null,false);
            
        }
    }
    _createtryout(){
        let dataPar = MainRoleManager.Instance.GetFrindPar();
        if (!dataPar) { console.warn("friendPar没有位置了"); return; }
        let p = GamePool.Instance.SetActiveObject(GamePool.pooltype.player + PlayerManager.Instance.equipId);
        if (p) {
            let tar = dataPar["sp"];
            tar.addChild(p);
            p.transform.localPosition = Mathf.VecZero;
            p.transform.localRotationEuler = Mathf.VecZero;
            let ps: Mate = p.getComponent(Mate);
            if (!ps) ps = p.addComponent(Mate);
            ps.TryAddFriend(dataPar);
            this.friendDic.set(p.id, ps);
        }
    }
    //试用人物播放动画
    TryOutFriendPlayAni(){
        for (const key in  this.friendDic.container) {
            const element = this.friendDic.get(key) as Mate;
            element.CreateBullet();
            element.PlayAni(MusicType.RunAndGun, 2);
        }
    }
    

    /**
      *回收friend
      */
    RemoveFriend(sp: Sprite3D) {
        this.friendDic.delete(sp.id);
    }
    AddGetDic(id, fr: Mate) {
        console.log(22222222);
        this.getDic.set(id, fr);
    }
    RemoveGet(sp: Sprite3D) {
        this.getDic.delete(sp.id);
    }

    /**
     * 到达终点
     */
    GetArrive() {
        return this.getDic.size;
    }
    private v3 = Mathf.VecZero;
    ToArrive(count: number, zs: number) {
        if (count == 1) {
            for (const key in this.getDic.container) {
                const element = this.getDic.get(key) as Mate;
                this.v3.x = 0;
                element.ToStand(this.v3.clone());
            }
            return;
        }
        let len = 2 * GlobalVariable.arriveStandWidth;
        let avr = len / (count - 1);
        let index = 0;
        this.v3.z = zs;
        for (const key in this.getDic.container) {
            const element = this.getDic.get(key) as Mate;
            this.v3.x = GlobalVariable.arriveStandWidth - index * avr;
            element.ToStand(this.v3.clone());
            // console.log(this.v3.x);
            index++;
        }
    }

    /**
     * 开始攻击
     */
    StartAtkEnemy() {
        for (const key in this.getDic.container) {
            const element = this.getDic.get(key) as Mate;
            element.StartAtkEnemy();
        }
    }
    GetGetSp() {
        let count = this.getDic.size;
        let a = GameTool.randomInt(1, count);
        let tindex = 1;
        for (const key in this.getDic.container) {
            if (tindex == a) {
                const element = this.getDic.get(key) as Mate;
                return element.selfobj;
            }
            tindex++;
        }
        return null;

    }

    GoToDance() {
        for (const key in this.getDic.container) {
            let t = PlaneManager.Instance.GetDancePos();
            if (!t) return;
            const element = this.getDic.get(key) as Mate;
            element.Dance(t);
        }
    }

    GoToDance1() {
        for (const key in this.getDic.container) {
            let t = PlaneManager.Instance.GetDancePos();
            const element = this.getDic.get(key) as Mate;
            element.SetPar();
            if (t) {
                element.Dance(t);
            }

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