import { PanelBaseControl } from "../Control/PanelBaseControl";
import { GameUtils } from "../utils/GameUtils";
import { TimerService } from "../Time/TimerService";
import { GuangGao } from "../WXSDK/GuangGao";
import { ViewAction } from "../utils/ViewAction";
import { ViewManager } from "./ViewManager";
import { AgencyInGame } from "../Agency/AgencyInGame";
import { AgencyLottery } from "../Agency/AgencyLottery";
import LotteryCFG from "../CFG/LotteryCFG";
import { SceneControl } from "../Control/SceneControl";
import { GameStateControl } from "../Control/GameStateControl";
import { GlobalTip } from "../utils/GlobalTip";
import { WxApi } from "../WXSDK/Sdk";
import { ttad } from "../WXSDK/ttad";

export default class EggPanel extends PanelBaseControl implements TimerControl
{
    static key:string = "EggPanel";

    load_click:fairygui.GLoader;
    pro_0:fairygui.GProgressBar;
    btn_1:fairygui.GLoader;
    btn_close:fairygui.GButton;
    hitani:fairygui.Transition;
    getrewardbtn:fairygui.GButton;

    private _isComplete = false;
    private _isShowBanner = false;
    private _bannerVal = 0.5;
    private _maxClickCnt = 18;
    private _clickCnt = 0;
    private _interval = 100;
    private _decrement = 1;

    n6:fairygui.GGroup;
    Chest:fairygui.GGroup;
    
    constructor() 
    { 
        super( "main","eggview",false,ViewAction.ActionType_none,1 ); 
    }

    protected initView():void
    {
        this.hitani = this.getTransitionByName( "hitani" );
        this.pro_0.value = 1;
        this._bannerVal = GameUtils.randRangefloat(0.2, 0.4);
        TimerService.Instance.addUpdateTarget( this );
        // GuangGao.CheatBanner(this.btn_1);
        // GuangGao.Clear();//h["default"].Clear()r["default"]
        ttad.Clear();
        var sceneheight = Laya.stage.height;
        this.btn_1.y=sceneheight-this.btn_1.height-15;
        this.getrewardbtn.y = this.btn_1.y;
        this.getrewardbtn.visible = false;

        if(SceneControl.EggGGroup==0)
        {
            this.n6.visible=true;
        }

    }

    private bannercallback():void
    {

    }

    protected initEvent():void
    {
        this.AddEventByType( this.btn_1,Laya.Event.CLICK,this,this.qiaoDa );
        this.AddEventByType( this.btn_close,Laya.Event.CLICK,this,EggPanel.onclosefun );
        this.AddEventByType( this.getrewardbtn,Laya.Event.CLICK,this,EggPanel.VideoGetReward );
    }

    private _time:number = 0;
    public update( dt:number ):void
    {
        this._time += dt;
        if( this._time >= this._interval )
        {
            this._time = 0;
            this.Daojishi();
        }
    }

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

    private qiaoDa() 
    {
        if (!this._isComplete) 
        {
            if(this.hitani!=null&& !this.hitani.playing )
            {
                this.hitani.play( null,1 );
            }
            this._clickCnt++;
            if (this._clickCnt > this._maxClickCnt) 
                this._clickCnt = this._maxClickCnt;
            let pv = this._clickCnt / this._maxClickCnt;
            if( pv > 1 )
            {
                pv = 1;
            }
            this.pro_0.value = pv*100;
            if (pv >= this._bannerVal && !this._isShowBanner) {
                this._isShowBanner = true;
                this.getrewardbtn.visible = true;
                // GuangGao.OpenBanner();
                Laya.timer.once( 2000,this,()=>{
                    this.getrewardbtn.visible = false;
                } );
            }
            if (pv >= 1) {
                this._isComplete = true;
                EggPanel.getreward();
            }
        }
    }

    static getreward():void
    {
        // var zpproxy:AgencyLottery = this.getProxy( AgencyLottery ) as AgencyLottery;
        // var index = ( this.getProxy( AgencyLottery ) as AgencyLottery ).GetReward();
        // var selectcfg:LotteryCFG = LotteryCFG.vec[ index ];
        // zpproxy.getreward( selectcfg );
        // TimerService.Instance.removeUpdateTarget( this );
        GlobalTip.Instance.showWordTips( "获得开局加速" );
        EggPanel.onclosefun();
    }

    //看视频得奖励
  static VideoGetReward(){
        ttad.ShowVideo((isLoad=>{
            if(isLoad){
                GlobalTip.Instance.showWordTips( "获得开局加速" );
                EggPanel.onclosefun();
            }
            else{
                EggPanel.onclosefun();
            }
        }))
    }
    private hidebanner():void
    {
        // PlatformTool.getInstance().hideBanner("EggView");
        GuangGao.hidebanner();
    }

    static onclosefun():void
    {
        ViewManager.Instance.close( EggPanel );
    }

    public destory():void
    {
        // PlatformTool.getInstance().hideBanner("EggView");
        // GuangGao.hidebanner();
        TimerService.Instance.removeUpdateTarget( this );
        super.destory();
        GameStateControl.Instance.entergame(true);
    }


    
}