import * as THREE from "three";
import HorizontalBox from "./HorizontalBox";
import VerticalBox from "./VerticalBox";
import SimpleLabel from "./object/SimpleLabel";

// import { Line2 } from "three/examples/jsm/lines/Line2.js";
// import { LineGeometry } from "three/examples/jsm/lines/LineGeometry.js";
// import { LineMaterial } from "three/examples/jsm/lines/LineMaterial.js";

export default class BoxManager {
  constructor(scene) {
    this.scene = scene;
    this.loadingManager = new THREE.LoadingManager();
    this.loader = new THREE.TextureLoader(this.loadingManager);
    this.model = new THREE.Group();
    this.model.name = "boxManager";
    this.maps = {};
    this.obb = new THREE.Box3();
    this.imgList = [];
    this.opacity = 1;

    this.onBindEvent();
  }

  load = (list, type) => {
    console.log("this.model.name", this.model.name);
    const total = list.length;
    list.forEach((item, index) => {
      if (type === 1) {
        //横排
        console.log("横排");
        const box = new HorizontalBox(this, item, index, total);
        this.model.add(box);
      }
      if (type === 2) {
        //竖排
        const box = new VerticalBox(this, item, index, total);
        // console.log("竖排");
        this.model.add(box);
      }
    });
    // this.model.position.y += 0.3;
    // this.model.visible =false;
    this.scene.scene.add(this.model);
    this.setArea();
    // console.log("this.scene.scene", this.scene.scene);
  };

  onBindEvent = () => {
    const _this = this;
    this.loadingManager.onStart = (url, itemsLoaded, itemsTotal) => {
      // console.log( 'Started loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' );
      console.log("loading_manager: loading...");
 
    };
    this.loadingManager.onLoad = () => {
      console.log("loading_manager: loading complete!");
      this.scene.emit("loaded");

    };
    this.loadingManager.onProgress = function (url, itemsLoaded, itemsTotal) {
      // console.log( 'Loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' );
    };
    this.loadingManager.onError = function (url) {
      console.error("loading_manager: error loading " + url);
    };
  };
  setArea() {
    const object = this.model;
    const boundingBox = new THREE.Box3().setFromObject(object);
    // boundingBox.expandByScalar(1.3);
    const size = boundingBox.getSize(new THREE.Vector3());
    this.obb = boundingBox;
    console.log("boundingBox-size", size);
    // const topLine = [
    //   -size.x,
    //   0,
    //   (size.z + 0.3) / -2,
    //   size.x + 0.1,
    //   0,
    //   (size.z + 0.3) / -2,
    // ];

    // helper.scale.set(1.2, 1.2, 1.2);
    // helper.update();
  }
  setVisible = (val) => {
    if (!this.model) return;
    this.model.visible = val;
  };

  setOpacity = (val) => {
    this.material.opacity = val;
  };
}
