import { v3 } from "cc";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { Checkpoint } from "../Checkpoint";
import { CheckpointModelComp } from "../model/CheckpointModel";
import { CheckpointViewComp } from "../view/CheckpointViewComp";
import { Station } from "../../station/Station";

/**
 * 添加两格
 */
@ecs.register('AddCellOperation')
export class AddCellOperationComp extends ecs.Comp {

    reset() {
    }
}


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

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

    entityEnter(e: Checkpoint): void {
        this.createStation(e, 2)
        e.remove(AddCellOperationComp)
    }

    createStation(e: Checkpoint, count: number) {
        const checkpoint_root = e.get(CheckpointViewComp).node
        const checkpoint_model = e.get(CheckpointModelComp)
        const start_point = v3(-1.2, 0, 1.2)
        for (let index = 0; index < count; index++) {
            const station = ecs.getEntity<Station>(Station);
            // 添加关卡到场景
            const h_index = checkpoint_model.stations.length >= 5 ? checkpoint_model.stations.length - 5 : (checkpoint_model.stations.length) % 5
            // const v_index =  Math.floor((checkpoint_model.stations.length + index) / 5)
            const v_index = checkpoint_model.stations.length >= 5 ? 1 : 0
            checkpoint_model.stations.push(station)
            station.load(checkpoint_root, v3(start_point.x + h_index * 0.5, 0, start_point.z + v_index * 0.5));
            e.addChild(station)
        }
    }

}