import { scene } from './initHelpers';
import { newMetaDataEntry, newTexture } from './utilityHelpers';
import { selectedMeshes } from './viewportHelpers';
import { singleMeshTNode, toDegrees } from '../helpers';
import { MESSAGE_TYPES } from '../constants';
import { reactProps as props } from '../Canvas';
import _ from 'lodash';
export const addToMaterial = inboundData => {
  const {
    payload
  } = inboundData;
  const {
    id,
    albedoTexture,
    metallicTexture,
    microSurfaceTexture,
    emissiveTexture,
    bumpTexture
  } = payload;
  const material = scene.getMaterialByName(id, true);
  if (material) {
    // Basic attribute textures.
    if (_.isString(albedoTexture)) material.albedoTexture = newTexture(albedoTexture);
    if (_.isString(metallicTexture)) material.metallicTexture = newTexture(metallicTexture);
    if (_.isString(microSurfaceTexture)) material.microSurfaceTexture = newTexture(microSurfaceTexture);
    if (_.isString(emissiveTexture)) material.emissiveTexture = newTexture(emissiveTexture);
    if (_.isString(bumpTexture)) material.bumpTexture = newTexture(bumpTexture);
  }
};
export const addToMesh = inboundData => {
  const {
    payload
  } = inboundData;
  const {
    id
  } = payload;
  if (_.isEmpty(selectedMeshes) || selectedMeshes.length > 1) {
    return;
  }
  const mesh = selectedMeshes[0];
  if (mesh) {
    // Custom user mesh positions.
    const duplicate = scene.metadata?.userMeshPositions?.find(meshPos => meshPos.meshId === mesh.id && meshPos.positionId === id);
    if (_.isString(id) && !duplicate) {
      const {
        position,
        rotation,
        scaling
      } = singleMeshTNode;
      const [tx, ty, tz] = position.asArray();
      const [rx, ry, rz] = rotation.asArray().map(val => toDegrees(val * -1));
      const [sx, sy, sz] = scaling.asArray();
      const positionObj = {
        meshId: mesh.id,
        positionId: id,
        tx,
        ty,
        tz,
        rx,
        ry,
        rz,
        sx,
        sy,
        sz
      };
      const updatedPositions = [...(scene.metadata?.userMeshPositions || {}), positionObj];
      const selectedUserMeshPositions = scene.metadata.selectedUserMeshPositions;
      const filteredPositions = selectedUserMeshPositions.filter(pos => pos.meshId !== mesh.id);
      filteredPositions.push(positionObj);
      newMetaDataEntry('userMeshPositions', updatedPositions.filter(item => item));
      newMetaDataEntry('selectedUserMeshPositions', filteredPositions);
    }
  }
};
export const addToCamera = inboundData => {
  const {
    payload
  } = inboundData;
  const {
    id
  } = payload;
  const camera = scene.activeCamera;
  if (camera) {
    // Custom user camera views.
    const duplicate = scene.metadata?.userCameraViews.find(cameraPos => cameraPos.id === id);
    if (_.isString(id) && !duplicate) {
      const {
        position,
        rotation,
        target
      } = scene.activeCamera;
      const [tx, ty, tz] = [position.x, position.y, position.z];
      const [rx, ry, rz] = [rotation.x, rotation.y, rotation.z];
      const [tarX, tarY, tarZ] = [target.x, target.y, target.z];
      const updatedPositions = [...(scene.metadata?.userCameraViews || []), {
        id,
        tx,
        ty,
        tz,
        rx,
        ry,
        rz,
        tarX,
        tarY,
        tarZ
      }];
      newMetaDataEntry('userCameraViews', updatedPositions.filter(item => item));
      newMetaDataEntry('selectedUserCameraView', {
        id,
        tx,
        ty,
        tz,
        rx,
        ry,
        rz,
        tarX,
        tarY,
        tarZ
      });
    }
  }
};
export const addToEnvironment = inboundData => {
  const {
    payload
  } = inboundData;
  const {
    id,
    url
  } = payload;
  const skyBox = scene.getMaterialById('skyBox', true);
  if (skyBox) {
    // Add custom user environment.
    const duplicate = scene.metadata?.userEnvironments.find(env => env.id === id);
    const isNotDefault = url !== scene.metadata.defaultEnvironment;
    const isValid = isNotDefault && _.isString(id) && _.isString(url) && !duplicate;
    if (isValid) {
      const message = 'Adding custom environment...';
      const clearExisting = true;
      const isUserFile = true;
      const config = {
        type: MESSAGE_TYPES.loading,
        showModal: true,
        closeButton: false
      };
      props.addNotification?.([{
        message,
        config
      }], clearExisting);
      newTexture(url, isUserFile);
      const updatedEnvironments = [...(scene.metadata?.userEnvironments || {}), {
        id,
        url
      }];
      newMetaDataEntry('userEnvironments', updatedEnvironments.filter(item => item));
    }
  }
};
export const addToViewport = () => {
  // Add functions here...
};