import { _decorator, director, Label } from 'cc';
import { UIBase } from '../../GameFrameWork/UIBase';
import { resMgr } from '../../../Frames/ResourcesMgr';
import { GameOverData } from './Data/GameOverData';
const { ccclass, property } = _decorator;

@ccclass('GameOver')
export class GameOver extends UIBase {
    // //是否胜利
    // win: boolean = null;

    // killCount: number = null;
    // killPercent: number = null;

    // //金币奖励
    // killAward: number = null;
    // lifeAward: number = null;
    // stageClearAward: number = null;
    // totalAward: number = null;

    // //经验奖励
    // xp: number = null;
    // //钻石奖励
    // lifeDia: number = null;
    // stageClearDia: number = null;
    // totalDia: number = null;

    gameOverDt: GameOverData = null;
    protected onStart() {
        this.gameOverDt = GameOverData.Instance;
        this.onBtnClick("_btnLeft", this._onBtnBack);
        this._calculatedReward();
        this._config();
    }

    // config() {
    //     this.getSprite("_gameOverUITitle").spriteFrame = this.win ?
    //         resMgr.getSpriteFrame("GameWin") :
    //         resMgr.getSpriteFrame("GameOver");
    //     this.getLabel("_labelRight").string = this.win ? "下一关" : "重新开始";

    //     const labelDate = {
    //         "_killCount":this.killCount,
    //         "_lifePercent":`${this.killPercent} %`,
    //         "_kill":this.killAward,
    //         "_life":this.lifeAward,
    //         "_stageClear":this.stageClearAward,
    //         "_total":this.totalAward,
    //         "_xp":this.xp,
    //         "_lifeDia":this.lifeDia,
    //         "_stageClearDia":this.stageClearDia,
    //         "_totalDia":this.totalDia,
    //     }
    //     this._setLabels(labelDate);
    //     // this.getLabel("_killCount").string = String(this.killCount);
    //     // this.getLabel("_lifePercent").string = `${String(this.killPercent)} %`;
    //     // this.getLabel("_kill").string = String(this.killAward);
    //     // this.getLabel("_life").string = String(this.lifeAward);
    //     // this.getLabel("_stageClear").string = String(this.stageClearAward);
    //     // this.getLabel("_total").string = String(this.totalAward);
    //     // this.getLabel("_xp").string = String(this.xp);
    //     // this.getLabel("_lifeDia").string = String(this.lifeDia);
    //     // this.getLabel("_stageClearDia").string = String(this.stageClearDia);
    //     // this.getLabel("_totalDia").string = String(this.totalDia);
    // }

    // private _setLabels(labelDate){
    //     for(const [labelName, value] of labelDate){
    //         this.getLabel(labelName).string = String(value);
    //     }
    // }

    private _calculatedReward() {
        if (this.gameOverDt.KillCount >= 20) {
            this.gameOverDt.KillAward = 100;
            this.gameOverDt.Xp = 30;
        } else if (this.gameOverDt.KillCount < 20) {
            this.gameOverDt.KillAward = 50;
            this.gameOverDt.Xp = 15;
        }

        if (this.gameOverDt.LifePercent === 100) {
            this.gameOverDt.LifeAward = 40;
            this.gameOverDt.LifeDia = 10;
        } else if (this.gameOverDt.LifePercent >= 50 && this.gameOverDt.LifePercent < 100) {
            this.gameOverDt.LifeAward = 30;
            this.gameOverDt.LifeDia = 6;
        } else if (this.gameOverDt.LifePercent >= 0 && this.gameOverDt.LifePercent < 50) {
            this.gameOverDt.LifeAward = 20;
            this.gameOverDt.LifeDia = 2;
        }
        
        // if(this.gameOverDt.Win){
        //     this.gameOverDt.StageClearDia = 10;
        // } else {
        //     this.gameOverDt.StageClearDia = 2;
        // }

        this.gameOverDt.StageClearDia = this.gameOverDt.Win ? 10 : 2;

        this.gameOverDt.TotalAward = this.gameOverDt.KillAward + this.gameOverDt.LifeAward;
        this.gameOverDt.TotalDia = this.gameOverDt.LifeDia + this.gameOverDt.StageClearDia;
    }

    private _config() {

        this.getSprite("_gameOverUITitle").spriteFrame = this.gameOverDt.Win ?
            resMgr.getSpriteFrame("GameWin") : resMgr.getSpriteFrame("GameOver");
        this.getNode("_btnRight").getChildByName("Label").getComponent(Label).string =
         this.gameOverDt.Win ? "下一关" : "重新开始";

        const labelData = new Map<string, number | string>();
        labelData.set("_killCount", this.gameOverDt.KillCount);
        labelData.set("_lifePercent", `${this.gameOverDt.LifePercent} %`);
        labelData.set("_kill", this.gameOverDt.KillAward);
        labelData.set("_life", this.gameOverDt.LifeAward);
        //通关奖励：StageClearAward
        labelData.set("_stageClear", this.gameOverDt.StageClearAward);
        labelData.set("_total", this.gameOverDt.TotalAward);
        labelData.set("_xp", this.gameOverDt.Xp);
        labelData.set("_lifeDia", this.gameOverDt.LifeDia);
        labelData.set("_stageClearDia", this.gameOverDt.StageClearDia);
        labelData.set("_totalDia", this.gameOverDt.TotalDia);

        this._setLabels(labelData);
    }

    private _setLabels(labelData: Map<string, number | string>) {
        for (const [labelName, value] of labelData.entries()) {
            this.getLabel(labelName).string = String(value);
        }
    }

    private _onBtnBack() {
        //销毁
        this.gameOverDt.clearData();
        this.hide(false);
        director.loadScene("StartScene");
    }
}


