import assert from "assert";
import { THREE } from "threebox-plugin";
import { Emitter, Tween } from "./particleSystem";
import MercatorCoordinate from "../../geo/mercator_coordinate";
import firefly from "./textures/firefly.png";
import { loadTexture } from "./utils";

export default class Particles {
    constructor(scene) {
        this.scene = scene;
        this.particleList = [];
    }

    async lightupCoord(options) {
        const center = MercatorCoordinate.fromLngLat(options.coord, 0);
        const texture = await loadTexture(firefly);
        const color = new THREE.Color(options.color);
        texture.needsUpdate = true;
        this.firefly = new Emitter({
            particlesPerSecond: 20,
            particleDeathAge: 4,
            positionRange: [
                new THREE.Vector3(0, 0, 0),
                options.raduis,
                [Math.PI / 2, Math.PI / 2], // phi 绕x轴角度
                [0, Math.PI * 2], // phi 绕y轴角度
            ],
            directionRange: [
                [-Math.PI / 10, Math.PI / 10], // phi 绕x轴角度
                [0, Math.PI * 2], // phi 绕y轴角度
            ],
            speedRange: [0.000001, 0.000001],
            sizeRange: [20, 40],
            colorRange: [new THREE.Vector3(color.r, color.g, color.b), 0.2],
            opacityTween: new Tween([0, 1, 2, 3, 4], [0, 1, 0, 1, 0]),
            blendMode: THREE.NormalBlending,
            depthTest: true,
            texture,
        });

        const mesh = this.firefly.mesh;
        mesh.rotateX(Math.PI / 2);
        mesh.position.set(center.x, center.y, center.z);
        this.particleList.push(mesh);

        this.scene.add(mesh);
        this.firefly.fire();
    }

    setFlotsam(options) {
        assert(options.texture, "请上传贴图！");
        const center = MercatorCoordinate.fromLngLat(
            options.coord,
            options.height
        );
        const texture = new THREE.Texture(options.texture);
        texture.needsUpdate = true;
        const flotsam = new Emitter({
            particlesPerSecond: options.perSecond,
            particleDeathAge: options.deathAge,
            positionRange: [
                new THREE.Vector3(0, 0, 0),
                options.raduis,
                [Math.PI / 2, Math.PI / 2], // phi 绕x轴角度
                [0, Math.PI * 2],
            ],
            directionRange: [
                [Math.PI - Math.PI / 10, Math.PI + Math.PI / 10], // phi 绕x轴角度
                [-Math.PI / 10, Math.PI / 10], // phi 绕y轴角度
            ],
            speedRange: [0.00001, 0.00002],
            sizeRange: options.sizeRange,
            colorRange: [new THREE.Vector3(1, 1, 1), 0],
            opacityTween: new Tween(
                [0, 0.2, options.deathAge],
                [0, 1, 0]
            ),
            blendMode: THREE.NormalBlending,
            depthTest: true,
            texture,
        });

        const mesh = flotsam.mesh;
        mesh.rotateX(Math.PI / 2);
        mesh.position.set(center.x, center.y, center.z);
        this.particleList.push(mesh);

        this.scene.add(mesh);
        flotsam.fire();
    }

    scale(zoom) {
        this.particleList.forEach((mesh) => {
            mesh.material.uniforms["u_zoom"].value = zoom;
        });
    }
}
