import { makeMirEngineConfig } from "../../../../../../../model/game/makeMirEngineConfig.js";
import { makeEngineOptionsModel } from "../../../../../../../model/game/options/makeEngineOptionsModel.js";
import Vector3 from "../../../../../core/geom/Vector3.js";
import {
    v3_computeCatmullRomSplineUniformDistance
} from "../../../../../core/math/spline/v3_computeCatmullRomSplineUniformDistance.js";
import { Animation } from "../../../../ecs/animation/Animation.js";
import { EntityBlueprint } from "../../../../ecs/EntityBlueprint.js";
import RenderSystem from "../../../../ecs/systems/RenderSystem.js";
import { Transform } from "../../../../ecs/transform/Transform.js";
import { EngineHarness } from "../../../../EngineHarness.js";
import Path from "../../../../navigation/ecs/components/Path.js";
import Mesh from "../../mesh/Mesh.js";
import { EntityPath } from "./EntityPath.js";
import { EntityPathMarkerDefinition } from "./EntityPathMarkerDefinition.js";
import { EntityPathStyle } from "./EntityPathStyle.js";

const engineHarness = new EngineHarness();

/**
 *
 * @param {EngineHarness} harness
 * @return {Promise<void>}
 */
async function init(harness) {
    const engine = harness.engine;

    engine.entityManager.addSystem(new RenderSystem(engine.graphics));

    makeEngineOptionsModel(engine.options, engine);

    await makeMirEngineConfig(engine).apply(engine);

    await harness.initialize();

    main(engine);
}

function main(engine) {
    EngineHarness.buildBasics({ engine });

    const path = new EntityPath();
    const _p = new Path();

    const curve_points = v3_computeCatmullRomSplineUniformDistance([
        new Vector3(3, 0, 3),
        new Vector3(7, 0, 3),
        new Vector3(7, 0, 7),
        new Vector3(3, 0, 7)
    ], 50);

    _p.setPositionsFromVectorArray(curve_points);

    const style = new EntityPathStyle();
    style.spacing = 1;
    style.marker_start = EntityPathMarkerDefinition.from({
        blueprint: EntityBlueprint.from([
            Transform.fromJSON({}),
            Mesh.fromJSON({
                url: 'data/models/arrow-symbols/arrow4/model-green.gltf',
                castShadow: true,
                receiveShadow: true
            })
        ]),
        transform: Transform.fromJSON({
            scale: {
                x: 0.3,
                y: 0.3,
                z: 0.3
            },
            rotation: {
                x: 0.5,
                y: -0.5,
                z: 0.5,
                w: 0.5
            },
            position: {
                x: 0,
                y: 1,
                z: 0
            }
        })
    });
    style.marker_end = EntityPathMarkerDefinition.from({
        blueprint: EntityBlueprint.from([
            Transform.fromJSON({}),
            Mesh.fromJSON({
                url: 'data/models/arrow-symbols/arrow4/model-red.gltf',
                castShadow: true,
                receiveShadow: true
            })
        ]),
        transform: Transform.fromJSON({
            scale: {
                x: 0.3,
                y: 0.3,
                z: 0.3
            },
            rotation: {
                x: 0.5,
                y: -0.5,
                z: 0.5,
                w: 0.5
            },
            position: {
                x: 0,
                y: 1,
                z: 0
            }
        })
    });
    style.marker_main = EntityPathMarkerDefinition.from({
        blueprint: EntityBlueprint.from([
            Transform.fromJSON({}),
            Mesh.fromJSON({
                url: 'data/models/Barel-Roll Droid/handpainted.gltf',
                castShadow: true,
                receiveShadow: true
            }),
            Animation.fromJSON({
                clips: [
                    {
                        name: "fly",
                        repeatCount: Infinity
                    }
                ]
            })
        ]),
        transform: Transform.fromJSON({
            scale: {
                x: 0.2,
                y: 0.2,
                z: 0.2
            },
            position: {
                x: 0,
                y: 1,
                z: 0
            }
        })
    });

    path.setPath(_p);
    path.setStyle(style);


    path.build();

    path.markers.forEach(m => m.entity.build(engine.entityManager.dataset));

    console.log(path);
}

init(engineHarness);
