import { ui } from "../ui/layaMaxUI";
import { PlayerManager } from "../Manager/PlayerManager";
import { GameTools, faafssfafsa } from "../GameUtils/GameTools";
import { AccountManager } from "../NetWork/AccountManager";
import { signdata } from "../NetWork/PlayerData";
import GuangGao from "../NetWork/GuangGao";
import { ViewManager, ViewType } from "./ViewManager";
import { ServerManager, TeamWave } from "../NetWork/ServerManager";
import Dictionary from "../GameUtils/Dictionary";
import { WXSDK } from "../NetWork/WXSDK";
const REWARDNAME = "reward";
const MAINPATH = "Main/";
const SKINPATH = "subpackage2/res/skin/";
const CIRCLES = 5;
const ROTATIME: number = 5000;

export class LotteryView extends ui.LotteryViewUI{
    private static ins:LotteryView;
    constructor(){
        super();
        LotteryView.ins = this;
    }
    public static get Instance():LotteryView{
        if(LotteryView.ins == null){
            return new LotteryView();
        }
        return LotteryView.ins;
    }
    private _itemDic: Dictionary;
    OnShow(){
        this.RegButton();
        this.RefreshUI();
        this.height = Laya.stage.height;
        Laya.stage.on(Laya.Event.RESIZE,this,()=>
        {
            this.height = Laya.stage.height;
        })
    }
    RefreshUI(){
        let lottery = AccountManager.Instance.curplayerData.GainCollocation("lottery");
        let idx = 1;
        this._itemDic = new Dictionary();
        lottery && lottery.forEach(item => {
            this._itemDic.set(idx, item);
            let type = Math.floor(Number(item.reward_item) * 0.001);
            let reward = this.dial.getChildByName(`${REWARDNAME}${idx++}`);
            if (!reward) return;
            let skin = reward.getChildByName("skin") as Laya.Image;
            let name = reward.getChildByName("name") as Laya.Text;
            if (type == 1 ) {
                // skin.scaleX = skin.scaleY = 0.7;
                skin.skin = MAINPATH + item.icon + ".png";
                name.text = "X" + item.reward_num.toString();
            }
            else if (type == 6) {
                // skin.scaleX = skin.scaleY = 0.25;
                skin.skin = SKINPATH + item.icon + ".png";
                name.text = item.name.toString();
                name.visible = false;
            }
        }, this);
    }

    private _rotateDial(data: any) {
        let targRota = 360 * CIRCLES - 45 * (data.lottery_id - 1);
        let tween = Laya.Tween.to(this.dial, { rotation: targRota }, ROTATIME, Laya.Ease.quadInOut, Laya.Handler.create(this, () => {
            let item = this._itemDic.get(data.lottery_id);
            let type = Math.floor(Number(item.reward_item) * 0.001);



            var reward = data["rewards"];
            if (reward["1000"]) {
                var gg = Number(reward["1000"]);
                GameTools.GetMoney(gg, null, Laya.Handler.create(this, () => {
                    ViewManager.Instance.ShowView(ViewType.LotteryView);
                }));
            }
            else {
                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.LotteryView);
                }))
            }
        }));
    }
    RegButton(){
        this.VideoGetBtn.clickHandler = new Laya.Handler(this,this.ClickVideoGet);
        this.CloseBtn.clickHandler = new Laya.Handler(this,this.ClickClose);
    }
    ClickClose(){
        ViewManager.Instance.ShowView(ViewType.HomeView);
    }
    ClickVideoGet(){
        // GuangGao.ShowVideo((isLoad)=>{
        //     if(isLoad){
                this.VideoGetBtn.visible = false;
                let param = [{ "channel": "video" }, { "t": AccountManager.Instance.ServerTime.toString() }];
                ServerManager.Instance.SendHttp(TeamWave.Lottery, param, Laya.Handler.create(this, function (e) {
                    this._rotateDial(e);
                }), Laya.Handler.create(this, () => {
                    WXSDK.ShowTxt("抽奖次数已经用完!");
                }), true);
        //     }
        // })
    }
    OnHide(){
        
    }
}