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 { AgencyUserMessage } from "../Agency/AgencyUserMessage";
import { PlayerAccount } from "../WXSDK/PlayerAccount";
import { GameState } from "../utils/GameState";
import { PropManager } from "../PrefabControl/PropManager";
import { CompletePanel } from "./CompletePanel";
import { AgencyTrial } from "../Agency/AgencyTrial";
import { RoleData } from "../Data/RoleData";
import { ttad } from "../WXSDK/ttad";

export default class GiftView extends PanelBaseControl 
{
    static key:string = "GiftView";

    private clickani:fairygui.Transition;
    private jindu:fairygui.GProgressBar;
    private btnbox:fairygui.GGroup;
    private anniu:fairygui.GLoader;
    private shou:fairygui.GLoader;
    private kuaisudianji:fairygui.GLoader;
    private di:fairygui.GLoader;

    private getrewardbtn:fairygui.GButton;

    private item:fairygui.GComponent;


    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","giftcheatview" ); 
    }

    protected initView():void
    {
        this.anniu = this.item.asCom.getChildAt(2) as fairygui.GLoader;
        this.kuaisudianji = this.item.asCom.getChildAt(0) as fairygui.GLoader;
        this.shou = this.item.asCom.getChildAt(3) as fairygui.GLoader;
        this.di = this.item.asCom.getChildAt(1) as fairygui.GLoader;
        this.getrewardbtn = this.item.asCom.getChildAt(4) as fairygui.GButton;
        this.getrewardbtn.visible = false;

        this.clickani = this.item.getTransition( "clickbtnani" );
        this.jindu.value = 1;
        this._bannerVal = GameUtils.randRangefloat(0.2, 0.4);
        TimerService.Instance.addUpdateTarget( this );
        // GuangGao.Clear();
        ttad.Clear();
        Laya.timer.loop(2000,this,this.playAnimator);
        // var sceneheight = Laya.stage.height;
        // this.btnbox.y=sceneheight-this.btnbox.y-15;
    }

    playAnimator(){
        this.clickani.play(null,1);
    }

    protected initEvent():void
    {
        this.AddEventByType( this.anniu,Laya.Event.CLICK,this,this.qiaoDa );
        this.AddEventByType( this.getrewardbtn,Laya.Event.CLICK,this,this.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.jindu.value;
        pv -= this._decrement;
        if (pv <= 0) pv = 1;
        this.jindu.value = pv;
        this._clickCnt = Math.ceil(pv *this._maxClickCnt /100);
    }

    private qiaoDa() 
    {
        if(this.shou.visible){
            this.shou.visible = false;
            this.kuaisudianji.visible = false;
            Laya.timer.clear(this,this.playAnimator);
        }
        
        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*100;
            if (pv >= this._bannerVal && !this._isShowBanner) {
                this._isShowBanner = true;
                this.getrewardbtn.visible = true;
                Laya.timer.once( 2000,this,()=>{
                    this.getrewardbtn.visible = false
                } );
            }
            if (pv >= 1) {
                this._isComplete = true;
                GiftView.getreward();
            }
        }
    }

    static getreward():void
    {
        
        RoleData.Instance.Money += PlayerAccount.CurrentAccount.CurAccountData.CheatReward;
        // ( this.getProxy( AgencyUserMessage ) as AgencyUserMessage ).GoldUp( PlayerAccount.CurrentAccount.CurAccountData.CheatReward );
        // var index = ( this.getProxy( AgencyLottery ) as AgencyLottery ).GetReward();
        // var selectcfg:LotteryCFG = LotteryCFG.vec[ index ];
        // zpproxy.getreward( selectcfg );
        // TimerService.Instance.removeUpdateTarget( this );
        GlobalTip.Instance.showWordTips( "获得"+PlayerAccount.CurrentAccount.CurAccountData.CheatReward+"金币" );
        GiftView.onclosefun();
    }

    //看视频得奖励
    VideoGetReward(){
        ttad.ShowVideo((isLoad=>{
            if(isLoad){
                RoleData.Instance.Money += PlayerAccount.CurrentAccount.CurAccountData.CheatReward;
                GlobalTip.Instance.showWordTips( "获得"+PlayerAccount.CurrentAccount.CurAccountData.CheatReward+"金币" );
                GiftView.onclosefun();
            }
            else{
                GiftView.onclosefun();
            }
        }))
    }

    private hidebanner():void
    {
        GuangGao.hidebanner();
    }

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

    public destory():void
    {
        GuangGao.hidebanner();
        TimerService.Instance.removeUpdateTarget( this );
        super.destory();
    }


    
}