import type { Group, Object3DEventMap } from 'three';
import type { axisId } from '../../../osr-emu.js';
import { MeshPhongMaterial, MeshStandardMaterial } from 'three';

import {
  loadObj,
  recomputeNormals,
} from '../../util.js';
import baseGeometry from './geometry/base.js';
import railsGeometry from './geometry/rails.js';
import receiverGeometry from './geometry/receiver.js';

/**
 * 3D Model of the SSR1
 */
export default class SSR1Model {
  _orientation = -Math.PI / 2; // Rotation around the x-axis.
  objects!: Record<string, Group<Object3DEventMap>>;

  load() {
    const ayvaBlueMaterial = new MeshPhongMaterial({ color: 0x3F5173 });
    const darkMaterial = new MeshPhongMaterial({ color: 0x1E1E1E });
    const railMaterial = new MeshStandardMaterial({
      color: 0xC0C0C0,
      metalness: 1,
      roughness: 0.5,
    });

    const base = loadObj(baseGeometry, darkMaterial);
    const receiver = loadObj(receiverGeometry, ayvaBlueMaterial);
    const rails = loadObj(railsGeometry, railMaterial, recomputeNormals);

    this.objects = {
      base,
      receiver,
      rails,
    };

    return { objects: this.objects, orientation: this._orientation };
  }

  preRender(axes: Record<axisId, number>, scale: Record<axisId, number>) {
    const position = -scale.L0 * axes.L0 * 120; // 120mm stroke
    this.objects.receiver.position.y = position;
  }
}
