import { v3 } from "cc";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { MoveToComp } from "../../common/ecs/position/MoveTo";
import { Checkpoint } from "../Checkpoint";
import { CheckpointModelComp } from "../model/CheckpointModel";
import { ECSEntity } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECSEntity";
import { StationOperationComp } from "./StationOperation";
import { VehicleOperationComp } from "./VehicleOperation";
import { CheckpointUpgradeComp } from "./CheckpointUpgrade";
import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { UIID } from "../../common/config/GameUIConfig";

/**
 * 通关
 */
@ecs.register('CheckpointCheckPass')
export class CheckpointCheckPassComp extends ecs.Comp {

    reset() {
    }
}

/**
 * 失败
 */
@ecs.register('CheckpointCheckFail')
export class CheckpointCheckFailComp extends ecs.Comp {

    reset() {
    }
}


export class CheckpointCheckSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {

    filter(): ecs.IMatcher {
        return ecs.allOf(CheckpointModelComp).anyOf(CheckpointCheckPassComp, CheckpointCheckFailComp);
    }


    entityEnter(e: Checkpoint): void {
        // e.CheckpointModel.curVehicle
        const passComp = e.get(CheckpointCheckPassComp)
        const failComp = e.get(CheckpointCheckFailComp)
        if(passComp){
            oops.audio.playEffect('common/audio/level_complete')
            e.upgrade()
            oops.gui.open(UIID.LevelPass)
            this.exit(e)
            return
        }
        if(failComp){
            oops.gui.open(UIID.LevelFail)
            this.exit(e)
            return
        }
    }

    exit(e:Checkpoint){
        e.remove(CheckpointCheckPassComp)
        e.remove(CheckpointCheckFailComp)
        e.remove(StationOperationComp)
        e.remove(VehicleOperationComp)
    }

}