import { Group, CylinderGeometry, Mesh, MeshStandardMaterial, Vector3 } from 'three';
import { EventManager } from '@/lib/EventManager';
//import { TextObject } from '../TextObject/TextObject';

class Teleporter extends EventManager {
    emits = ['interaction']
    constructor(engine, data) {
        super();
        return new Promise(async (resolve, reject)=>{
            const container = new Group();
            const geometry = new CylinderGeometry(0.25, 0.25, 0.1);
            const material = new MeshStandardMaterial({ 
                color: 0x55aaff, roughness: 0.1, metalness: 0.9
            });
            const s1 = new Mesh(geometry, material);
            s1.isIndividual = true;
            s1.name = 'T1';
            engine.clickable.add(s1, ()=>{
                this.dispatchEvent({type:'interaction', value: s1.name});
                let v = new Vector3().copy(s2.position);
                v.applyMatrix4(this.object.matrix);
                engine.hero?.teleport(v)
            });
            const s2 = s1.clone();
            s2.isIndividual = true;
            s2.name = 'T2';
            engine.clickable.add(s2, ()=>{
                this.dispatchEvent({type:'interaction', value: s2.name})
                let v = new Vector3().copy(s1.position);
                v.applyMatrix4(this.object.matrix);
                engine.hero?.teleport(v)
            });
            s1.position.x = -1;
            s2.position.x = 1;
            container.add(s1);
            container.add(s2);

            this.object = engine.meshUtils.centerOrigin(container);
            this.object.hasIndividualChildren = true;
            resolve(this);
        })
    }
}

export { Teleporter }