import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import Transform3D = Laya.Transform3D;
import { GamePool } from "../Game/GamePool";
import Dictionary from "../Util/Dictionary";
import {MainRoleManager} from "./MainRoleManager";
import {Mate} from "../Game/Mate";
import { PlayerManager } from "../Game/PlayerManager";
import { GlobalParam } from "../Net/GlobalParam";
import { MathTool } from "../Util/MathTool";
import {GameTool} from "../Util/GameTool";
import {PlaneManager} from "./PlaneManager";
import {LevelController} from "../Game/LevelController";
import {  MusicType } from "../Game/ColliderAndMusicType";

import Sprite = Laya.Sprite;
import Stage = Laya.Stage;
import Event = Laya.Event;
import Rectangle = Laya.Rectangle;
import Texture = Laya.Texture;
import Browser = Laya.Browser;
import Handler = Laya.Handler;
import WebGL = Laya.WebGL;

export  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;//子弹的间隔时间
    
    /**
     * 试用
     */
    Trial() {
        for (let index = 0; index <3; index++) {
           Laya.timer.frameOnce(index,this,this._createtrialmodel,null,false);
        }
    }
    //试用人物播放动画
    TrialModelPlayAni(){
        for (const key in  this.friendDic.container) {
            const element = this.friendDic.get(key) as Mate;
            element.CreateShoot();
            element.AnimatorPlayByType(MusicType.RunAndGun, 2);
        }
    }
    
    /**
      *回收friend
      */
    RecoverMateModel(sp: Sprite3D) {
        this.friendDic.delete(sp.id);
    }
    AddModelToDic(id, fr: Mate) {
        this.getDic.set(id, fr);
    }
    RemoveModelToDic(sp: Sprite3D) {
        this.getDic.delete(sp.id);
    }

    /**
     * 创建friend
     */
    MadeMate(tra: Transform3D) {
        let p = GamePool.Instance.GetModel(GamePool.modeltype.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;
    }

/************************************************************************* */
    private ApePath: string = "res/apes/monkey2.png";
    private ape: Sprite;
    private dragRegion: Rectangle;
    private showDragRegion(): void {
        //拖动限制区域
        var dragWidthLimit: number = 350;
        var dragHeightLimit: number = 200;
        this.dragRegion = new Rectangle(Laya.stage.width - dragWidthLimit >> 1, Laya.stage.height - dragHeightLimit >> 1, dragWidthLimit, dragHeightLimit);

        //画出拖动限制区域
        Laya.stage.graphics.drawRect(
            this.dragRegion.x, this.dragRegion.y, this.dragRegion.width, this.dragRegion.height,
            null, "#FFFFFF", 2);
    }

    private onStartDrag(e: Event): void {
        //鼠标按下开始拖拽(设置了拖动区域和超界弹回的滑动效果)
        this.ape.startDrag(this.dragRegion, true, 100);
    }
/************************************************************************* */


    /**
     * 到达终点destination
     */
    NearDestination() {
        return this.getDic.size;
    }
    private v3 = MathTool.VecZero;
    Near(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.Defense(this.v3.clone());
            }
            return;
        }
        let len = 2 * GlobalParam.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 = GlobalParam.arriveStandWidth - index * avr;
            element.Defense(this.v3.clone());
            index++;
        }
    }

    /**
     * 开始攻击
     */
    Attack() {
        for (const key in this.getDic.container) {
            const element = this.getDic.get(key) as Mate;
            element.Attack();
        }
    }
    GetSprite() {
        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;

    }
    TiaoWu() {
        for (const key in this.getDic.container) {
            let t = PlaneManager.Instance.GetWuDaoPosition();
            if (!t) return;
            const element = this.getDic.get(key) as Mate;
            element.TiaoWu(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.Recover();
            }
        }
        console.log("this.friendDic.size", this.friendDic.size);
        this.getDic.clear();
    }

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

    _createtrialmodel(){
        let dataPar = MainRoleManager.Instance.GetMatePos();
        if (!dataPar) { console.warn("friendPar没有位置了"); return; }
        let p = GamePool.Instance.GetModel(GamePool.modeltype.Player + PlayerManager.Instance.equipId);
        if (p) {
            let tar = dataPar["sp"];
            tar.addChild(p);
            p.transform.localPosition = MathTool.VecZero;
            p.transform.localRotationEuler = MathTool.VecZero;
            let ps: Mate = p.getComponent(Mate);
            if (!ps) ps = p.addComponent(Mate);
            ps.TrialAdd(dataPar);
            this.friendDic.set(p.id, ps);
        }
    }
}