import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { SingletonModuleComp, smc } from "../../common/SingletonModuleComp";
import { Puppet } from "../../puppet/puppet";
import { PuppetViewComp } from "../../puppet/view/PuppetViewComp";
import { Checkpoint } from "../Checkpoint";
import { CheckpointModelComp } from "../model/CheckpointModel";

/**
 * 清空格子
 */
@ecs.register('ClearCellOperation')
export class ClearCellOperationComp extends ecs.Comp {

    reset() {
    }
}


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

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

    entityEnter(e: Checkpoint): void {
        const vehicles = e.CheckpointModel.vehicles
        e.CheckpointModel.stations.forEach(val => {
            const findVehicle = vehicles.find(vehicle=>{
                return !vehicle.VehicleModel.isFull&&val.StationModel.puppet&&vehicle.VehicleModel.color === val.StationModel.puppet.PuppetModel.color
            })
            if (findVehicle) {
                // console.log('找到合适的车')
                findVehicle.VehicleModel.useSit += 1
                val.StationModel.puppet.PuppetView.animator.onRunComplete = () => {
                    findVehicle.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(findVehicle.VehicleView.node.position.clone())
            }
        })
        e.remove(ClearCellOperationComp)
    }

}