///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2025, 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
// protected 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-2025 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 { Euler, Vector3 } from "three";
import { PointColorType, PointCloudOctree, PointSizeType, PointShape, Potree } from "potree-core";
import { Loader, LoadParams, Viewer } from "@inweb/viewer-three";

import { PotreeModelImpl } from "./PotreeModelImpl";

export type PotreeLoadParams = LoadParams & {
  path?: string;
  position?: Vector3;
  rotation?: Euler;
  scale?: Vector3;
};

export class PotreeFileLoader extends Loader {
  public viewer: Viewer;
  public potree: Potree;
  public pointClouds: PointCloudOctree[];

  constructor(viewer: Viewer) {
    super();
    this.viewer = viewer;

    this.potree = new Potree();
    this.pointClouds = [];

    this.viewer.addEventListener("render", this.updatePointClouds);
    this.viewer.addEventListener("changecamera", this.updatePointClouds);
  }

  override dispose() {
    this.pointClouds.forEach((pco) => pco.dispose());

    this.viewer.removeEventListener("render", this.updatePointClouds);
    this.viewer.removeEventListener("changecamera", this.updatePointClouds);
  }

  override isSupport(file: any, format?: string): boolean {
    return typeof file === "string" && /(cloud.js|metadata.json)$/i.test(file);
  }

  override async load(file: any, format?: string, params: PotreeLoadParams = {}): Promise<this> {
    const path = (params.path || "") + file;
    const index = path.lastIndexOf("/");
    const baseUrl = path.slice(0, index + 1);
    const url = path.slice(index + 1);

    const pco = await this.potree.loadPointCloud(url, baseUrl);
    pco.material.size = 1.0;
    pco.material.shape = 2;
    pco.material.inputColorEncoding = 1;
    pco.material.outputColorEncoding = 1;
    pco.material.pointColorType = PointColorType.RGB; // RGB | DEPTH | HEIGHT | POINT_INDEX | LOD | CLASSIFICATION
    pco.material.pointSizeType = PointSizeType.ADAPTIVE; // ADAPTIVE | FIXED
    pco.material.shape = PointShape.CIRCLE; // CIRCLE | SQUARE

    if (params.position) pco.position.copy(params.position);
    if (params.rotation) pco.rotation.copy(params.rotation);
    if (params.scale) pco.scale.copy(params.scale);

    this.pointClouds.push(pco);

    const modelImpl = new PotreeModelImpl(pco);
    modelImpl.id = params.modelId || this.extractFileName(file);
    modelImpl.pco = pco;

    this.viewer.scene.add(pco);
    this.viewer.models.push(modelImpl);

    this.viewer.syncOptions();
    this.viewer.syncOverlay();

    this.viewer.emitEvent({ type: "databasechunk", data: pco, file });
    this.viewer.update(true);

    return this;
  }

  updatePointClouds = () => {
    const result = this.potree.updatePointClouds(this.pointClouds, this.viewer.camera, this.viewer.renderer);
    if (result.exceededMaxLoadsToGPU || result.nodeLoadPromises.length > 0) this.viewer.update();
  };
}
