import { TextureLoader, MeshStandardMaterial, MeshBasicMaterial, PlaneGeometry, Mesh, DoubleSide } from "three";
import { assignParams } from "@/lib/MeshUtils";

class ImageObject {
    constructor(engine, obj) {
        return new Promise(async(resolve, reject)=>{
            var t = await engine.loadTexture(obj.value, obj.path)
            var mp = {
                map: t,
                alphaTest: 0.5,
                side: DoubleSide
            };
            if (obj.nm) {
                mp.normalMap = new TextureLoader().setPath(obj.path).load(obj.nm);
            }
            if (obj.em) {
                mp.emissiveMap = new TextureLoader().setPath(obj.path).load(obj.em);
            }
            if (obj.am) {
                mp.alphaMap = new TextureLoader().setPath(obj.path).load(obj.am);
            }
            obj.material && Object.assign(mp, obj.material);
            let geo = new PlaneGeometry(obj.width || 1, obj.height || 1);
            if (obj.uv) {
                var uvAttribute = geo.attributes.uv;
                for (var i = 0; i < uvAttribute.count; i++) {
                    uvAttribute.setXY(i, obj.uv[2 * i], obj.uv[2 * i + 1]);
                }
            }
            this.object = new Mesh(geo, mp.metalness ? new MeshStandardMaterial(mp) : new MeshBasicMaterial(mp));
            resolve(this)
        })
    }
}

export {ImageObject}