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
        e.CheckpointModel.nextVehicle = null
        this.initCar(e)
    }



    initCar(e: Checkpoint) {
        // const vehiclesCopy = e.CheckpointModel.vehicles
        let vColor = e.CheckpointModel.puppets.filter(val => val.PathFind?.canReach).map(v => v.PuppetModel?.color)
        // console.log(vColor)
        if (vColor.length < 2) {
            vColor = e.CheckpointModel.puppets.slice(0, 8).map(v => v.PuppetModel?.color)
        }
        // console.log(vColor)
        // 统计颜色频率并按频率降序排序
        const colorFrequency = vColor.reduce((acc, color) => {
            acc[color] = (acc[color] || 0) + 1;
            return acc;
        }, {});
        // console.log('e.CheckpointModel.nextVehicle.VehicleModel.color:',e.CheckpointModel.curVehicle?.VehicleModel?.color)
        if(e.CheckpointModel.nextVehicle?.VehicleModel?.color&&colorFrequency[e.CheckpointModel.nextVehicle.VehicleModel.color]){
            colorFrequency[e.CheckpointModel.nextVehicle.VehicleModel.color] -=3
            // console.log('当前车辆色减去3个再计算多少')
        }
        // console.log(colorFrequency)
        const sortedColors = Object.keys(colorFrequency).sort((a, b) => colorFrequency[b] - colorFrequency[a]);
        const vehicles = e.CheckpointModel.vehicles

        const getColorVehicle = () => {
            const sortColor = sortedColors.shift()
            const colorIndex = vehicles.findIndex(v=>v.VehicleModel.color===sortColor)
            if(colorIndex>-1){
                return vehicles.splice(colorIndex,1)[0]
            }else{
                return vehicles.shift()
            }
        }
        if (!e.CheckpointModel.nextVehicle) {
            const vehicle = getColorVehicle()
            if (vehicle) {
                e.CheckpointModel.curVehicle = vehicle
                vehicle.VehicleView.animator.onStationComplete = () => {
                    // vehicle.remove(MoveToComp)
                    oops.audio.playEffect("common/audio/bus_horn");
                    e.add(StationOperationComp)
                }
                vehicle.VehicleView?.animator.moveToStation()
            }
        } else {
            const vehicle = e.CheckpointModel.nextVehicle
            e.CheckpointModel.curVehicle = vehicle
            vehicle.VehicleView.animator.onStationComplete = () => {
                // vehicle.remove(MoveToComp)
                oops.audio.playEffect("common/audio/bus_horn");
                e.add(StationOperationComp)
            }
            e.CheckpointModel.nextVehicle = null
            vehicle.VehicleView?.animator.moveToStation()
        }

        if (!e.CheckpointModel.nextVehicle) {
            const nextVehicle = getColorVehicle()
            if (nextVehicle) {
                e.CheckpointModel.nextVehicle = nextVehicle
                nextVehicle.VehicleView?.animator.moveToWait()
            }
        }
    }

    update(e: Checkpoint): void {
        const vehicle = e.CheckpointModel.curVehicle

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

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

        }

    }
}