import XStaticMesh from "./XStaticMesh.js"
import XLowpolyModelError from "./error/XLowpolyModelError.js"
import util from "./util.js"
import EMeshType from "./enum/EMeshType.js"
import Logger from "./Logger.js"

const logger = new Logger('XStaticMeshComponent')
export default class XStaticMeshFromOneGltf {
    constructor(e, t) {
        E(this, "_scene");
        E(this, "_url");
        E(this, "_group");
        E(this, "_pickable");
        E(this, "_meshes");
        E(this, "_id");
        E(this, "_lod");
        E(this, "_groupUuid");
        E(this, "_isInScene");
        E(this, "_skinInfo");
        E(this, "loadMesh", (e,t)=>{
            const r = this._meshes.length
              , n = t ? 1 : 0
              , o = this._url;
            return BABYLON.SceneLoader.LoadAssetContainerAsync("", o, this._scene, ()=>{
                this._scene.blockMaterialDirtyMechanism = !0
            }
            , ".glb").then(a=>{
                for (let s = a.materials.length - 1; s >= 0; --s)
                    a.materials[s].dispose();
                this._scene.blockMaterialDirtyMechanism = !0;
                for (let s = 0; s < a.meshes.length; ++s) {
                    const l = a.meshes[s];
                    if ("instances"in l) {
                        "visibility"in l && (l.visibility = 0),
                        "isPickable"in l && (l.isPickable = this._pickable),
                        e != null && (l.material = e),
                        "hasVertexAlpha"in l && (l.hasVertexAlpha = !1);
                        const u = new XStaticMesh({
                            id: this._groupUuid + "-" + Math.random().toString(36).substr(2, 5),
                            mesh: l,
                            lod: this._lod,
                            group: this._group,
                            url: this._url,
                            xtype: EMeshType.XStaticMesh,
                            skinInfo: this._skinInfo
                        });
                        this._meshes.push(u)
                    }
                    this._scene.addMesh(l)
                }
                return !0
            }
            ).then(()=>{
                this._isInScene = !0;
                for (let a = r; a < this._meshes.length; ++a)
                    this._meshes[a].mesh.visibility = n;
                return Promise.resolve(!0)
            }
            ).catch(a=>{
                logger.error("[Engine] input gltf mesh uri error! " + a),
                Promise.reject(new XLowpolyModelError("[Engine] input gltf mesh uri error! " + a))
            }
            )
        }
        );
        this._meshes = [],
        this._scene = e,
        this._url = t.url,
        t.group != null ? this._group = t.group : this._group = "default",
        t.pick != null ? this._pickable = t.pick : this._pickable = !1,
        t.id != null ? this._id = t.id : this._id = "default",
        t.lod != null ? this._lod = t.lod : this._lod = -1,
        t.skinInfo != null ? this._skinInfo = t.skinInfo : this._skinInfo = "default",
        this._groupUuid = util.uuid(),
        this._isInScene = !1
    }
    get isinscene() {
        return this._isInScene
    }
    get groupUuid() {
        return this._groupUuid
    }
    get skinInfo() {
        return this._skinInfo
    }
    get group() {
        return this._group
    }
    get meshes() {
        return this._meshes
    }
    get url() {
        return this._url
    }
    get id() {
        return this._id
    }
    get lod() {
        return this._lod
    }
    removeFromScene() {
        if (this._isInScene) {
            this._isInScene = !1;
            for (let e = 0, t = this._meshes.length; e < t; ++e)
                this._meshes[e].mesh != null && this._scene.removeMesh(this._meshes[e].mesh)
        }
    }
    addToScene() {
        if (this._isInScene == !1) {
            this._isInScene = !0;
            for (let e = 0, t = this._meshes.length; e < t; ++e)
                this._meshes[e].mesh != null && this._scene.addMesh(this._meshes[e].mesh)
        }
    }
    toggleVisibility(e) {
        const t = e ? 1 : 0;
        for (let r = 0, n = this._meshes.length; r < n; ++r)
            "visibility"in this._meshes[r].mesh && (this._meshes[r].mesh.visibility = t)
    }
    togglePickable(e) {
        for (let t = 0, r = this._meshes.length; t < r; ++t)
            "isPickable"in this._meshes[t].mesh && (this._meshes[t].mesh.isPickable = e)
    }
    setMaterial(e) {
        for (let t = 0, r = this._meshes.length; t < r; ++t)
            "material"in this._meshes[t].mesh && (this._meshes[t].mesh.material = e)
    }
    dispose() {
        for (let e = 0, t = this._meshes.length; e < t; ++e)
            this._meshes[e].mesh.dispose(!1, !1)
    }
}