import * as cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader'
function parseImageId(imageId) {
  // build a url by parsing out the url scheme and frame index from the imageId
  const firstColonIndex = imageId.indexOf(':');

  let url = imageId.substring(firstColonIndex + 1);
  const frameIndex = url.indexOf('frame=');

  let frame;

  if (frameIndex !== -1) {
    const frameStr = url.substr(frameIndex + 6);

    frame = parseInt(frameStr, 10);
    url = url.substr(0, frameIndex - 1);
  }

  return {
    scheme: imageId.substr(0, firstColonIndex),
    url,
    frame,
  };
}
function getNumberValues(dataSet, tag, minimumLength) {
  const values = [];
  const valueAsString = dataSet.string(tag);

  if (!valueAsString) {
    return;
  }
  const split = valueAsString.split('\\');

  if (minimumLength && split.length < minimumLength) {
    return;
  }
  for (let i = 0; i < split.length; i++) {
    values.push(parseFloat(split[i]));
  }

  return values;
}
function metaDataProvider(type, imageId) {
  const parsedImageId = parseImageId(imageId);
  const dataSet = cornerstoneWADOImageLoader.wadouri.dataSetCacheManager.get(parsedImageId.url);

  if (!dataSet) {
    return;
  }
  if (type === 'imagePlaneModule') {
    const imageOrientationPatient = getNumberValues(dataSet, 'x00200037', 6);
    const imagePositionPatient = getNumberValues(dataSet, 'x00200032', 3);
    const pixelSpacing = getNumberValues(dataSet, 'x00280030', 2);
    const imagePixelSpacing = getNumberValues(dataSet, 'x00181164', 2);
    const estimatedRadiographicMagnificationFactor = getNumberValues(dataSet, 'x00181114', 2);
    let columnPixelSpacing = null;

    let rowPixelSpacing = null;

    if (pixelSpacing) {
      rowPixelSpacing = pixelSpacing[0];
      columnPixelSpacing = pixelSpacing[1];
    } else if (imagePixelSpacing && estimatedRadiographicMagnificationFactor) {
      rowPixelSpacing = imagePixelSpacing[0] / estimatedRadiographicMagnificationFactor[0];
      columnPixelSpacing = imagePixelSpacing[1] / estimatedRadiographicMagnificationFactor[1];
    } else if (imagePixelSpacing && !estimatedRadiographicMagnificationFactor) {
      rowPixelSpacing = imagePixelSpacing[0];
      columnPixelSpacing = imagePixelSpacing[1];
    }

    let rowCosines = null;

    let columnCosines = null;

    if (imageOrientationPatient) {
      rowCosines = [
        parseFloat(imageOrientationPatient[0]),
        parseFloat(imageOrientationPatient[1]),
        parseFloat(imageOrientationPatient[2]),
      ];
      columnCosines = [
        parseFloat(imageOrientationPatient[3]),
        parseFloat(imageOrientationPatient[4]),
        parseFloat(imageOrientationPatient[5]),
      ];
    }

    return {
      frameOfReferenceUID: dataSet.string('x00200052'),
      rows: dataSet.uint16('x00280010'),
      columns: dataSet.uint16('x00280011'),
      imageOrientationPatient,
      rowCosines,
      columnCosines,
      imagePositionPatient,
      sliceThickness: dataSet.floatString('x00180050'),
      sliceLocation: dataSet.floatString('x00201041'),
      pixelSpacing,
      rowPixelSpacing,
      columnPixelSpacing,
    };
  }
}
export default metaDataProvider;