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


export default class GiftView extends ui.GiftViewUI implements SC{
    private static ins:GiftView;

    //banner总展示次数
    private shownum:number = 3;
    //目前已展示的次数
    private nowshownum:number = 0;
    private _isComplete = false;
    private _bannerVal = 0.5;
    private _maxClickCnt = 12;
    private _clickCnt = 0;
    private _interval = 500;
    private _decrement = 0.03;

    private banner: any;

    constructor(){
        super();
        GiftView.ins = this;
    }
    public static get Instance():GiftView{
        if(GiftView.ins == null){
            return new GiftView();
        }
        return GiftView.ins;
    }
    RefreshData(){

    }
    RegButton(){

    }
    Resize(){

    }
    RefreshUI(data?){

    }
    OnShow(){
        
        
    
        this.HitBtn.clickHandler = Laya.Handler.create(this, this.QiaoDa, null, false);
        this._bannerVal = GameTool.randRangefloat(0.35, 0.75);
        
        this.click.play(0,true);
        this.jinduBar.value = 0;
        Laya.timer.loop(this._interval, this, this.Daojishi);
        WXSdk.OnShow(this.onBannerHide);
        GuangGao.Instance.showBanner((banner) => {
            console.log("礼盒界面出现banner---");
            console.log("banner加载完毕--",banner);
            this.banner = banner["banner"];
            banner["banner"].hide();
        }, () => {
        });
         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;
         })
    }
    OnClose(){

    }
    QiaoDa() {
        this.click.stop();
        this.shou.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.jinduBar.value = pv;
            if (pv >= this._bannerVal && this.nowshownum<this.shownum) {
                if (this.banner) {
                    console.log("有banner",this.banner);
                    this.banner.show();
                    this.nowshownum ++;
                    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.jinduBar.value;
        // pv -= this._decrement;
        // if (pv < 0) pv = 0;
        // this.jinduBar.value = pv;

        let pv = this.jinduBar.value;
        pv -= this._decrement;
        if (pv <= 0) pv = 0;
        this.jinduBar.value = pv;
        if(this._clickCnt>2){
            this._clickCnt -= 2
        }
        
    }
    // ClickBanner(){
    //     this.nowshownum = 3;
    //     WXSdk.OffShow(this.ClickBanner);
    // }
    onBannerHide() {
        if(GiftView.Instance.banner)GiftView.Instance.banner.hide();
        WXSdk.OffShow(this.onBannerHide);
        ViewManager.Instance.CloseView(ViewType.GiftView);

        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) => {

            });
    }
}