import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { SingletonModuleComp } from "../../common/SingletonModuleComp";
import { Checkpoint } from "../Checkpoint";
import { CheckpointModelLevelComp } from "../model/CheckpointModelLevel";
import { InitCheckpointComp } from "./InitCheckpoint";

/**
 * 关卡升级
 */
@ecs.register('CheckpointUpgrade')
export class CheckpointUpgradeComp extends ecs.Comp {
    /** 当前等级 */
    lv: number = 0;

    reset() {
        this.lv = 0;
    }
}

export class CheckpointUpgradeSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(CheckpointUpgradeComp, CheckpointModelLevelComp);
    }

    entityEnter(e: Checkpoint): void {
        let rlm = e.CheckpointModelLevel;
        let ru = e.CheckpointUpgrade;

        if (ru.lv == 0)
            rlm.vm.lv++;                   // 提升一级
        else
            rlm.vm.lv = ru.lv;             // 设置等级

        const rnd = Math.floor(Math.random() * 3)
        // 当前等级配置
        rlm.rtluCurrent.init(rlm.vm.lv);
        if (rlm.rtluCurrent.vehicleType == null) {
            if (rlm.vm.vehicleType == null) { // 没有下一关信息用随机
                rlm.rtluCurrent.vehicleType = rnd
            } else { // 用下一关信息
                rlm.rtluCurrent.vehicleType = rlm.vm.vehicleType
            }
        }
        ecs.getSingleton(SingletonModuleComp).account.AccountModel.lv = rlm.vm.lv
        // 下个关卡配置
        rlm.rtluNext.init(rlm.vm.lv + 1);

        rlm.vm.stationCount = rlm.rtluNext.stationCount;
        rlm.vm.gridRowCount = rlm.rtluNext.gridRowCount;
        rlm.vm.gridColCount = rlm.rtluNext.gridColCount;
        rlm.vm.peopleCount = rlm.rtluNext.peopleCount;
        if (rlm.rtluNext.vehicleType == null) {
            rlm.rtluNext.vehicleType  = rnd
            rlm.vm.vehicleType = rnd
        } else {
            rlm.vm.vehicleType = rlm.rtluNext.vehicleType
        }


        e.remove(CheckpointUpgradeComp);
        e.add(InitCheckpointComp)
    }
}