import { _decorator, director, EAxisDirection, Label, log, PhysicsSystem, } from 'cc';
import { ModulerBase } from '../../GameFrameWork/ModulerBase';
import { resMgr } from '../../../Frames/ResourcesMgr';
import { GameInfo } from '../../../GameInfo';
import { localDt } from '../../../Frames/LocalDt';
import { GameMgr } from '../../GameFrameWork/GameMgr';
import { EnemyMgr } from './EnemyMgr';
import { MyTower } from './MyTower';
import { Bottom } from './Bottom';
import { EnemyTower } from './EnemyTower';
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() {
        GameInfo.Instance.setIsGameOver(true);
        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(Number(GameInfo.Instance.getCurlv()), this.newresultRewardConfig);

        this.xp = Number(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() {
        //director.purgeDirector();
        this.addReward();
        this.clearDt();
        this.hide(false);
        director.loadScene("StartScene", () => {
            director.resume();
            GameInfo.Instance.setIsGameOver(false);
        });
    }

    private _onBtnRight() {
        director.resume();
        GameInfo.Instance.setIsGameOver(false);
        if (GameInfo.Instance.getOverWin()) {
            //初始化场景
            this._initGameScene();
        } else {
            //初始化场景
            this._initGameScene();
        }
    }

    //计算奖励
    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 + Number(localDt.getData("Gold"))));
        map.set("GameOverRewardDia", this.totalDia + GameInfo.Instance.getDiamond());
        map.set("Xp", this.xp + GameInfo.Instance.getCurGradeExp());

        if (GameInfo.Instance.getOverWin()) {
            GameInfo.Instance.setCurLv(Number(GameInfo.Instance.getCurlv()) + 1);
            map.set("CurLv", Number(GameInfo.Instance.getCurlv()));
        }
        GameInfo.Instance.setGameOverReward(map);
        //GameInfo.Instance.setGold(this.totalGold + GameInfo.Instance.getGold());
        localDt.saveData("Gold", this.totalGold + Number(localDt.getData("Gold")));

        GameInfo.Instance.setDiamond(this.totalDia + GameInfo.Instance.getDiamond());
        localDt.saveData("Diamond", GameInfo.Instance.getDiamond())
    }

    //初始化场景 重置数据
    private _initGameScene() {
        this.addReward();
        this.clearDt();
        GameMgr.Instance.getModuler(EnemyMgr).node.removeAllChildren();
        this.node.parent.getChildByPath("Road/Roles").removeAllChildren();
        this.node.parent.getChildByPath("BulletLayer").removeAllChildren();

        GameMgr.Instance.getModuler(EnemyMgr).init();
        this.node.parent.getComponentInChildren(EnemyTower).initData();
        this.node.parent.getComponentInChildren(MyTower).initData();
        GameMgr.Instance.getModuler(Bottom).initData();

        //隐藏
        this.hide(false);
    }
}


