import { _decorator, Director, director, EventKeyboard, find, Label, NodeEventType, } from 'cc';
import { ModulerBase } from '../../GameFrameWork/ModulerBase';
import { resMgr } from '../../../Frames/ResourcesMgr';
import { GameInfo } from '../../../GameInfo';
import { messageMgr } from '../../../Frames/MessageMgr';
import { localDt } from '../../../Frames/LocalDt';
const { ccclass, property } = _decorator;

@ccclass('GameOver')
export class GameOver extends ModulerBase {
    //击杀奖励
    KillRewardConfig = [
        { min: 0, max: 5, reward: 100 },
        { min: 6, max: 10, reward: 200 },
        { min: 11, max: 15, reward: 300 },
        { min: 16, max: 20, reward: 400 },
        { min: 21, max: 25, reward: 500 },
        { min: 26, max: 30, reward: 600 },
        { min: 31, max: Infinity, reward: 700 },
    ]
    //生命奖励
    LifeRewardConfig = [
        { min: 0, max: 20, reward: 100 },
        { min: 21, max: 40, reward: 200 },
        { min: 41, max: 60, reward: 300 },
        { min: 61, max: 80, reward: 400 },
        { min: 81, max: 100, reward: 500 },
    ]
    //通关奖励
    newresultRewardConfig = [
        { min: 1, max: 5, reward: 100 },
        { min: 6, max: 10, reward: 150 },
        { min: 11, max: 15, reward: 200 },
        { min: 16, max: 20, reward: 250 },
        { min: 21, max: 25, reward: 300 },
        { min: 26, max: Infinity, reward: 350 },
    ]

    private xp: number = null;
    private totalGold: number = null;
    private totalDia: number = null;

    protected onEnable(): void {
        this.config();
    }
    protected onStart(): void {
        this.onBtnClick("_btnLeft", this._onBtnBack, this);
        this.onBtnClick("_btnRight", this._onBtnRight, this);
        director.preloadScene("StartScene")
    }
    config() {
        this.getSprite("_gameOverUITitle").spriteFrame =
            GameInfo.Instance.getOverWin() ?
                resMgr.getSpriteFrame("GameWin") :
                resMgr.getSpriteFrame("GameOver");

        this.getNode("_btnRight").getComponentInChildren(Label).string =
            GameInfo.Instance.getOverWin() ? "下一关" : "重新开始";

        this.getLabel("_lifePercent").string = `${GameInfo.Instance.getLifePecent()}%`;
        this.getLabel("_killCount").string = `${GameInfo.Instance.getKillCount()}`;
        
        const kill: number = this.calculateReward(GameInfo.Instance.getKillCount(), this.KillRewardConfig);
        const life: number = this.calculateReward(GameInfo.Instance.getLifePecent(), this.LifeRewardConfig);
        const stageClear: number = this.calculateReward(GameInfo.Instance.getCurlv(), this.newresultRewardConfig);
        
        this.xp = GameInfo.Instance.getCurlv() * 30;
        this.totalGold = kill + life + stageClear;
        
        const lifeDia: number = Math.floor(life * 0.08);
        const stageClearDia: number = Math.floor(stageClear * 0.07);
        
        this.totalDia = lifeDia + stageClearDia;
        
        this.getLabel("_kill").string = `${kill}`;
        this.getLabel("_life").string = `${life}`;
        this.getLabel("_stageClear").string = `${stageClear}`;
        this.getLabel("_total").string = `${this.totalGold}`;
        this.getLabel("_xp").string = `${this.xp}`;
        this.getLabel("_lifeDia").string = `${lifeDia}`;
        this.getLabel("_stageClearDia").string = `${stageClearDia}`;
        this.getLabel("_totalDia").string = `${this.totalDia}`;
    }

    private _onBtnBack() {
        this.addReward();
        this.clearDt();
        this.hide(false);
        director.loadScene("StartScene");
    }

    private _onBtnRight() {
        if(GameInfo.Instance.getOverWin()){
            this.addReward();
            console.log("下一关")
        } else {
            console.log("重新开始")
        }
        this.clearDt();
    }

    //计算奖励
    private calculateReward(lifeReward: number, rewardConfig: any) {
        const matchRule = rewardConfig.find(rule =>
            lifeReward >= rule.min && lifeReward <= rule.max
        )
        return matchRule ? matchRule.reward : 0;
    }

    //清除数据
    private clearDt() {
        GameInfo.Instance.setKillCount(0);
        GameInfo.Instance.setLifePecent(0);
        GameInfo.Instance.setOverWin(false)
    }

    private addReward(){
        const map: Map<string, number> = new Map();
        map.set("GameOverRewardGold",this.totalGold + GameInfo.Instance.getGold());
        map.set("GameOverRewardDia", this.totalDia + GameInfo.Instance.getDiamond());
        map.set("Xp", this.xp + GameInfo.Instance.getCurGradeExp());
        
        if(GameInfo.Instance.getOverWin()){
            GameInfo.Instance.setCurLv(GameInfo.Instance.getCurlv() + 1);
            map.set("CurLv", GameInfo.Instance.getCurlv());
        }
        GameInfo.Instance.setGameOverReward(map);
        GameInfo.Instance.setGold(this.totalGold + GameInfo.Instance.getGold());
        GameInfo.Instance.setDiamond(this.totalDia + GameInfo.Instance.getDiamond());
        localDt.saveGold(GameInfo.Instance.getGold());
        localDt.saveDiamond(GameInfo.Instance.getDiamond());
    }
}


