import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { ECSEntity } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECSEntity";
import { smc } from "../../common/SingletonModuleComp";
import { UIID } from "../../common/config/GameUIConfig";
import { MoveToComp } from "../../common/ecs/position/MoveTo";
import { VehicleState } from "../../vehicle/model/VehicleModelComp";
import { Checkpoint } from "../Checkpoint";
import { CheckpointModelComp } from "../model/CheckpointModel";
import { CheckpointLevelPassViewComp } from "../view/CheckpointLevelPassViewComp";
import { CheckpointCheckPassComp } from "./CheckpointCheck";
import { StationOperationComp } from "./StationOperation";


/**
 * 车辆调度
 */
@ecs.register('VehicleOperation')
export class VehicleOperationComp extends ecs.Comp {

    isRun: boolean = false
    reset() {
        this.isRun = false
    }
}


export class VehicleOperationSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem, ecs.ISystemUpdate {

    filter(): ecs.IMatcher {
        return ecs.allOf(CheckpointModelComp, VehicleOperationComp);
    }

    entityEnter(e: Checkpoint): void {
        e.CheckpointModel.curVehicle = null
        this.initCar(e)
    }



    initCar(e: Checkpoint) {
        const vehicles = e.CheckpointModel.vehicles
        for (let index = 0; index < vehicles.length; index++) {
            const vehicle = vehicles[index];
            if (index == 0) {
                e.CheckpointModel.curVehicle = vehicle
                vehicle.VehicleView.animator.onStationComplete = () => {
                    // vehicle.remove(MoveToComp)
                    oops.audio.playEffect("common/audio/bus_horn");
                    e.add(StationOperationComp)
                    // const checkpoint = smc.initialize.account.checkpoint
                    // console.log('puppet', !checkpoint.CheckpointModel.puppets.some(val => val.PuppetModel?.color === vehicle.VehicleModel.color))
                    // console.log('stations', !checkpoint.CheckpointModel.stations.some(val => val.StationModel.puppet && val.StationModel.puppet.PuppetModel?.color === vehicle.VehicleModel?.color))
                    // if (!checkpoint.CheckpointModel.puppets.some(val => val.PuppetModel?.color === vehicle.VehicleModel.color)
                    //     && !checkpoint.CheckpointModel.stations.some(val => val.StationModel.puppet && val.StationModel.puppet.PuppetModel?.color === vehicle.VehicleModel?.color)) {
                    //     vehicle.VehicleModel.ready = true
                    // }
                }
                vehicle.VehicleView?.animator.moveToStation()
            }
            if (index == 1) {
                vehicle.VehicleView?.animator.moveToWait()
                break
            }
        }
    }
 
    update(e: Checkpoint): void {
        const vehicles = e.CheckpointModel.vehicles
        const vehicle = e.CheckpointModel.curVehicle

        if (e.CheckpointModel.peopleCount <= 0) {
            // console.log('过关了')
            e.add(CheckpointCheckPassComp)
            return
        }

        if (vehicle && vehicle.VehicleModel?.ready) {
            vehicles.shift()
            vehicle.VehicleView.animator.onLeaveComplete = () => {
                vehicle.destroy()
            }
            vehicle.VehicleView.animator.moveToLeave()
            this.initCar(e)

        }

    }
}