import { BoxBufferGeometry, MeshStandardMaterial } from "three";
import { GLTFAssetLoader } from "../../../src/engine/asset/loaders/GLTFAssetLoader.js";
import { TextureAssetLoader } from "../../../src/engine/asset/loaders/texture/TextureAssetLoader.js";
import Entity from "../../../src/engine/ecs/Entity.js";
import { TransformAttachmentSystem } from "../../../src/engine/ecs/transform-attachment/TransformAttachmentSystem.js";
import { Transform } from "../../../src/engine/ecs/transform/Transform.js";
import { EngineHarness } from "../../../src/engine/EngineHarness.js";
import { Camera } from "../../../src/engine/graphics/ecs/camera/Camera.js";
import { ShadedGeometry } from "../../../src/engine/graphics/ecs/mesh-v2/ShadedGeometry.js";
import { ShadedGeometrySystem } from "../../../src/engine/graphics/ecs/mesh-v2/ShadedGeometrySystem.js";
import {
    three_object_to_entity_composition
} from "../../../src/engine/graphics/ecs/mesh-v2/three_object_to_entity_composition.js";
import InputController from "../../../src/engine/input/ecs/components/InputController.js";
import InputControllerSystem from "../../../src/engine/input/ecs/systems/InputControllerSystem.js";
import { TransformControls } from "./TransformControls.js";

const harness = new EngineHarness();

/**
 *
 * @param {Engine} engine
 */
async function main(engine) {
    await EngineHarness.buildBasics({
        engine,
        cameraController: false
    });

    const gltf_asset = await engine.assetManager.promise('data/models/sponza-pbr/gltf/sponza.glb', 'gltf');


    const ecd = engine.entityManager.dataset;

    // create something to drag
    const cube_entity = new Entity()
        .add(ShadedGeometry.from(
            new BoxBufferGeometry(1, 1, 1),
            new MeshStandardMaterial({
                color: 0xFFAAAA
            })
        ))
        .add(Transform.fromJSON({
            position: {
                x: 10,
                y: 0,
                z: 10
            }
        }))
        .build(ecd);

    const camera = ecd.getAnyComponent(Camera);

    const controls = new TransformControls(camera.component.object, engine.gameView.el);

    const entityNode = three_object_to_entity_composition(gltf_asset.create());
    entityNode.build(ecd);
    entityNode.transform.scale.multiplyScalar(0.1);

    controls.build(ecd); // add controls to the scene
    controls.attach(cube_entity); // make controls target the cube
    // controls.attach(entityNode.entity.id); // make controls target the cube

    new Entity()
        .add(new InputController([{
            path: 'keyboard/keys/w/down',
            listener() {
                controls.mode = "translate"
            }
        }, {
            path: 'keyboard/keys/e/down',
            listener() {
                controls.mode = "scale"
            }
        }, {
            path: 'keyboard/keys/r/down',
            listener() {
                controls.mode = "rotate"
            }
        }]))
        .build(ecd);

}

harness.initialize({
    configuration(config, engine) {
        config.addSystem(new ShadedGeometrySystem(engine));
        config.addSystem(new TransformAttachmentSystem(engine));
        config.addSystem(new InputControllerSystem(engine.devices));

        config.addLoader('gltf', new GLTFAssetLoader());
        config.addLoader('texture', new TextureAssetLoader())
    }
}).then(main);
