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

export default class NotWinningPanel extends Panel {
    get skinKey() { return "NotWinningPanel" }
    get groupName() { return "not_winning_panel" }
    public score: eui.Label;
    public labelMyRank: eui.Label;
    public labelMyNickname: eui.Label;
    public labelMyScore: eui.Label;
    public dataGroup: eui.DataGroup;
    public btnViewAllRank: eui.Button;
    public btnRestart: eui.Button;
    public labelHighestScore: eui.Label;
    public btnClose: eui.Label;
    data: {
        score: number,
        highestScore: number
    }

    onSkinComplete() {
        super.onSkinComplete();
        this.score.text = this.data.score.toString();
        this.labelHighestScore.text = "历史最高分：" + this.data.highestScore.toString();
        this.btnRestart.onTap(this, () => {
            this.hidePanel();
            // SceneCtrl.instance.change(StartScene);
            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.btnViewAllRank.onTap(this, () => {
            PanelCtrl.instance.show(RankListPanel);
        });
        this.btnClose.onTap(this, () => {
            this.hidePanel();
            SceneCtrl.instance.change(StartScene);
        });

        NetManager.ins.getRecentRankListNew((success: any) => {  // 查询排行榜
            if (!success) return;
            let rankData = DataManager.ins.getData(NetName.RANK_NEW_LIST).data.currentRankList;

            //我的数据
            let myData = rankData["currentUserRankInfo"];
            this.labelMyRank.text = myData["rank"];
            this.labelMyNickname.text = /* MUtils.stringOverEllipsis(myData["nickName"], 16); */"我自己";
            this.labelMyScore.text = myData["maxScore"];

            //取前三
            let three = [];
            for (let i = 0; i < 3; i++) {
                if (rankData["userRankList"][i])
                    three.push(rankData["userRankList"][i]);
            }
            this.dataGroup.itemRenderer = RankListItem;
            this.dataGroup.dataProvider = new eui.ArrayCollection(three);
        }, window["rankId"], DataManager.ins.customCfgData.actId, true, 0, 20);

    }

}