import * as BABYLON from 'babylonjs';
import { LIGHTS } from '../constants';
import { newVector, scene } from '../helpers';

export const newLight = (name, location, type, meta = {}) => {
  const defaultName = `light_${getSceneLights().length + 1}`;

  if (type === LIGHTS.SpotLight) {
    const orientation = meta.orientation;
    return new BABYLON.SpotLight(
      name || `${defaultName}_spot`,
      newVector(location.x, location.y, location.z),
      newVector(orientation.x, orientation.y, orientation.z),
      meta.angle,
      meta.exponent,
      scene,
    );
  }

  if (type === LIGHTS.ShadowGenerator) {
    return new BABYLON.ShadowGenerator(
      meta.mapSize,
      meta.dirLight ||
        newLight(
          defaultName,
          newVector(location.x, location.y, location.z),
          LIGHTS.DirectionalLight,
        ),
    );
  }

  if (LIGHTS[type] && typeof BABYLON[type] === 'function') {
    return new BABYLON[type](
      name || defaultName,
      newVector(location.x, location.y, location.z),
      scene,
    );
  }
};

export const getSceneLights = () => scene.lights;
