import { GUIManager } from "../../../engines/gui/GUIManager";
import GUIMediator from "../../../engines/gui/GUIMediator";
import SceneManager from "../../../engines/scenes/SceneManager";
import { LiangLiangSDK } from "../../../platforms/libs/liangliangSDK";
import { PlatformManager } from "../../../platforms/PlatformManager";
import UI_WX_S_02 from "../../../ui/WXSExport/UI_WX_S_02";
import Main from "../../Main";
import UIKeys from "../../UIKeys";
import ExportList from "./ExportList";




export default class WXS02Mediator extends GUIMediator
{
    private __showTime:number;
    private __bannerTime:number;
    private __state:number=0;

    private listA:ExportList;
    private listB:ExportList;
    constructor(){
        super();
    }

    protected OnShow():void{
        PlatformManager.hideBanner();
        this.__showTime=cc.sys.now();
        this.__state=0;
        this.__bannerTime=0;
        if(!Main.TEST){
            this.view.m_btnBack.visible=false;
            this.view.m_btnContinue.visible=false;
        }
        if(this.listA==null){
            this.listA=new ExportList(this.view.m_listA01,0);
        }
        if(this.listB==null){
            this.listB=new ExportList(this.view.m_listB,1);
        }
        this.listA.Refresh();
        this.listB.Refresh();
    }

    protected OnHide():void{

    }

    protected AddEvents():void{
        this.view.m_btnBack.onClick(this.BackButtonClick,this);
        this.view.m_btnContinue.onClick(this.ContinueButtonClick,this);
    }

    protected RemoveEvents():void{
        this.view.m_btnBack.offClick(this.BackButtonClick,this);
        this.view.m_btnContinue.offClick(this.ContinueButtonClick,this);
    }
    
    private ContinueButtonClick():void{
        if(LiangLiangSDK.CanWuChu()){
            if(this.__state==0){
                this.CallBannar(true);
                this.__bannerTime=cc.sys.now();
                this.__state=1;
            }else if(this.__state==2){
                this.CallCpa();
                //循环了 哈哈
                this.__state=0;
            }
        }else{
            this.BackButtonClick();
        }
    }

    private BackButtonClick():void{
        //游戏结束流程
        if(this.data==true){
            this.GotoHall();
        }else{
            this.CallCpa();
            this.HideSelf();
            PlatformManager.showBanner();
        }
    }

    private CallCpa():void{
        PlatformManager.randomNavigate2Mini();
    }

    private CallBannar(isShow:boolean):void{
        if(isShow){
            PlatformManager.showBanner();
        }else{
            PlatformManager.hideBanner();
        }
    }

    private GotoHall():void{
        SceneManager.single.Swicth("mainPackage","Hall");
        GUIManager.single.Hide(UIKeys.Fighting);
        GUIManager.single.Show(UIKeys.Hall);
        this.HideSelf();
    }
    
    Tick(dt:number):void{
        let curTime:number=cc.sys.now();
        if(this.view.m_btnBack.visible==false){
            if(curTime-this.__showTime>2000){
                this.view.m_btnBack.visible=true;
            }
        }
        if(this.view.m_btnContinue.visible==false){
            if(curTime-this.__showTime>1000){
                this.view.m_btnContinue.visible=true;
            }
        }
        //显示banner中
        if(this.__state==1){
            if(curTime-this.__bannerTime>2000){
                this.CallBannar(false);
                this.__state=2;
            }
        }
        this.listA.Tick(dt);
        this.listB.Tick(dt);
    }

    private get view():UI_WX_S_02{
        return this._view as UI_WX_S_02;
    }
}