import { Vec3, Node } from "cc";
import { ViewUtil } from "../../../../extensions/oops-plugin-framework/assets/core/utils/ViewUtil";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";

import { CheckpointUpgradeComp, CheckpointUpgradeSystem } from "./bll/CheckpointUpgrade";
import { InitCheckpointSystem } from "./bll/InitCheckpoint";
import { CheckpointModelLevelComp } from "./model/CheckpointModelLevel";
import { CheckpointViewComp } from "./view/CheckpointViewComp";
import { CheckpointModelComp } from "./model/CheckpointModel";
import { VehicleOperationSystem } from "./bll/VehicleOperation";
import { StationOperationSystem } from "./bll/StationOperation";
import { CheckpointCheckSystem } from "./bll/CheckpointCheck";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { UIID } from "../common/config/GameUIConfig";
import { CheckpointModelBaseComp } from "./model/CheckpointModelBase";
import { SkinChangeSystem } from "./bll/SkinChange";
import { FullVehicleOperationSystem } from "./bll/FullVehicleOperation";
import { AddCellOperationSystem } from "./bll/AddCellOperation";
import { ClearCellOperationSystem } from "./bll/ClearCellOperation";
import { CheckpointPathTriggerSystem } from "./bll/CheckpointPathTrigger";

/**
 * 关卡实体
 * 1、生成关卡初始话数据
 */
@ecs.register('Checkpoint')
export class Checkpoint extends ecs.Entity {

    CheckpointModelLevel!: CheckpointModelLevelComp;
    CheckpointModel!: CheckpointModelComp;
    CheckpointModelBase!: CheckpointModelBaseComp;

    // 业务层
    CheckpointUpgrade!: CheckpointUpgradeComp;

    // 视图层
    CheckpointView!:CheckpointViewComp;

    protected init() {
        // 添加关卡数据组件
        this.addComponents<ecs.Comp>(CheckpointModelComp, CheckpointModelLevelComp,CheckpointModelBaseComp);
        oops.audio.volumeMusic = 0
        oops.audio.playerMusicLoop("common/audio/bgm");
    }

    /** 关卡升级（升级只修改数据） */
    upgrade(lv: number = 0) {
        var ru = this.add(CheckpointUpgradeComp);
        ru.lv = lv;
    }


    destroy(): void {
        // 如果该组件对象是由ecs系统外部创建的，则不可回收，需要用户自己手动进行回收。
        this.remove(CheckpointViewComp);
        super.destroy();
    }

    /** 加载关卡显示对象（cc.Component在创建后，添加到ECS框架中，使实体上任何一个ECS组件都可以通过 ECS API 获取到视图层对象 */
    load(parent: Node, pos: Vec3 = Vec3.ZERO, cb: () => void = () => { }) {
        var node =  ViewUtil.createPrefabNode("game/prefab/level");
        var mv = node.getComponent(CheckpointViewComp)!;
        this.add(mv);
        node.parent = parent;
        node.setPosition(pos);
        cb && cb()
    }
}

export class EcsCheckpointSystem extends ecs.System {
    constructor() {
        super();

        this.add(new InitCheckpointSystem());
        this.add(new CheckpointUpgradeSystem());
        this.add(new VehicleOperationSystem());
        this.add(new StationOperationSystem());
        this.add(new CheckpointCheckSystem());
        this.add(new SkinChangeSystem())
        this.add(new FullVehicleOperationSystem())
        this.add(new AddCellOperationSystem())
        this.add(new ClearCellOperationSystem())
        this.add(new CheckpointPathTriggerSystem())
    }
}