import { EngineHarness } from "../../../src/engine/EngineHarness.js";
import { GizmoRenderingPlugin } from "../../../src/engine/graphics/render/gizmo/GizmoRenderingPlugin.js";
import { GameAssetType } from "../../../src/engine/asset/GameAssetType.js";
import { TextureAssetLoader } from "../../../src/engine/asset/loaders/texture/TextureAssetLoader.js";
import Vector2 from "../../../src/core/geom/Vector2.js";
import { enableEditor } from "../../enableEditor.js";
import { initializeEditor } from "../../../../model/game/initializeEditor.js";

import '../../../../../../css/game.scss';
import '../../../../../../css/editor/EntityEditorView.scss';
import '../../../../../../css/editor/EditorView.scss';
import { TerrainLayer } from "../../../src/engine/ecs/terrain/ecs/layers/TerrainLayer.js";
import Terrain from "../../../src/engine/ecs/terrain/ecs/Terrain.js";
import Vector3 from "../../../src/core/geom/Vector3.js";
import { BlenderCameraOrientationGizmo } from "../v2/BlenderCameraOrientationGizmo.js";
import { Transform } from "../../../src/engine/ecs/transform/Transform.js";
import TopDownCameraController from "../../../src/engine/graphics/ecs/camera/topdown/TopDownCameraController.js";

new EngineHarness()
    .initialize({
        configuration(config, engine) {

            config.addPlugin(GizmoRenderingPlugin);

            config.addLoader(GameAssetType.Texture, new TextureAssetLoader());
        }
    }).then(main);

/**
 *
 * @param {Engine} engine
 * @returns {Promise<void>}
 */
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
    });

    const ecd = engine.entityManager.dataset;
    const terrain_entity = ecd.getAnyComponent(Terrain);
    const terrain = terrain_entity.component;

    terrain.samplerHeight.resize(256, 256);
    await terrain.updateHeights();


    terrain.layers.addLayer(TerrainLayer.from("data/textures/materials/terrain_township_set/512/Grass_1.png", 20, 20));
    terrain.layers.addLayer(TerrainLayer.from("data/textures/materials/terrain_township_set/512/Ground_1.png", 20, 20));
    terrain.layers.addLayer(TerrainLayer.from("data/textures/materials/RAYGEAS/Stylized Ground Textures/Sand_2/Sand_2_AlbedoSmoothness.png", 20, 20));
    terrain.layers.buildTexture();


    terrain.splat.resize(256, 256, terrain.layers.count())

    await terrain.layers.loadTextureData(engine.assetManager);

    terrain.splat.fillLayerWeights(0, 50);

    terrain.splat.weightTexture.needsUpdate = true;
    terrain.updateMaterial();

    const editor_controls = enableEditor(engine, initializeEditor);

    editor_controls.enable();

    const camera_orientation_gizmo = new BlenderCameraOrientationGizmo();

    camera_orientation_gizmo.css({
        position: 'absolute',
        top: 0,
        right: 0,
        pointerEvents: 'auto'
    });


    camera_orientation_gizmo.on.axisSelected.add((axis) => {
        /**
         *
         * @type {TopDownCameraController}
         */
        const tdcc = editor_controls.editor.cameraEntity.getComponent(TopDownCameraController);

        tdcc.pitch = 0;
        tdcc.yaw = 0;
        tdcc.roll = 0;

        switch (axis) {
            case 'x':
                tdcc.yaw = Math.PI / 2;
                break;
            case '-x':
                tdcc.yaw = -Math.PI / 2;
                break;
            case 'z':
                tdcc.yaw = 0;
                break;
            case '-z':
                tdcc.yaw = Math.PI;
                break;
            case 'y':
                tdcc.pitch = Math.PI / 2;
                break;
            case '-y':
                tdcc.pitch = -Math.PI / 2;
                break;
        }
    });

    engine.gameView.addChild(camera_orientation_gizmo);

    engine.graphics.on.preRender.add(() => {
        const camera_transform = editor_controls.editor.cameraEntity.getComponent(Transform);

        if (camera_transform === null) {
            return;
        }

        camera_orientation_gizmo.orientation = camera_transform.rotation;

        camera_orientation_gizmo.update();
    });

    setTimeout(() => {
        editor_controls.editor.selection.add(terrain_entity.entity);

        // camera_orientation_gizmo.orientation =;
    }, 100);

    console.warn(terrain);
}
