
export class BannerLoop{
    /**
     * banner创建函数
     */
    private __bannerCreateHandler:Function;

    /**
     * banner销毁函数
     */
    private __bannerDestroyHandler:Function;


    /**
     * bannerID列表
     */
    private __bannerIDs:string[];

    /**
     * banner实例
     */
    private __bannerMap:any;

    /**
     * 当前显示的Banner
     */
    private __currentBanner:string;

    /**
     * 最大显示次数
     */
    private __maxShowCount:number;

    constructor(){

    }

    init(maxShowCount:number,bannerIDs:string[],bannerCreateHandler:Function,bannerDestroyHanler:Function):void{
        this.__maxShowCount=maxShowCount;
        this.__bannerIDs=bannerIDs;
        this.__bannerCreateHandler=bannerCreateHandler;
        this.__bannerDestroyHandler=bannerDestroyHanler;
        this.__bannerMap={};

        if(this.__bannerIDs.length==0){
            return;
        }
        this.__bannerIDs.forEach(id => {
            let banner:any=this.__bannerCreateHandler(id);
            let bannerProxy:BannerProxy=new BannerProxy(id,banner);
            this.__bannerMap[id]=bannerProxy;
        });
    }


    showBanner():void{
        if(this.__currentBanner!=undefined){
            this.hideBanner();
        }
        if(this.__currentBanner==undefined){
            this.__currentBanner=this.__bannerIDs[0];
        }else{
            let index:number=this.__bannerIDs.indexOf(this.__currentBanner);
            if(index<this.__bannerIDs.length-1){
                this.__currentBanner=this.__bannerIDs[index+1];
            }else{
                this.__currentBanner=this.__bannerIDs[0];
            }
        }
        let proxy:BannerProxy=this.__bannerMap[this.__currentBanner];
        if(!proxy){
            let banner:any=this.__bannerCreateHandler(this.__currentBanner);
            proxy=new BannerProxy(this.__currentBanner,banner);
            this.__bannerMap[this.__currentBanner]=proxy;
        }
        proxy.show();
    }

    hideBanner():void{
        let proxy:BannerProxy=this.__bannerMap[this.__currentBanner];
        if(!proxy){
            return;
        }
        proxy.hide();
        if(proxy.showIndex>this.__maxShowCount){
            delete this.__bannerMap[proxy.id];
            this.__bannerDestroyHandler(proxy.banner);
            proxy.destroy();
        }
    }
}
class BannerProxy
{
    banner:any;
    id:string;
    showIndex:number;
    maxCount:number;

    private __isShowing:boolean;
    private __loaded:boolean;
    constructor(id:string,banner:any){
        this.id=id;
        this.banner=banner;
        this.showIndex=0;

        //加载回调
        this.banner.onLoad(()=>{
            console.log("banner加载完成：");
            this.__loaded=true;
            if(this.__isShowing){
                this.banner.show();
            }
        })
    }

    show():void{
        this.__isShowing=true;
        this.showIndex++;
        if(this.__loaded){
            this.banner.show();
        }
    }

    hide():void{
        this.__isShowing=false;
        if(this.__loaded){
            this.banner.hide();
        }
    }

    destroy():void{
        this.banner=null;
        this.id=null;
        this.showIndex=0;
    }
}
