///////////////////////////////////////////////////////////////////////////////
// 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
// 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-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 { Box3 } from "three";
import { PointCloudOctree } from "potree-core";
import { IInfo, Info } from "@inweb/viewer-core";
import { ModelImpl } from "@inweb/viewer-three";

// Potree model implementation.

export class PotreeModelImpl extends ModelImpl {
  public pco: PointCloudOctree;

  override getInfo(): IInfo {
    // ===================== AI-CODE-START ======================
    // Source: Claude Sonnet 4.5
    // Date: 2025-10-02
    // Reviewer: roman.mochalov@opendesign.com
    // Issue: CLOUD-5738

    let totalPoints = 0;
    let totalNodes = 0;
    let loadedGeometryBytes = 0;
    let estimatedTotalGeometryBytes = 0;
    let geometryBytes = 0;

    const bytesPerPoint = this.pco.pcoGeometry?.pointAttributes?.byteSize || 0;

    if (this.pco.pcoGeometry && this.pco.pcoGeometry.root) {
      this.pco.pcoGeometry.root.traverse((node: any) => {
        totalNodes++;

        const numPoints = node.numPoints || 0;
        totalPoints += numPoints;

        if (node.loaded && node.geometry) {
          if (node.geometry.attributes) {
            for (const name in node.geometry.attributes) {
              const attribute = node.geometry.attributes[name];
              if (attribute && attribute.array) {
                loadedGeometryBytes += attribute.array.byteLength;
              }
            }
          }

          if (node.geometry.index && node.geometry.index.array) {
            loadedGeometryBytes += node.geometry.index.array.byteLength;
          }
        }

        if (bytesPerPoint > 0 && numPoints > 0) {
          estimatedTotalGeometryBytes += numPoints * (bytesPerPoint * 2 - 1);
        }
      }, true);
    }

    geometryBytes = Math.max(loadedGeometryBytes, estimatedTotalGeometryBytes);

    // ===================== AI-CODE-END ======================

    const info = new Info();

    info.scene.objects = totalNodes;
    info.scene.points = totalPoints;
    info.scene.triangles = 0;
    info.scene.lines = 0;
    info.scene.edges = 0;

    info.memory.geometries = totalNodes;
    info.memory.geometryBytes = Math.floor(geometryBytes);
    info.memory.textures = 0;
    info.memory.textureBytes = 0;
    info.memory.materials = 1;
    info.memory.totalEstimatedGpuBytes = Math.floor(geometryBytes);

    info.optimizedScene.objects = info.scene.objects;
    info.optimizedScene.triangles = info.scene.triangles;
    info.optimizedScene.points = info.scene.points;
    info.optimizedScene.lines = info.scene.lines;
    info.optimizedScene.edges = info.scene.edges;

    return info;
  }

  override getExtents(target: Box3): Box3 {
    return target.union(this.pco.pcoGeometry.boundingBox);
  }
}
