import { instantiate, LabelComponent, Node, Prefab, Size, UITransformComponent, Vec2, Vec3, view } from "cc";
import { NoticeViewScript } from "./NoticeViewScript";



export class NoticeManager
{
    private static startPos:Vec3[]=[];
    private static root:Node;

    private static ViewMap:Map<string,Prefab>;
    /**
     * 池
     */
    private static Pool:Map<string,NoticeViewScript[]>;

    public static Init(root:Node,viewMap:Map<string,Prefab>):void{
        this.root=root;
        this.ViewMap=viewMap;
        this.Pool=new Map<string,NoticeViewScript[]>();

        let pos:Vec3;
        let viewSize:Size=view.getVisibleSize();
        let w:number=viewSize.width/3;
        let helfW:number=w/2;
        let h:number=viewSize.height/3;
        let helfH:number=h/2;
        for (let yIndex = 0; yIndex < 3; yIndex++) {
            for (let xIndex = 0; xIndex < 3; xIndex++) {
               pos=new Vec3(xIndex*w+helfW,yIndex*h,0);
               this.startPos.push(pos);
            }
        }
    }

    /**
     * 显示提示
     * @param data 提示内容
     * @param startPos 开始位置
     * @param type 类型
     * @param moveType 移动类型
     * @param time 时间
     * @param dis  距离
     */
    public static ShowPromptByPos(data:any,startPos:Vec3,type:string="Text",moveType:number=1,time:number=1000,dis:number=100):void{
        let viewScript:NoticeViewScript=this.Create(type);
        viewScript.type=type;
        viewScript.data=data;
        viewScript.moveType=moveType;
        viewScript.startPos=startPos;
        viewScript.time=time;
        viewScript.dis=dis;
        viewScript.StartMove(data);

        viewScript.node.setPosition(viewScript.startPos);
        this.root.addChild(viewScript.node);
    }

    /**
     * 显示提示
     * @param data          提示内容
     * @param posType       位置类型678，345，012
     * @param type          类型
     * @param moveType      移动类型
     * @param time          持续时间
     * @param dis           移动距离
     */
    public static ShowPrompt(data:any,posType:number=7,type:string="Text",moveType:number=1,time:number=1000,dis:number=100):void{
        let startPos:Vec3=this.startPos[posType];
        this.ShowPromptByPos(data,startPos,type,moveType,time,dis);
    }


    public static Create(type:string):NoticeViewScript{
        let list:NoticeViewScript[];
        if(this.Pool.get(type)!=null){
            list=this.Pool.get(type);
            if(list.length>0){
                return list.shift();
            }
        }
        if(this.ViewMap.has(type)==false){
            throw new Error("找不到对应的Notice Prefab"+type);
        }
        let prefab:Prefab=this.ViewMap.get(type);
        let node:Node=instantiate(prefab);
        let result:NoticeViewScript=node.getComponent(NoticeViewScript);
        if(result==null){
            throw new Error("节点上找不到NoticeViewScript脚本！");
        }
        return result;
    }
    
    public static recycle(value:NoticeViewScript):void{
        this.root.removeChild(value.node);
        let list:NoticeViewScript[];
        if(this.Pool.get(value.type)!=null){
            list=this.Pool.get(value.type);
        }else{
            list=[];
            this.Pool.set(value.type,list);
        }
        if(list.indexOf(value)>=0){
            throw new Error("重复回收！");
        }
        list.push(value);
    }
}