import { Vec3 } from "cc";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { ECSEntity } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECSEntity";
import { VM } from "../../../../../extensions/oops-plugin-framework/assets/libs/model-view/ViewModel";
import { Grid } from "../../grid/Grid";
import { Obstacle } from "../../obstacle/Obstacle";
import { Puppet } from "../../puppet/puppet";
import { Station } from "../../station/Station";
import { Vehicle } from "../../vehicle/Vehicle";
import { Subway } from "../../subway/Subway";


type Cell = Puppet | Obstacle | Subway

export interface MapInfo {
    x: number,
    y: number,
    fill?: string | number
    pos: Vec3
}


@ecs.register('CheckpointModel')
export class CheckpointModelComp extends ecs.Comp {


    private vm: any = {};

    private _grid: Grid[][] = []
    private _path_grid: MapInfo[][] = []
    private _cells: Cell[][] = []

    private _station: Station[] = []

    private _vehicles: Vehicle[] = []

    private _curVehicle: Vehicle = null

    private _peopleCount:number = 0

    get peopleCount(): number {
        return this._peopleCount
    }

    set peopleCount(_v: number) {
        this.vm.peopleCount = _v
        this._peopleCount = _v
    }

    get curVehicle(): Vehicle {
        return this._curVehicle
    }

    set curVehicle(_v: Vehicle) {
        this._curVehicle = _v
    }


    /** 格子 */
    get grids(): Grid[][] {
        return this._grid;
    }
    set grids(value: Grid[][]) {
        this._grid = value;
    }

    /** 寻路数据 */
    get path_grid(): MapInfo[][] {
        return this._path_grid;
    }
    set path_grid(value: MapInfo[][]) {
        this._path_grid = value;
    }

    /** 站台 */
    get stations(): Station[] {
        return this._station;
    }
    set stations(value: Station[]) {
        this._station = value;
    }

    /** 车辆 */
    get vehicles(): Vehicle[] {
        return this._vehicles;
    }
    set vehicles(value: Vehicle[]) {
        this._vehicles = value;
    }

    /** 棋盘格中物体 */
    get cells(): Cell[][] {
        return this._cells;
    }
    set cells(value: Cell[][]) {
        this._cells = value;
    }

    get emptyStation(): Station {
        return this._station.find(val => val.StationModel.isEmpty && !val.StationModel.isLock)
    }

    get emptyStationCount(): number {
        return this._station.filter(val => val.StationModel.isEmpty && !val.StationModel.isLock).length
    }

    get puppets(): Puppet[] {
        const flatArr = this.flattenArray(this.cells)
        return flatArr.filter(val => val instanceof Puppet && val.PuppetModel)
    }
    get subways(): Subway[] {
        const flatArr = this.flattenArray(this.cells)
        return flatArr.filter(val => val instanceof Subway && val.SubwayModel)
    }

    flattenArray(arr) {
        const result = [];
        arr.forEach((item) => {
            if (Array.isArray(item)) {
                result.push(...this.flattenArray(item)); // 如果是数组，则递归调用
            } else {
                result.push(item); // 否则直接加入到结果数组中
            }
        });
        return result;
    }





    vmAdd() {
        VM.add(this.vm, "Checkpoint");
    }

    vmRemove() {
        VM.remove("Checkpoint");
    }

    reset() {
        this.grids = []
        this.path_grid = []
        this.cells = []
        this.stations = []
        this.vehicles = []
        this.curVehicle = null
        this.peopleCount = 0
        this.vmRemove();
    }
}

