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('FullVehicleOperation')
export class FullVehicleOperationComp extends ecs.Comp {

    reset() {
    }
}


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

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

    entityEnter(e: Checkpoint): void {
        const curVehicleModel =  e.CheckpointModel.curVehicle.VehicleModel
        const needCount = curVehicleModel.capcity - curVehicleModel.useSit
        const color = curVehicleModel.color
        const puppets = e.CheckpointModel.puppets
        let count = 0
        for (let index = 0; index < puppets.length; index++) {
            const element = puppets[index];
            if(element.PuppetModel.color===color){
                count+=1
                element.PuppetView.animator.onRunComplete = () => {
                    e.CheckpointModel.curVehicle.VehicleView.createPuppet()
                    e.removeChild(element)
                    element.destroy()
                    e.CheckpointModel.peopleCount-=1
                }
                const cell = e.CheckpointModel.cells
                cell[element.PuppetModel.x][element.PuppetModel.y] = null
                e.CheckpointModel.curVehicle.VehicleModel.useSit += 1
                element.PuppetView.animator.moveToTarget(e.CheckpointModel.curVehicle.VehicleView.node.position.clone())
            }
            if(count>=needCount){
                break
            }
        }
        e.remove(FullVehicleOperationComp)
    }

}