///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2026, Open Design Alliance (the "Alliance").
// All rights reserved.
//
// This software and its documentation and related materials are owned by
// the Alliance. The software may only be incorporated into application
// programs owned by members of the Alliance, subject to a signed
// Membership Agreement and Supplemental Software License Agreement with the
// Alliance. The structure and organization of this software are the valuable
// trade secrets of the Alliance and its suppliers. The software is also
// private by copyright law and international treaty provisions. Application
// programs incorporating this software must include the following statement
// with their copyright notices:
//
//   This application incorporates Open Design Alliance software pursuant to a
//   license agreement with Open Design Alliance.
//   Open Design Alliance Copyright (C) 2002-2026 by Open Design Alliance.
//   All rights reserved.
//
// By use of this software, its documentation or related materials, you
// acknowledge and accept the above terms.
///////////////////////////////////////////////////////////////////////////////

import { IComponent, RGB, ResizeEvent } from "@inweb/viewer-core";
import type { Viewer } from "../Viewer";
import { SectionsHelper } from "../helpers/SectionsHelper.js";
import { Object3D } from "three";

export class SectionsComponent implements IComponent {
  private viewer: Viewer;
  private sectionsHelper: SectionsHelper;
  private updateTimerId = 0;
  private updateDelay = 250;

  constructor(viewer: Viewer) {
    this.sectionsHelper = new SectionsHelper();

    this.viewer = viewer;
    this.viewer.addEventListener("initialize", this.syncHelper);
    this.viewer.addEventListener("databasechunk", this.syncHelper);
    this.viewer.addEventListener("drawviewpoint", this.syncHelper);
    this.viewer.addEventListener("changecuttingplanes", this.syncSections);
    this.viewer.addEventListener("explode", this.syncSections);
    this.viewer.addEventListener("hide", this.syncSections);
    this.viewer.addEventListener("isolate", this.syncSections);
    this.viewer.addEventListener("show", this.syncSections);
    this.viewer.addEventListener("showall", this.syncSections);
    this.viewer.addEventListener("resize", this.viewerResize);
    this.viewer.addEventListener("optionschange", this.syncOptions);

    this.syncOptions();
  }

  dispose() {
    clearTimeout(this.updateTimerId);

    this.sectionsHelper.removeFromParent();
    this.sectionsHelper.dispose();

    this.viewer.removeEventListener("initialize", this.syncHelper);
    this.viewer.removeEventListener("databasechunk", this.syncHelper);
    this.viewer.removeEventListener("drawviewpoint", this.syncHelper);
    this.viewer.removeEventListener("changecuttingplanes", this.syncSections);
    this.viewer.removeEventListener("explode", this.syncSections);
    this.viewer.removeEventListener("hide", this.syncSections);
    this.viewer.removeEventListener("isolate", this.syncSections);
    this.viewer.removeEventListener("show", this.syncSections);
    this.viewer.removeEventListener("showall", this.syncSections);
    this.viewer.removeEventListener("resize", this.viewerResize);
    this.viewer.removeEventListener("optionschange", this.syncOptions);
  }

  syncOptions = () => {
    function rgbToHex(c: any): string {
      const hex = (v = 0) => v.toString(16).padStart(2, "0");
      return "#" + hex(c.r) + hex(c.g) + hex(c.b);
    }

    const options = this.viewer.options;
    const flags = this.sectionsHelper.flags;

    flags.fillEnabled = options.enableSectionFill;
    flags.fillColor = rgbToHex(options.sectionFillColor);
    flags.useObjFillColor = options.sectionUseObjectColor;
    flags.hatchEnabled = options.enableSectionHatch;
    flags.hatchColor = rgbToHex(options.sectionHatchColor);
    flags.hatchScale = options.sectionHatchScale;
    flags.outlineEnabled = options.enableSectionOutline;
    flags.outlineColor = rgbToHex(options.sectionOutlineColor);
    flags.outlineWidth = options.sectionOutlineWidth;

    this.syncSections();
  };

  syncHelper = () => {
    this.sectionsHelper.removeFromParent();
    this.viewer.helpers.add(this.sectionsHelper);
  };

  syncSections = () => {
    this.sectionsHelper.visible = false;
    clearTimeout(this.updateTimerId);
    this.updateTimerId = window.setTimeout(this.updateSections, this.updateDelay);
  };

  viewerResize = (event: ResizeEvent) => {
    this.sectionsHelper.setSize(event.width, event.height);
  };

  updateSections = () => {
    const objects: Object3D[][] = [];
    this.viewer.models.forEach((model) => objects.push(model.getVisibleObjects()));
    const objects2 = objects.flat();

    this.sectionsHelper.update(objects2, this.viewer.extents, this.viewer.clippingPlanes);
    this.sectionsHelper.visible = true;
    this.viewer.update();
  };
}
