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";

/**
 * 站台人员进车
 */
@ecs.register('StationOperation')
export class StationOperationComp extends ecs.Comp {

    reset() {
    }
}


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

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

    entityEnter(e: Checkpoint): void {
        e.CheckpointModel.stations.forEach(val => {
            if (val.StationModel.puppet && val.StationModel.puppet.PuppetModel.color === e.CheckpointModel.curVehicle.VehicleModel.color) {
                if (!e.CheckpointModel.curVehicle.VehicleModel.isFull) {
                    e.CheckpointModel.curVehicle.VehicleModel.useSit += 1
                    val.StationModel.puppet.PuppetView.animator.onRunComplete = () => {
                        e.CheckpointModel.curVehicle.VehicleView.createPuppet()
                        e.removeChild(val.StationModel.puppet)
                        val.StationModel.puppet.destroy()
                        val.StationModel.puppet = null
                        e.CheckpointModel.peopleCount-=1
                    }
                    val.StationModel.puppet.PuppetView.animator.moveToTarget()
                }

            }
        })
        e.remove(StationOperationComp)
    }

}