import { ui } from "../ui/layaMaxUI";
import { SC, ViewManager, ViewType } from "./ViewManager";
import GuangGao from "../Net/GuangGao";
import { AccountManager } from "../Net/AccountManager";
import { ServerManager, BlastToMerge } from "../Net/ServerManager";
import { WXSDK } from "../Net/WXSDK";
import { GameTools,faafssfafsa } from "../Tool/GameTools";

export default class LotteryView extends ui.LotteryViewUI implements SC{
    private static ins:LotteryView;
    public circleNum:number = 5;
    constructor(){
        super();
    }
    public static get Instance():LotteryView{
        if(LotteryView.ins){
            return LotteryView.ins
        }
        else{
            return new LotteryView();
        }
    }
    Show(){
        this.CloseBtn.clickHandler = Laya.Handler.create(this,this.ClickClose);
        this.LotteryBtn.clickHandler = Laya.Handler.create(this,this.OnVedio);
        this.height = Laya.stage.height; 
        Laya.stage.on(Laya.Event.RESIZE,this,()=>
        {
            this.height = Laya.stage.height;
        })
    }
    Close(){
        
    }
    ClickClose(){
        ViewManager.Instance.ShowView(ViewType.MainView);
    }
    //观看视频抽奖
    OnVedio(){
        GuangGao.KaiShiPin((flag) => {
            if (flag) {
                this.LotteryBtn.visible = false;
                let param = [{ "channel": "video" }, { "t": AccountManager.Instance.ServerTime.toString() }];
                ServerManager.Instance.SendHttp(BlastToMerge.Lottery, param, Laya.Handler.create(this, function (e) {
                    this.RotationDial(e);
                }), Laya.Handler.create(this, () => {
                    WXSDK.ShowTxt("抽奖次数已经用完!");
                }), true);
            }
        })
    }
    //旋转转盘
    RotationDial(e){
        var result = e.lottery_id - 1;
        this.dial.rotation = 0;
        let tween = Laya.Tween.to(this.dial, {
            rotation: 360 * (this.circleNum) - result * 60
        }, 5000, Laya.Ease.quadInOut, new Laya.Handler(null, () => {
            tween.clear();
            this.LotteryBtn.visible = true;
            if (!LotteryView.IsCanLottery()) {
                this.LotteryBtn.gray = true;
                console.log("观看视频抽奖按钮已变灰");
            }
            var reward = e["rewards"];
            GameTools.TweenText(new faafssfafsa("道具 + 1", new Laya.Vector2(Laya.stage.width / 2, Laya.stage.height * 0.35), new Laya.Vector2(250, 80), 30), 150, Laya.Handler.create(this, () => {
                ViewManager.Instance.ShowView(ViewType.MainView);
            }))
            
        }))
    }
    
    public static IsCanLottery(): boolean {
        var _timer = Number(AccountManager.Instance.curplayerData.lottery.day_video_times);
        return _timer > 0;
    }

} 