import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { SingletonModuleComp, smc } from "../../common/SingletonModuleComp";
import { PuppetAnimatorType } from "../../puppet/model/PuppetEnum";
import { PuppetModelComp } from "../../puppet/model/PuppetModelComp";
import { Puppet } from "../../puppet/puppet";
import { PuppetViewComp } from "../../puppet/view/PuppetViewComp";
import { Subway } from "../../subway/Subway";
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
                const { x, y } = element.PuppetModel
                cell[x][y] = null
                e.CheckpointModel.path_grid[x][y].fill = null
                e.CheckpointModel.curVehicle.VehicleModel.useSit += 1
                element.PuppetView.animator.moveToTarget(e.CheckpointModel.curVehicle.VehicleView.node.position.clone())
            }
            if (count >= needCount) {
                break
            }
        }
        if (count < needCount) {
            const subways = e.CheckpointModel.subways
            for (let index = 0; index < subways.length; index++) {
                const subway = subways[index];
                const peopleIndex = subway.SubwayModel.colorArr.findIndex(val => val === color)
                if (peopleIndex > -1) {
                    count += 1
                    subway.SubwayModel.colorArr.splice(peopleIndex, 1)
                    subway.SubwayView.updateCount(subway.SubwayModel.colorArr.length)
                    const tempPuppet = this.creatPuppet(color)
                    const account = smc.initialize.account
                    tempPuppet.load(account.checkpoint.CheckpointView.node, color, subway.SubwayView.node.position.clone(), account.AccountModel.skin);
                    e.addChild(tempPuppet)

                    tempPuppet.PuppetView.animator.onRunComplete = () => {
                        e.CheckpointModel.curVehicle.VehicleView.createPuppet()
                        e.removeChild(tempPuppet)
                        tempPuppet.destroy()
                        e.CheckpointModel.peopleCount -= 1
                    }
                    e.CheckpointModel.curVehicle.VehicleModel.useSit += 1
                    tempPuppet.PuppetView.animator.moveToTarget(e.CheckpointModel.curVehicle.VehicleView.node.position.clone())

                }
                if (count >= needCount) {
                    break
                }
            }
        }
        e.remove(FullVehicleOperationComp)
    }

    creatPuppet(color: string) {
        const puppet = ecs.getEntity<Puppet>(Puppet)
        const puppetModel = puppet.get(PuppetModelComp)
        puppetModel.color = color
        puppetModel.withColider = true
        return puppet
    }

}