import { make_justified_point_grid } from "../../../../core/geom/3d/util/make_justified_point_grid.js";
import Vector2 from "../../../../core/geom/Vector2.js";
import Vector3 from "../../../../core/geom/Vector3.js";
import { GameAssetType } from "../../../asset/GameAssetType.js";
import { GLTFAssetLoader } from "../../../asset/loaders/GLTFAssetLoader.js";
import { TextureAssetLoader } from "../../../asset/loaders/texture/TextureAssetLoader.js";
import Entity from "../../../ecs/Entity.js";
import { TransformAttachmentSystem } from "../../../ecs/transform-attachment/TransformAttachmentSystem.js";
import { Transform } from "../../../ecs/transform/Transform.js";
import { EngineHarness } from "../../../EngineHarness.js";
import { BehaviorSystem } from "../../../intelligence/behavior/ecs/BehaviorSystem.js";
import { SGMesh } from "../../ecs/mesh-v2/aggregate/SGMesh.js";
import { SGMeshSystem } from "../../ecs/mesh-v2/aggregate/SGMeshSystem.js";
import { ShadedGeometry } from "../../ecs/mesh-v2/ShadedGeometry.js";
import { ShadedGeometrySystem } from "../../ecs/mesh-v2/ShadedGeometrySystem.js";


/**
 *
 * @param {AABB3} bounds
 * @param {number} [spacing]
 */
function buildProbeGrid(bounds, spacing = 1) {
    const positions = [];

    make_justified_point_grid(bounds, spacing, (x, y, z) => {
        positions.push(x, y, z);
    })

}

/**
 *
 * @param {Engine} engine
 */
async function buildGI(engine) {

    const ecd = engine.entityManager.dataset;

    ecd.traverseEntities([ShadedGeometry, Transform], (sg, transform, entity) => {

    });

    new Entity()
        .add(SGMesh.fromURL("moicon/gnutti_not_optimized/model.gltf"))
        .add(Transform.fromJSON({
            position: { x: 10, y: 3, z: 10 },
            scale: 1.5
        }));

}


function init() {

    /**
     *
     * @param {Engine} engine
     */
    async function main(engine) {

        await EngineHarness.buildBasics({
            engine,
            focus: new Vector3(128, 0, 128),
            distance: 30,
            terrainResolution: 1,
            terrainSize: new Vector2(128, 128),
            enableWater: false,
        });

    }

    new EngineHarness().initialize({
        configuration(config, engine) {
            config.addLoader(GameAssetType.Texture, new TextureAssetLoader());

            const gltfAssetLoader = new GLTFAssetLoader();

            config.addLoader(GameAssetType.ModelGLTF_JSON, gltfAssetLoader);
            config.addLoader(GameAssetType.ModelGLTF, gltfAssetLoader);

            config.addSystem(new BehaviorSystem(engine))
            config.addSystem(new SGMeshSystem(engine));
            config.addSystem(new ShadedGeometrySystem(engine));
            config.addSystem(new TransformAttachmentSystem(engine));
        }
    })
        .then(main);
}


init();