import {TypedContainer} from "./_Base";
import {CoreGeometry} from "../../core/geometry/Geometry";
import {CoreObject} from "../../core/geometry/Object";
export class GeometryContainer extends TypedContainer {
  coreContentCloned() {
    if (this._content) {
      return this._content.clone();
    }
  }
  set_content(content) {
    super.set_content(content);
  }
  firstObject() {
    if (this._content) {
      return this._content.objects()[0];
    }
  }
  firstCoreObject() {
    const object = this.firstObject();
    if (object) {
      return new CoreObject(object, 0);
    }
  }
  firstGeometry() {
    const object = this.firstObject();
    if (object) {
      return object.geometry;
    } else {
      return null;
    }
  }
  objectsCount() {
    if (this._content) {
      return this._content.objects().length;
    } else {
      return 0;
    }
  }
  objectsVisibleCount() {
    let count = 0;
    if (this._content) {
    }
    return count;
  }
  objectsCountByType() {
    const count_by_type = {};
    const core_group = this._content;
    if (this._content && core_group) {
      for (let core_object of core_group.coreObjects()) {
        const human_type = core_object.humanType();
        if (count_by_type[human_type] == null) {
          count_by_type[human_type] = 0;
        }
        count_by_type[human_type] += 1;
      }
    }
    return count_by_type;
  }
  objectsNamesByType() {
    const names_by_type = {};
    const core_group = this._content;
    if (this._content && core_group) {
      for (let core_object of core_group.coreObjects()) {
        const human_type = core_object.humanType();
        names_by_type[human_type] = names_by_type[human_type] || [];
        names_by_type[human_type].push(core_object.name());
      }
    }
    return names_by_type;
  }
  pointAttributeNames() {
    let names = [];
    const geometry = this.firstGeometry();
    if (geometry) {
      names = Object.keys(geometry.attributes);
    }
    return names;
  }
  pointAttributeSizesByName() {
    let sizes_by_name = {};
    const geometry = this.firstGeometry();
    if (geometry) {
      Object.keys(geometry.attributes).forEach((attrib_name) => {
        const attrib = geometry.attributes[attrib_name];
        sizes_by_name[attrib_name] = attrib.itemSize;
      });
    }
    return sizes_by_name;
  }
  objectAttributeSizesByName() {
    let sizes_by_name = {};
    const core_object = this.firstCoreObject();
    if (core_object) {
      const attribNames = core_object.attribNames();
      for (let name of attribNames) {
        const size = core_object.attribSize(name);
        if (size != null) {
          sizes_by_name[name] = size;
        }
      }
    }
    return sizes_by_name;
  }
  pointAttributeTypesByName() {
    let types_by_name = {};
    const geometry = this.firstGeometry();
    if (geometry) {
      const core_geo = new CoreGeometry(geometry);
      Object.keys(geometry.attributes).forEach((attrib_name) => {
        types_by_name[attrib_name] = core_geo.attribType(attrib_name);
      });
    }
    return types_by_name;
  }
  objectAttributeTypesByName() {
    let types_by_name = {};
    const core_object = this.firstCoreObject();
    if (core_object) {
      for (let name of core_object.attribNames()) {
        types_by_name[name] = core_object.attribType(name);
      }
    }
    return types_by_name;
  }
  objectAttributeNames() {
    let names = [];
    const object = this.firstObject();
    if (object) {
      names = Object.keys(object.userData["attributes"] || {});
    }
    return names;
  }
  pointsCount() {
    if (this._content) {
      return this._content.pointsCount();
    } else {
      return 0;
    }
  }
  totalPointsCount() {
    if (this._content) {
      return this._content.totalPointsCount();
    } else {
      return 0;
    }
  }
  objectsData() {
    if (this._content) {
      return this._content.objectsData();
    } else {
      return [];
    }
  }
  boundingBox() {
    return this._content.boundingBox();
  }
  center() {
    return this._content.center();
  }
  size() {
    return this._content.size();
  }
}
