import { ui } from "../ui/layaMaxUI";
import { WXSdk } from "../Net/WXSdk";
import { GameTool } from "../Util/GameTool";
import { ViewManager, ViewType } from "../Net/ViewManager";
import { GuangGao } from "../Net/GuangGao";
import UserModel from "../Net/UserModel";
import NetManager, { PROTOCOLS } from "../Net/NetManager";
import MainView from "./MainView";

export default class GoldEggView extends ui.GoldEggViewUI implements SC {
   
    private static ins:GoldEggView;

    
    private _isComplete = false;
    private _isShowBanner = false;
    private _bannerVal = 0.5;
    private _maxClickCnt = 12;
    private _clickCnt = 0;
    private _interval = 500;
    private _decrement = 0.03;

    //banner总展示次数
    private shownum:number = 3;
    //目前已展示的次数
    private nowshownum:number = 0;

    constructor() { 
        super();
        GoldEggView.ins = this;
     }
     public static get Instance():GoldEggView{
         if(GoldEggView.ins){
            return GoldEggView.ins;
         }
         else{
            return new GoldEggView();
         }
     }

    private banner: any;
    RefreshData(){

    }
    RegButton(){

    }
    
    Resize(){

    }
    RefreshUI(data?){

    }
     OnShow(data = null){
        this.HitBtn.y = Laya.stage.height - this.HitBtn.height - 50;
        this.kuangdian.y = Laya.stage.height - this.HitBtn.height - 200;
        this.btnscale.play(null,true);
        this.HitBtn.clickHandler = Laya.Handler.create(this, this.QiaoDa, null, false);
        this._bannerVal = GameTool.randRangefloat(0.35, 0.75);
        this.jindu.value = 0;
        Laya.timer.loop(this._interval, this, this.Daojishi);
        WXSdk.OnShow(this.onBannerHide);
        // GuangGao.Instance.showBanner((banner) => {
        //     if (banner) {
        //         console.log("banner加载完毕--",banner);
        //         this.banner = banner;
        //         banner.hide();
        //     }
        // }, () => {
        // });
        if(GuangGao.Instance._bannerAd){
            this.banner = GuangGao.Instance._bannerAd;
            this.banner.hide();
            console.log("金蛋界面banner加载完毕--",this.banner);
        }

         this.height = Laya.stage.height;
         this.BG.height = Laya.stage.height;
         Laya.stage.on(Laya.Event.RESIZE,this,()=>
         {
            this.BG.height = Laya.stage.height;
            this.height = Laya.stage.height;
         })
     }
     ClosePanel(){
        ViewManager.Instance.CloseView(ViewType.AttributeUpView);
     }
     OnClose(){
        console.log("设置界面关闭---");
     }

     QiaoDa() {
        this.ani1.play(null, false);
        this.btnscale.stop();
        this.kuangdian.visible = false;
        
        if (!this._isComplete) {
            this._clickCnt++;
            if (this._clickCnt > this._maxClickCnt) 
                this._clickCnt = this._maxClickCnt;
            let pv = this._clickCnt / this._maxClickCnt;
            if( pv >= 1 )
            {
                pv = 1;
            }
            this.jindu.value = pv;

            if (pv >= this._bannerVal  && this.nowshownum<this.shownum) {
                if (this.banner) {
                    this.nowshownum++;
                    this.banner.show();
                    Laya.timer.once(2000, this, () => {
                        this.banner.hide();
                        this._bannerVal = GameTool.randRangefloat(0.35,0.75);
                    })
                }
            }
            if (pv >= 1) {
                this._isComplete = true;
                this.onBannerHide();
            }
        }
    }

    Daojishi() {
        // let pv = this.jindu.value;
        // pv -= this._decrement;
        // if (pv <= 0) pv = 1;
        // this.jindu.value = pv;
        // this._clickCnt = Math.ceil(pv *this._maxClickCnt /100);

        let pv = this.jindu.value;
        pv -= this._decrement;
        if (pv <= 0) pv = 0;
        this.jindu.value = pv;
        if(this._clickCnt>2){
            this._clickCnt -= 2
        }
    }
  
    onBannerHide() {
        GoldEggView.Instance._isComplete = true;
        ViewManager.Instance.CloseView(ViewType.GoldEggView);
        MainView.Instance.ShowBoxPanel();
        WXSdk.OffShow(this.onBannerHide);

        var param = [{ "t": UserModel.Instance.CurModel.ServerTime.toString() }, { "reward_item": "1000" }, { "reward_num": "200" }];
        NetManager.Instance.HttpPostRequest(PROTOCOLS.protocols.Fissionvd, param,
            (data: any) => {
                ViewManager.Instance.ShowTips("金币:X" + 200);
            },
            (data) => {

            });
            if(GuangGao.Instance._bannerAd){
                GuangGao.Instance._bannerAd.hide();
            }
    }
}