import { EngineHarness } from "../../EngineHarness.js";
import { GameAssetType } from "../../asset/GameAssetType.js";
import { QuestDescriptionDatabase } from "../../../../../model/game/ecs/component/quest/QuestDescriptionDatabase.js";
import { MeshSystem } from "../../graphics/ecs/mesh/MeshSystem.js";
import { makeEngineOptionsModel } from "../../../../../model/game/options/makeEngineOptionsModel.js";
import Vector3 from "../../../core/geom/Vector3.js";
import Entity from "../Entity.js";
import { Transform } from "../transform/Transform.js";
import Mesh from "../../graphics/ecs/mesh/Mesh.js";
import { TooltipComponent } from "./TooltipComponent.js";
import { TooltipComponentSystem } from "./TooltipComponentSystem.js";

const engineHarness = new EngineHarness();

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

    engine.assetManager.insert('quests', GameAssetType.JSON, {
        create: function () {
            return [
                {
                    "id": "test",
                    "completionCode": "",
                    "completionCondition": "a > 7 && variable.name == 200 && flag.value == true",
                    "variableMapping": {
                        "a": "a_key",
                        "variable.name": "v0_name",
                        "flag.value": "flag0"
                    }
                },
                {
                    "id": "test-2",
                    "completionCode": "",
                    "completionCondition": "a > 50",
                    "variableMapping": {
                        "a": "a_key",
                        "variable.name": "v0_name",
                        "flag.value": "flag0"
                    }
                }
            ];
        }
    })

    engine.staticKnowledge.add('quests', 'quests', new QuestDescriptionDatabase());

    const em = engine.entityManager;

    em.addSystem(new MeshSystem(engine.graphics, engine.assetManager));
    em.addSystem(new TooltipComponentSystem({
        graphics: engine.graphics,
        tooltips: engine.gui.tooltips,
        pointer: engine.devices.pointer,
        localization: engine.localization
    }));

    await makeEngineOptionsModel(engine.options, engine);


    await harness.initialize();

    main(engine);
}


/**
 *
 * @param {Engine} engine
 */
function main(engine) {
    engine.graphics.renderer.setClearColor("#666666");

    EngineHarness.buildLights({ engine: engine });

    const camera = EngineHarness.buildCamera({
        engine,
        target: new Vector3(10, 1, 10),
        pitch: -0.7
    });

    EngineHarness.buildOrbitalCameraController({ cameraEntity: camera.id, engine });

    const ecd = engine.entityManager.dataset;

    new Entity()
        .add(Transform.fromJSON({
            position: new Vector3(12.17416, 0.09969, 11.06082)
        }))
        .add(Mesh.fromJSON({
            url: "data/models/Fantasy Props/coins-3.gltf",
            // url: "data/models/characters5/maleWarrior/wariorjin/gltf/warriorjin_003.glb",
            castShadow: true
        }))
        .add(TooltipComponent.fromJSON({
            key: 'a'
        }))
        .build(ecd);

    new Entity()
        .add(Transform.fromJSON({
            position: new Vector3(10.5, 0.09969, 9.06082)
        }))
        .add(Mesh.fromJSON({
            url: "data/models/Fantasy Props/bottle-yellow-1.gltf",
            // url: "data/models/characters5/maleWarrior/wariorjin/gltf/warriorjin_003.glb",
            castShadow: true
        }))
        .add(TooltipComponent.fromJSON({
            key: 'b'
        }))
        .build(ecd);
}

init(engineHarness);
