import { AABB3 } from "../../../../core/geom/3d/aabb/AABB3.js";
import { make_justified_point_grid } from "../../../../core/geom/3d/util/make_justified_point_grid.js";
import Vector3 from "../../../../core/geom/Vector3.js";
import { TextureAssetLoader } from "../../../asset/loaders/texture/TextureAssetLoader.js";
import Entity from "../../../ecs/Entity.js";
import { Transform } from "../../../ecs/transform/Transform.js";
import { EngineHarness } from "../../../EngineHarness.js";
import { BehaviorComponent } from "../../../intelligence/behavior/ecs/BehaviorComponent.js";
import { BehaviorSystem } from "../../../intelligence/behavior/ecs/BehaviorSystem.js";
import { OrbitingBehavior } from "../../../intelligence/behavior/ecs/OrbitingBehavior.js";
import Trail2D from "./Trail2D.js";
import Trail2DSystem from "./Trail2DSystem.js";

const harness = new EngineHarness();

/**
 *
 * @param {Engine} engine
 * @return {Promise<void>}
 */
async function main(engine) {
    await EngineHarness.buildBasics({
        engine,
    });

    const ecd = engine.entityManager.dataset;

    const bounds = new AABB3(2, 1, 2, 18, 1, 18);

    const grid_points = make_justified_point_grid(bounds, 0.2, (x, y, z) => {

        const entity = new Entity();

        entity.add(Trail2D.fromJSON({
            textureURL: "data/textures/trail/rainbow.png",
            width: 0.01
        }));
        entity.add(Transform.fromJSON({
            position: { x, y, z }
        }));
        entity.add(BehaviorComponent.from(OrbitingBehavior.from({
            center: new Vector3(x,y,z),
            radius: 0.1-0.02
        })))

        entity.build(ecd)

    });

    console.log("Grid Points:",grid_points);

}

harness.initialize({
    configuration(config, engine) {

        config.addLoader("texture", new TextureAssetLoader());

        config.addManySystems(
            new Trail2DSystem(engine),
            new BehaviorSystem(engine)
        );

    }
}).then(main)