import { initializeEditor } from "../../../../../model/game/initializeEditor.js";
import { enableEditor } from "../../../../editor/enableEditor.js";
import { GameAssetType } from "../../asset/GameAssetType.js";
import { GLTFAssetLoader } from "../../asset/loaders/GLTFAssetLoader.js";
import Entity from "../../ecs/Entity.js";
import { Transform } from "../../ecs/transform/Transform.js";
import { EngineHarness } from "../../EngineHarness.js";
import { SGMesh } from "../../graphics/ecs/mesh-v2/aggregate/SGMesh.js";
import { SGMeshAnimationController } from "../../graphics/ecs/mesh-v2/aggregate/SGMeshAnimationController.js";
import {
    SGMeshAnimationControllerSystem
} from "../../graphics/ecs/mesh-v2/aggregate/SGMeshAnimationControllerSystem.js";
import { SGMeshSystem } from "../../graphics/ecs/mesh-v2/aggregate/SGMeshSystem.js";
import { ShadedGeometrySystem } from "../../graphics/ecs/mesh-v2/ShadedGeometrySystem.js";

const harness = new EngineHarness();

/**
 *
 * @param {Engine} engine
 * @returns {Promise<void>}
 */
async function main(engine) {

    enableEditor(engine, initializeEditor);

    await EngineHarness.buildBasics({
        engine,
        enableTerrain: false,
        cameraFarDistance: 100,
        focus: {
            "x": 0,
            "y": 4,
            "z": 10.244190535355157
        },
        cameraAutoClip: false,
        pitch: 0.030000000000008027,
        yaw: 3.123185307179593,
    });
    //
    make_sample('data/models/samples/InterpolationTest.glb',[
        'Step Scale',
        'Linear Scale',
        'CubicSpline Scale',
        'Step Rotation',
        'CubicSpline Rotation',
        'Linear Rotation',
        'Step Translation',
        'CubicSpline Translation',
        'Linear Translation'
    ],engine.entityManager.dataset);

    // make_sample('data/models/samples/BoxAnimated.glb',[
    //    'animation_0'
    // ],engine.entityManager.dataset);

    // make_sample('data/models/samples/animatedbox1.gltf', [
    //     'All Animations'
    // ], engine.entityManager.dataset);

    // make_sample('moicon/Separated_Cardboard-Box-800x600x400_Roughness0/V1 Cardboard B.gltf', [
    //     'All Animations'
    // ], engine.entityManager.dataset);
    // make_sample('moicon/v26/v26.gltf', [
    //     'All Animations'
    // ], engine.entityManager.dataset);
}

/**
 *
 * @param {string} path
 * @param {string[]} animations
 * @param {EntityComponentDataset} ecd
 */
function make_sample(path, animations, ecd) {

    const controller = new SGMeshAnimationController();

    animations.forEach(name => {

        controller.start(name, true);

    })

    new Entity()
        .add(SGMesh.fromURL(path))
        .add(controller)
        .add(new Transform())
        .build(ecd);
}

harness.initialize({
    configuration(config, engine) {
        const gltfAssetLoader = new GLTFAssetLoader();
        config.addLoader(GameAssetType.ModelGLTF, gltfAssetLoader);
        config.addLoader(GameAssetType.ModelGLTF_JSON, gltfAssetLoader);
        config.addSystem(new SGMeshAnimationControllerSystem(engine));
        config.addSystem(new SGMeshSystem(engine));
        config.addSystem(new ShadedGeometrySystem(engine));
    }
}).then(main);