import Mesh from "../graphics/ecs/mesh/Mesh.js";
import {
    distributeParticlesOnObject3D
} from "../graphics/particles/particular/engine/utils/distributeParticlesOnObject3D.js";
import { removeComponentsExcept } from "../ecs/util/removeComponentsExcept.js";
import { ParticleEmitter } from "../graphics/particles/particular/engine/emitter/ParticleEmitter.js";
import Trail2D from "../graphics/ecs/trail2d/Trail2D.js";
import { Transform } from "../ecs/transform/Transform.js";
import Entity from "../ecs/Entity.js";
import { stopEntityAndNotifyWhenStopped } from "./AnimatedActions.js";

const lib = {
    'mesh-cloud-puff-0': {
        "position": {
            "x": 9.17416,
            "y": -1.90031,
            "z": 12.06082
        },
        "scale": {
            "x": 1,
            "y": 1,
            "z": 1
        },
        "rotation": {
            "x": 0,
            "y": 0,
            "z": 0,
            "w": 1
        },
        "parameters": [
            {
                "name": "scale",
                "itemSize": 1,
                "defaultTrackValue": {
                    "itemSize": 1,
                    "data": [
                        1
                    ],
                    "positions": [
                        0
                    ]
                }
            },
            {
                "name": "color",
                "itemSize": 4,
                "defaultTrackValue": {
                    "itemSize": 4,
                    "data": [
                        1,
                        1,
                        1,
                        1
                    ],
                    "positions": [
                        0
                    ]
                }
            }
        ],
        "preWarm": false,
        "readDepth": true,
        "softDepth": true,
        "blendingMode": 0,
        "layers": [
            {
                "imageURL": "data/textures/particle/smokeparticle.png",
                "particleLife": {
                    "min": 0.6,
                    "max": 1.2
                },
                "particleSize": {
                    "min": 0.2,
                    "max": 0.3
                },
                "particleRotation": {
                    "min": 0,
                    "max": 0
                },
                "particleRotationSpeed": {
                    "min": 0,
                    "max": 0
                },
                "emissionShape": 1,
                "emissionFrom": 1,
                "emissionRate": 0,
                "emissionImmediate": 1200,
                "parameterTracks": [
                    {
                        "name": "color",
                        "track": {
                            "itemSize": 4,
                            "data": [
                                0.9803921568627451,
                                0.9803921568627451,
                                0.9803921568627451,
                                0.3,
                                0.9882352941176471,
                                0.9882352941176471,
                                0.9882352941176471,
                                0.265,
                                0.9882352941176471,
                                0.9882352941176471,
                                0.9882352941176471,
                                0
                            ],
                            "positions": [
                                0,
                                0.7957639171068672,
                                1
                            ]
                        }
                    }
                ],
                "position": {
                    "x": 0,
                    "y": 0.6,
                    "z": 0
                },
                "scale": {
                    "x": 0.9,
                    "y": 0.5,
                    "z": 0.65
                },
                "particleVelocityDirection": {
                    "direction": {
                        "x": 0,
                        "y": 1,
                        "z": 0
                    },
                    "angle": 0
                },
                "particleSpeed": {
                    "min": 0.6,
                    "max": 1.1
                }
            }
        ]
    }
};

/**
 *
 * @param {EntityManager} entityManager
 * @param {EntityComponentDataset} ecd
 * @param {number} entity
 * @param {string} particles
 * @returns {Promise<any>}
 */
export function removeEntityWithMeshParticlesEffect(
    {
        entityManager,
        ecd,
        entity,
        particles = 'mesh-cloud-puff-0'
    }
) {

    const emitter = ParticleEmitter.fromJSON(lib[particles]);

    //get entity mesh

    const cMesh = ecd.getComponent(entity, Mesh);

    if (cMesh !== undefined) {
        if (cMesh.mesh !== null) {
            const randomSeed = Math.random() * 1000000;

            distributeParticlesOnObject3D(emitter, randomSeed, cMesh.mesh);
        }
    }

    //prevent further interactions
    removeComponentsExcept(ecd, entity, [Mesh, ParticleEmitter, Trail2D, Transform]);

    /**
     *
     * @type {Transform}
     */
    const originalTransform = ecd.getComponent(entity, Transform);

    if (originalTransform === undefined) {
        throw new Error(`entity ${entity} doesn't have Transform`);
    }

    //create particle emitter
    const effectBuilder = new Entity();
    const effectEntity = effectBuilder
        .add(emitter)
        .add(originalTransform.clone())
        .build(ecd);

    Promise.all([
        stopEntityAndNotifyWhenStopped(effectEntity, ecd)
    ])
        .then(() => effectBuilder.destroy());

    return stopEntityAndNotifyWhenStopped(entity, ecd)
        .then(() => {
            ecd.removeEntity(entity);
        });

}
