import dicomParser from "dicom-parser";
import dcmjs from './dcmjs'

export const anonymization = function (file, config) {
  return new Promise(async (resolve, reject) => {
    try {
      const reader = new FileReader()
      let AnonymizeFixedList = config.AnonymizeFixedList
      let AnonymizeNotFixedList = config.AnonymizeNotFixedList
      let DicomStoreInfo = config.DicomStoreInfo
      reader.onload = async (event) => {
        let buffer = event.target.result
        let data = dicomParser.parseDicom(new Uint8Array(buffer))
        let pixelDataElement = data.elements.x7fe00010
        let dataset = dcmjs.data.DicomMessage.readFile(buffer)
        for (var i = 0; i < AnonymizeFixedList.length; i++) {
          let AnonymizeFixed = AnonymizeFixedList[i]
          if (!dataset.dict.hasOwnProperty(`${AnonymizeFixed.Group + AnonymizeFixed.Element}`)) continue
          if (dataset.dict[AnonymizeFixed.Group + AnonymizeFixed.Element]) {
            dataset.dict[AnonymizeFixed.Group + AnonymizeFixed.Element].Value[0] = AnonymizeFixed.ReplaceValue
          } else {
            dataset.dict[AnonymizeFixed.Group + AnonymizeFixed.Element] = {
              vr: AnonymizeFixed.ValueRepresentation,
              Value: [
                AnonymizeFixed.ReplaceValue
              ]
            }
          }
        }
        for (var i = 0; i < AnonymizeNotFixedList.length; i++) {
          let AnonymizeNotFixed = AnonymizeNotFixedList[i]
          // if (!dataset.dict.hasOwnProperty(`${AnonymizeNotFixed.Group + AnonymizeNotFixed.Element}`)) continue
          if (AnonymizeNotFixed.Group + AnonymizeNotFixed.Element === '00100020') {
            console.log(`${DicomStoreInfo.TrialCode}_${DicomStoreInfo.SubjectCode}`)
            if (dataset.dict[AnonymizeNotFixed.Group + AnonymizeNotFixed.Element]) {
              dataset.dict[AnonymizeNotFixed.Group + AnonymizeNotFixed.Element].Value[0] = `${DicomStoreInfo.TrialCode}-${DicomStoreInfo.SubjectCode}`
            } else {
              dataset.dict[AnonymizeNotFixed.Group + AnonymizeNotFixed.Element] = {
                vr: AnonymizeNotFixed.ValueRepresentation,
                Value: [
                  `${DicomStoreInfo.TrialCode}_${DicomStoreInfo.SubjectCode}`
                ]
              }
            }
          } else {
            if (dataset.dict[AnonymizeNotFixed.Group + AnonymizeNotFixed.Element]) {
              dataset.dict[AnonymizeNotFixed.Group + AnonymizeNotFixed.Element].Value[0] = DicomStoreInfo[AnonymizeNotFixed.ReplaceValue] || Number(DicomStoreInfo[AnonymizeNotFixed.ReplaceValue]) === 0 ? DicomStoreInfo[AnonymizeNotFixed.ReplaceValue].toString() : ''
            } else {
              dataset.dict[AnonymizeNotFixed.Group + AnonymizeNotFixed.Element] = {
                vr: AnonymizeNotFixed.ValueRepresentation,
                Value: [
                  DicomStoreInfo[AnonymizeNotFixed.ReplaceValue]
                ]
              }
            }
          }
        }

        try {
          let newDicomFile = dataset.write() // fragmentMultiframe 原始数据是否进行分割
          const bufferArray = new Uint8Array(newDicomFile)
          const blob = new Blob([bufferArray], { type: 'application/octet-stream' })
          resolve({ blob, pixelDataElement })
        } catch (err) {
          console.log(file, 'warning')
          console.log(err)
          resolve(false);
        }
      };
      reader.readAsArrayBuffer(file);
    } catch (e) {
      console.log(file, 'warning')
      console.log(e)
      reject(e)
    }
  })
}
