package com.pretreatment.utils;

import com.pretreatment.domin.DicomObj;
import com.pretreatment.domin.Image;
import com.pretreatment.domin.Series;
import com.pretreatment.domin.Study;
import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;
import org.dcm4che3.data.VR;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: Created by Eric
 * @Date: 2017/10/26
 * @Description: 解析Dicom文件工具类
 */
public class PrecessDcmUtils {

    private Logger logger = LoggerFactory.getLogger(Study.class);

    public List<study> precessDicom(String path) {
        TraverseFolder traverseFolder = new TraverseFolder();
        List<string> fileList = traverseFolder.getAllDcm(new File(path));
        File file;
        List<study> studies = new LinkedList<study>();
        Study study;
        Series s;
        DicomObj obj;
        Image image;
        for (int i = 0; i < fileList.size(); i++) {
            List<series> series = new LinkedList<series>();
            List<dicomobj> objs = new LinkedList<dicomobj>();
            List<image> images = new LinkedList<image>();
            study = new Study();
            s = new Series();
            obj = new DicomObj();
            image = new Image();
            file = new File(fileList.get(i)); //	System.out.println(fileList.get(i)); DisplayTag d = new DisplayTag(file); 
            @SuppressWarnings("static-access") Attributes attr = null;
            try {
                attr = d.loadDicomObject(file);
                attr.setString(Tag.SpecificCharacterSet, VR.CS, "GB18030");
                study.setPatientName(attr.getString(Tag.PatientName));
                study.setPatientID(attr.getString(Tag.PatientID));
                study.setPatientSex(attr.getString(Tag.PatientSex));
                study.setPatientAge(attr.getString(Tag.PatientAge));
                study.setStudyInstanceUID(attr.getString(Tag.StudyInstanceUID));
                study.setStudyDate(attr.getString(Tag.StudyDate));
                study.setStudyID(attr.getString(Tag.StudyID));
                s.setSeriesInstanceUID(attr.getString(Tag.SeriesInstanceUID));
                s.setSeriesDate(attr.getString(Tag.SeriesDate));
                s.setSeriesNumber(attr.getString(Tag.SeriesNumber));
                s.setModality(attr.getString(Tag.Modality));
                s.setInstitutionName(attr.getString(Tag.InstitutionName));
                obj.setSopInstanceUID(attr.getString(Tag.SOPInstanceUID));
                obj.setImageType(attr.getString(Tag.ImageType));
                obj.setTransferSyntaxUID(attr.getString(Tag.TransferSyntaxUID));
                obj.setInstanceNumber(attr.getString(Tag.InstanceNumber));
                obj.setPhotometricInterpretation(attr.getString(Tag.PhotometricInterpretation));
                obj.setSamplesPerPixel(attr.getString(Tag.SamplesPerPixel));
                obj.setPixelRepresentation(attr.getString(Tag.PixelRepresentation));
                obj.setColumns(attr.getString(Tag.Columns));
                obj.setRows(attr.getString(Tag.Rows));
                obj.setBitsAllocated(attr.getString(Tag.BitsAllocated));
                obj.setBitsStored(attr.getString(Tag.BitsStored));
                image.setPixelSpacing(attr.getString(Tag.PixelSpacing));
                image.setSliceLocation(attr.getString(Tag.SliceLocation));
                image.setSliceThickness(attr.getString(Tag.SliceThickness));
                image.setImagePositionPatient(attr.getString(Tag.ImagePositionPatient));
                image.setImageOrientation(attr.getString(Tag.ImageOrientation));
                series.add(s);
                objs.add(obj);
                images.add(image);
                study.setSeries(series);
                study.setDicomObjs(objs);
                study.setImages(images);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                boolean b = studies.add(study);
                if (b) {
                    logger.info("解析成功!" + studies);
                } else {
                    logger.error("请稍后再试!");
                }
            }
        }
        System.out.println("kkk" + studies);
        return studies;
    }
}