import { UPDATABLE_ITEMS } from '../constants';
import {
  updateMaterial,
  addToMaterial,
  removeFromMaterial,
  updateMesh,
  addToMesh,
  removeFromMesh,
  updateCamera,
  addToCamera,
  removeFromCamera,
  updateEnvironment,
  addToEnvironment,
  removeFromEnvironment,
  updateViewport,
  addToViewport,
  removeFromViewport,
  serializeScene,
  updatePublish,
  newMetaDataEntry,
  buildMeshIdArray,
  updateLighting,
  requestRender,
} from '../helpers';
import { reactProps as props } from '../Canvas';

export const updateItem = inboundData => {
  const { type, method } = inboundData;
  const types = Object.keys(UPDATABLE_ITEMS);
  const isValidType = types.includes(type);
  const hidden = true;

  if (isValidType) {
    switch(type) {
      case UPDATABLE_ITEMS.material:
        if (method === 'update') updateMaterial(inboundData);
        if (method === 'add') addToMaterial(inboundData);
        if (method === 'remove') removeFromMaterial(inboundData);
        break;

      case UPDATABLE_ITEMS.mesh:
        if (method === 'update') updateMesh(inboundData);
        if (method === 'add') addToMesh(inboundData);
        if (method === 'remove') removeFromMesh(inboundData);
        break;

      case UPDATABLE_ITEMS.camera:
        if (method === 'update') updateCamera(inboundData);
        if (method === 'add') addToCamera(inboundData);
        if (method === 'remove') removeFromCamera(inboundData);
        break;

      case UPDATABLE_ITEMS.environment:
        if (method === 'update') updateEnvironment(inboundData);
        if (method === 'add') addToEnvironment(inboundData);
        if (method === 'remove') removeFromEnvironment(inboundData);
        break;

      case UPDATABLE_ITEMS.viewport:
        if (method === 'update') updateViewport(inboundData);
        if (method === 'add') addToViewport(inboundData);
        if (method === 'remove') removeFromViewport(inboundData);
        break;

      case UPDATABLE_ITEMS.publish:
        if (method === 'update') updatePublish(inboundData);
        break;

      case UPDATABLE_ITEMS.lighting:
        if (method === 'update') updateLighting(inboundData);
        break;

      default:
        break;
    }

    newMetaDataEntry('hiddenMeshes', buildMeshIdArray(hidden));
    props.setSerializedData?.(serializeScene());
    requestRender();
    props.clearUpdateData?.();
  }
};
