import { getBoundingBox, getBoundingBoxCenterPoint, getBoundingBoxMaxLength, centerOrigin } from "@/lib/MeshUtils";
import { SphereGeometry, Mesh, MeshStandardMaterial, BackSide, Group } from "three";
import { EventManager } from '@/lib/EventManager';

class SceneSwitcher extends EventManager{
    emits = ['sceneSwitch']
    constructor(engine, data){
        super();
        return new Promise(async(resolve, reject)=>{
            this.source = this;
            this.object = new Group()
            if (data.switchType == 'award'){
                let gltf = await engine.load('trophy.glb', '/static/meshes/scene-switcher/');
                this.object.add(gltf.scene);
                engine.motionQueue.add({ 
                    o: gltf.scene,
                    a:{rotation: { y: k=>k*Math.PI*2 }}, r: true, t: 4 
                })
            }else if(data.switchType == 'sphere'){
                let geo = new SphereGeometry(1);
                let material = new MeshStandardMaterial({
                    map: await engine.loadTexture(data.$go_env.asset.name)
                })
                let sphere = new Mesh(geo, material);
                sphere.position.y = 0.5;
                this.object.add(sphere);
            }else{
                //sensor, TODO!!!, to be implemented
            }
            engine.clickable.add(this.object, async e=>{
                this.dispatchEvent({type:'sceneSwitch', scene: data.switchScene});
            })
            resolve(this);
        })
    }
}

export { SceneSwitcher }