import Panel from "../views/Panel";
import { Decorator } from "../Decorator/Decorator";
import { MUtils } from "../MUtils";
import SceneCtrl from "../ctrls/SceneCtrl";
import MainScene from "../Scene/MainScene";
import LoadScene from "../Scene/LoadScene";
import StartScene from "../Scene/StartScene";
import { startGameRequest } from "../utils";
import Loading from "../loading/Loading";
import { NetManager } from "../../libs/tw/manager/NetManager";
import { DataManager } from "../../libs/tw/manager/DataManager";
import { NetName } from "../../libs/tw/enum/NetName";
import PanelCtrl from "../ctrls/PanelCtrl";
import ToastPanel from "../components/ToastPanel";

export default class WinningPanel extends Panel {
    get skinKey() { return "WinningPanel" }
    get groupName() { return "winning_panel" }
    public light: eui.Image;
    public score: eui.Label;
    public btnRestart: eui.Button;
    public btnReceive: eui.Button;
    public labelHighestScore: eui.Label;
    public imgReward: eui.Image;
    public btnClose: eui.Button;
    data: {
        score: number,
        imgUrl: string,
        highestScore: number,
        rewardUrl: string
    }


    async onSkinComplete() {
        super.onSkinComplete();
        //光效
        let callback = () => {
            egret.Tween.get(this.light).to({ rotation: 360 }, 8000).call(callback);
        }
        callback();

        this.btnClose.onTap(this, () => {
            this.hidePanel();
            SceneCtrl.instance.change(StartScene);
        });
        this.btnReceive.onTap(this, () => {
            this.hidePanel();
            location.href = this.data.rewardUrl;
        });
        this.btnRestart.onTap(this, () => {
            this.hidePanel();
            // SceneCtrl.instance.change(StartScene);
            startGameRequest();
            Loading.instace.show()
            NetManager.ins.doJoin2((success: any) => {
                if (!success) {
                    Loading.instace.hide();
                    SceneCtrl.instance.change(StartScene);
                    return;
                }
                NetManager.ins.getStartStatus((success: any) => {
                    Loading.instace.hide()
                    let res = DataManager.ins.getData(NetName.GAME_START_STATUS_NEW);
                    if (!success) {
                        SceneCtrl.instance.change(StartScene);
                        return;
                    }
                    if (res.code && res.code == "100201007") {
                        PanelCtrl.instance.show(ToastPanel, { msg: `${res["data"]["unitName"]}不足，多赚点${res["data"]["unitName"]}再来吧` });
                        SceneCtrl.instance.change(StartScene);
                        return;
                    }
                    if (res.code && res.code != "0000000000") return;

                    //开始游戏
                    SceneCtrl.instance.change(MainScene);
                }, /* DataManager.ins.getData(NetName.DO_JOIN_DATA2).data */window["netData"][window['CFG'].doJoin]["orderId"], () => {
                    return DataManager.ins.getData(NetName.GAME_START_STATUS_NEW).code != "0000000000";
                }, 10);
            });
        });
        this.score.text = this.data.score.toString();
        this.labelHighestScore.text = `历史最高分：` + this.data.highestScore.toString();
        this.imgReward.source = await MUtils.loadImage(this.data.imgUrl);
    }

}