import { DispatcherManager } from "../utils/DispatcherManager";
import {PanelResDic} from "../PrefabControl/PanelResDic";
import {ComponentEventData} from "../Data/ComponentEventData";
import {GamePool} from "../PrefabControl/GamePool";

export class PanelChildControl extends fairygui.GComponent 
{
    /**
     * 事件容器
     */
    private EventResDic:any = {};
    /**
     * 全局事件容器
     */
    private EventGlobal:any = {};

    public constructor() {
        super();
    }

    /**
     * 移除所有组件事件
     */
    public ClearEvent():void
    {
        if(this.EventResDic)
        {
            var s;
            var dict:any = this.EventResDic;
            var arr:Array<ComponentEventData>;
            var i:number;
            var nums:number = 0;
            var vo:ComponentEventData;
            for( s in dict )
            {
                arr = dict[s];
                if( arr && arr.length > 0 )
                {
                    i=0;
                    nums = arr.length;
                    for( i=nums-1;i>=0;i-- )
                    {
                        vo = arr[i];
                        if( vo )
                        {
                            if( vo.target )
                            {
                                vo.target.off( vo.type,vo.thisObject,vo.listener );
                            }
                            vo.clear();
                        }
                        arr.splice( i,1 );
                    }
                }
                delete this.EventResDic[s];
            }
        }
        this.EventResDic = {};
    }

    public dispose():void
    {
        this.ClearGlobalEvent();
        this.ClearEvent();
        super.dispose();
    }

    public constructFromXML(xml: any): void {
        super.constructFromXML(xml);
        for (var str in this._children) {
            var item: fairygui.GObject = this._children[str];
            this[item.name] = item;
        }
        this.onInit();
    }

    protected onInit(): void {

    }

    /*获取管理数据模块*/
    public getProxy(d: any): any {
        return PanelResDic.Instance.getSingleTon(d);
    }

    protected dispacheDatatrigger(type: string, data: any = null): void {
        DispatcherManager.dispatchEvent(type, data);
    }

    protected hasDataEvent(type: string): boolean {
        return DispatcherManager.hasEventListener(type);
    }

    /**
     * 注册全局事件
     * @param type 
     * @param thisObject 
     * @param listener 
     */
    protected AddEventData(type: string, thisObject: any, listener: Function): void {
        if( !this.EventGlobal.hasOwnProperty( type ) )
        {
            var vo:ComponentEventData = GamePool.GetItem( ComponentEventData.ResDicValue );
            this.EventGlobal[type] = vo;
            vo.type = type;
            vo.thisObject = thisObject;
            vo.listener = listener;
            DispatcherManager.addEventListener(type,thisObject,listener);
        }
    }

    /******************************************** */
        private DIALOG_WIDTH: number = 220;
        private DIALOG_HEIGHT: number = 275;
        private CLOSE_BTN_WIDTH: number = 43;
        private CLOSE_BTN_PADDING: number = 5;
        private assets: Array<string>;

        private onSkinLoadComplete(): void {
            var dialog:Laya. Dialog = new Laya.Dialog();

            var bg:Laya.Image = new Laya.Image(this.assets[0]);
            dialog.addChild(bg);
            var button:Laya.Button = new Laya.Button(this.assets[1]);
            // button.name = Dialog.CLOSE;
            button.pos(this.DIALOG_WIDTH - this.CLOSE_BTN_WIDTH - this.CLOSE_BTN_PADDING, this.CLOSE_BTN_PADDING);
            dialog.addChild(button);

            dialog.dragArea = "0,0," + this.DIALOG_WIDTH + "," + this.DIALOG_HEIGHT;
            dialog.show();
        }
    
    /******************************************** */


    /**
     * 移除全局事件
     * @param type 
     * @param thisObject 
     * @param listener 
     */
    protected removeDataEvent(type: string, thisObject: any, listener: Function): void {
        if( this.EventGlobal[type] )
        {
            var vo:ComponentEventData = this.EventGlobal[type];
            DispatcherManager.removeEventListener(type, thisObject, listener);
            vo.clear();
            delete this.EventGlobal[type];
        }
    }

    /**
     * 注册组件事件，调用此方法，界面关闭会自动移除
     * @param type 时间类型
     * @param callbackobj 作用域
     * @param callbackHandler 事件回调
     */
    protected AddEventByType( target:any, type: string, thisObject: any, listener: Function,args:any=null):void
    {
        if( target )
        {
            if( target.name == null || target.name == "" )
            {
                throw "childbaseview addListener target.name is null";
            }
            var vo:ComponentEventData = GamePool.GetItem( ComponentEventData.ResDicValue );
            vo.target = target;
            vo.type = type;
            vo.thisObject = thisObject;
            vo.listener = listener;
            if( !this.EventResDic.hasOwnProperty( target.name ) )
            {
                this.EventResDic[target.name] = [];
            }
            var bo:boolean = this.JudgeIsHaveEvent( target,type );
            if( !bo )
            {
                target.on( type,thisObject,listener,args );
                this.EventResDic[target.name].push( vo );
            }
        }
    }

    protected JudgeIsHaveEvent( target:any,type: string ):boolean
    {
        var eventArr:Array<ComponentEventData>;
        eventArr = this.EventResDic[target.name];
        var childvo:ComponentEventData;
        for( var s in eventArr )
        {
            childvo = eventArr[s];
            if( childvo && childvo.type == type )
            {
                return true;
            }
        }
        return false;
    }

    /**
     * 移除组件事件
     * @param type 时间类型
     * @param callbackobj 作用域
     * @param callbackHandler 事件回调 
     */
    protected ClearEventByType( target:any, type: string, thisObject: any, listener: Function ):void
    {
        if( target && this.EventResDic[target.name] )
        {
            var arr:Array<ComponentEventData> = this.EventResDic[target.name];
            var i:number=0;
            var nums:number = arr.length;
            var vo:ComponentEventData;
            for(i;i<nums;i++)
            {
                vo = arr[i];
                if( vo && vo.type==type && vo.thisObject == thisObject && vo.listener == listener )
                {
                    target.off( type,thisObject,listener );
                    vo.clear();
                    arr.splice( i,1 );
                    break;
                }
            }
        }
    }

    /**
     * 移除所有全局事件
     */
    public ClearGlobalEvent():void
    {
        if( this.EventGlobal )
        {
            var s;
            var dict:any = this.EventGlobal;
            for( s in dict )
            {
                var vo:ComponentEventData = dict[s];
                if( vo )
                {
                    this.removeDataEvent( vo.type,vo.thisObject,vo.listener );
                }
            }
        }
        this.EventGlobal={};
    }

    /***************************************************** */
    private placeHScroller(): void {
        var hs:Laya.HScrollBar = new Laya.HScrollBar();
        hs.skin = "res/ui/hscroll.png";
        hs.width = 300;
        hs.pos(50, 170);

        hs.min = 0;
        hs.max = 100;

        hs.changeHandler = new Laya.Handler(this, this.onChange);
        Laya.stage.addChild(hs);
    }

    private placeVScroller(): void {
        var vs:Laya.VScrollBar = new Laya.VScrollBar();
        vs.skin = "res/ui/vscroll.png";
        vs.height = 300;
        vs.pos(400, 50);

        vs.min = 0;
        vs.max = 100;
        vs.changeHandler = new Laya.Handler(this, this.onChange);
        Laya.stage.addChild(vs);
    }

    private onChange(value: Number): void {
        console.log("滚动条的位置： value=" + value);
    }
    /***************************************************** */

    
}