import {
  TextureLoader,
  ObjectLoader,
  Mesh,
} from "three";

import "three/examples/js/loaders/ColladaLoader.js";
import "three/examples/js/loaders/OBJLoader.js";

import Loader from "../utils/Loader.js";

let PROMISES = Loader.promises;

function fixColladaLoaderData(data) {
  let meshContainers = [];
  data.scene.traverse((object3D) => {
    if(object3D.children[0] instanceof Mesh) {
      meshContainers.push(object3D);
    }
    [object3D.position.y, object3D.position.z] = [object3D.position.z, -object3D.position.y];
    [object3D.rotation.y, object3D.rotation.z] = [object3D.rotation.z, -object3D.rotation.y];
    [object3D.scale.y, object3D.scale.z] = [object3D.scale.z, object3D.scale.y];
  });
  for (let object3D of meshContainers) {
    let mesh = object3D.children[0];
    object3D.remove(mesh);
    mesh.name = object3D.name;
    mesh.position.copy(object3D.position);
    mesh.rotation.copy(object3D.rotation);
    mesh.scale.copy(object3D.scale);
    mesh.geometry.rotateX(-Math.PI * .5);
    object3D.parent.add(mesh);
    object3D.parent.remove(object3D);
  }
}

export default class THREELoader {
  static get onLoad() {
    return Loader.onLoad;
  }

  static load(value) {
    let loader;
    let texture;

    if(/\.(png|jpg)$/.test(value)) {
      loader = new TextureLoader();
    }
    else if(/\.(dae)$/.test(value)) {
      loader = new THREE.ColladaLoader();
    }
    else if(/\.(obj)$/.test(value)) {
      loader = new THREE.OBJLoader();
    }
    else if(/\.(json)$/.test(value)) {
      loader = new ObjectLoader();
    }

    let promise = new Promise((resolve) => {
      texture = loader.load(value, (data) => {
        PROMISES.delete(value);
        if(/\.(dae)$/.test(value)) {
          fixColladaLoaderData(data);
        }
        resolve(data);
      });
    });

    PROMISES.set(value, promise);

    return loader instanceof TextureLoader ? texture : promise;
  }
}

@@ -272,20 +272,29 @@
 ata(data
+, %7Bscale%7D
 ) %7B%0A
-
   let me
@@ -526,32 +526,77 @@
 3D.position.y%5D;%0A
+    object3D.position.multiplyScalar(scale);%0A
     %5Bobject3D.ro
@@ -754,16 +754,58 @@
 ale.y%5D;%0A
+    object3D.scale.multiplyScalar(scale);%0A
   %7D);%0A  
@@ -1288,16 +1288,34 @@
 ad(value
+, %7Bscale = 1%7D = %7B%7D
 ) %7B%0A    
@@ -1829,24 +1829,24 @@
 t(value)) %7B%0A
-
           fi
@@ -1864,24 +1864,33 @@
 derData(data
+, %7Bscale%7D
 );%0A        %7D

add scale for ColladaLoader