import {PanelBaseControl} from "../Control/PanelBaseControl";
import {ViewAction} from "../utils/ViewAction";
import { ViewManager } from "../View/ViewManager";
import {pingtaiutils} from "../WXSDK/pingtaiutils";
import LotteryCFG from "../CFG/LotteryCFG";
import { AssetPath } from "../Config/AssetPath";
import RoleCFG from "../CFG/RoleCFG";
import { GameUtils } from "../utils/GameUtils";
import {AgencyLottery} from "../Agency/AgencyLottery";
import {GuangGao} from "../WXSDK/GuangGao";
import { Global } from "../Global";
import { ttad } from "../WXSDK/ttad";

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

    backbtn:fairygui.GButton;
    lotterybtn:fairygui.GButton;
    icon_0:fairygui.GLoader;
    txt_0:fairygui.GTextField;
    icon_1:fairygui.GLoader;
    txt_1:fairygui.GTextField;
    icon_2:fairygui.GLoader;
    txt_2:fairygui.GTextField;
    icon_3:fairygui.GLoader;
    txt_3:fairygui.GTextField;
    icon_4:fairygui.GLoader;
    txt_4:fairygui.GTextField;
    icon_5:fairygui.GLoader;
    txt_5:fairygui.GTextField;
    icon_6:fairygui.GLoader;
    txt_6:fairygui.GTextField;
    icon_7:fairygui.GLoader;
    txt_7:fairygui.GTextField;

    private agencylottery:AgencyLottery;
    
    constructor() 
    { 
        super( "main","LotteryView",false,ViewAction.ActionType_none,1 ); 
    }

    /************************************* */
    private logger:Laya.Text;
    private showLogger(): void {
        this.logger = new Laya.Text();

        this.logger.fontSize = 30;
        this.logger.color = "#FFFFFF";
        this.logger.align = 'center';
        this.logger.valign = 'middle';

        this.logger.size(Laya.stage.width, Laya.stage.height);
        this.logger.text = "等待响应...\n";
        Laya.stage.addChild(this.logger);
    }
    /************************************* */


    private onHttpRequestError(e: any): void {
        console.log(e);
    }

    protected initEvent():void
    {
        this.AddEventByType( this.backbtn,Laya.Event.CLICK,this,this.onclosefun );
        this.AddEventByType( this.lotterybtn,Laya.Event.CLICK,this,this.choujiangclick );
    }


    private _tweeObject:any;
    private _nowgetIndex:number;
    private _preIndex:number;
    private tw: laya.utils.Tween;
    

    private playComplte():void
    {
        if( this.tw )
        {
            this.tw.clear();
            this.tw = null;
        }
        this.backbtn.enabled = true;
        this.lotterybtn.enabled = true;
        this.agencylottery.getreward( this._selectcfg );
    }

    private onclosefun():void
    {
        if( this.tw )
        {
            this.tw.clear();
            this.tw = null;
        }
        GuangGao.Clear();
        ViewManager.Instance.close( LotteryPanel );
    }


    protected initView():void
    {
        this.agencylottery = this.getProxy( AgencyLottery );
        var vec:Array<LotteryCFG> = LotteryCFG.vec;
        for( var i:number = 0;i < vec.length;i++ )
        {
            var cfg:LotteryCFG = vec[i];
            if( cfg.type == 1 )
            {
                this["icon_"+i].url = fairygui.UIPackage.getItemURL( "main","img_gold" );
                this["txt_"+i].text = "X" + cfg.value;
            }
            else
            {
                var rolecfg:RoleCFG = RoleCFG.dict[cfg.value];
                this["icon_"+i].url = AssetPath.getroleicon( rolecfg.icon );
                this["txt_"+i].text = rolecfg.rolename;
            }
        }
        GuangGao.CheatBanner(this.lotterybtn);
    }

    /**************************************** */
    private onSocketClose(): void {
        console.log("Socket closed");
    }
    private onMessageReveived(message: any): void {
        console.log("Message from server:");
        if (typeof message == "string") {
            console.log(message);
        }
        else if (message instanceof ArrayBuffer) {
            
        }
    }
    /**************************************** */



    private choujiangclick():void
    {
        this.videocallback();
        // pingtaiutils.Instance.playVideo( "",this,this.videocallback );
    }

    private _selectcfg:LotteryCFG;
    private videocallback():void
    {
        ttad.ShowVideo((isLoad=>{
            if(isLoad){
                this.startPlay();
            }
        }))
    }

    private startPlay():void
    {
        var index = this.agencylottery.GetReward();
        this._selectcfg = LotteryCFG.vec[ index ];
        this.backbtn.enabled = false;
        this.lotterybtn.enabled = false;
        this._nowgetIndex = index;
        var angle:number = 360 * 6 + (this._nowgetIndex)* 45 - 15;
        if( this.item_panel.rotation == 0 )
        {
            this._tweeObject = { value:0 };
        }
        else
        {
            this._tweeObject = { value:this._preIndex * 45 - 15 };
        }
        this._preIndex = this._nowgetIndex;
        this.tw = laya.utils.Tween.to(this._tweeObject, { value:angle }, 3000, laya.utils.Ease.quadInOut,Laya.Handler.create( this,this.playComplte ));
        this.tw.update = Laya.Handler.create(this, function (): void {
                var value:number = this._tweeObject.value % 360;
                this.item_panel.rotation =  value;
            }, null, false);
    }
    
}