package digital.laboratory.platform.inspection.utils.datafile;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.sql.visitor.functions.If;
import digital.laboratory.platform.inspection.utils.datafile.hair.HairSewageCompoundData;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xy
 * @version 1.0
 * @title HairDataFileUtil  毛发数据的文件解析工具类
 * @description
 * @create 2024/1/29 14:37
 */
@Slf4j
public class SewageDataFileUtil {
    public static Map<String, List<HairSewageCompoundData>> readDataFromText(String dataText) throws Exception {
        ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(dataText.getBytes());
        BufferedReader reader = new BufferedReader(new InputStreamReader(tInputStringStream));
        String currentLineContent = "";//当前行
        int sampleCount = 0;
        Map<String, List<HairSewageCompoundData>> retMap = new HashMap<>();//某种化合物的 10个样本的该化合物含量数据

        String compoundName = "";
        List<String> fieldList = null;
        boolean clearTab = true;
        ArrayList<HairSewageCompoundData> sewageCompoundDataList = new ArrayList<>();
        while ((currentLineContent = reader.readLine()) != null) {
            //如果该行是空行，直接跳到下一行
            if (StrUtil.isBlank(currentLineContent)) {
                continue;
            }
            String[] arrayLine = currentLineContent.split("\t", -1);
            if (arrayLine[0].equals("Index")) {
                fieldList = Arrays.asList(arrayLine);
                continue;
            }
            if (arrayLine[4].endsWith("2")) {
                continue;
            }
            List<HairSewageCompoundData> hairSewageCompoundDataList = new ArrayList<>();

            HairSewageCompoundData hairSewageCompoundData = new HairSewageCompoundData();
            hairSewageCompoundData.setCompoundName(arrayLine[3]);
            hairSewageCompoundData.setFieldList(fieldList); // 表头
            hairSewageCompoundData.setIndexNo(Integer.parseInt(arrayLine[0]));//
            hairSewageCompoundData.setSampleName(arrayLine[1]);
            hairSewageCompoundData.setIdName(arrayLine[1]);
            try {
                hairSewageCompoundData.setRtTime(StrUtil.isBlank(arrayLine[11]) ? 0 : Double.parseDouble(arrayLine[11])); // 保留时间
            } catch (Exception e) {
                hairSewageCompoundData.setRtTime(0.0); // 保留时间
            }
            try {
                hairSewageCompoundData.setArea1(StrUtil.isBlank(arrayLine[10]) ? 0 : Double.parseDouble(arrayLine[10]));
            } catch (Exception e) {
                hairSewageCompoundData.setArea1(0.0);
            }
            try {
                hairSewageCompoundData.setArea(StrUtil.isBlank(arrayLine[9]) ? 0 : Double.parseDouble(arrayLine[9]));
            } catch (Exception e) {
                hairSewageCompoundData.setArea(0.0);
            }
            try {
                hairSewageCompoundData.setRatioActual(StrUtil.isBlank(arrayLine[15]) ? 0 : Double.parseDouble(arrayLine[15]));
            } catch (Exception e) {
                hairSewageCompoundData.setRatioActual(0.0);
            }
            hairSewageCompoundData.setRatioPred(0.0);
            try {
                // 浓度的数组下标是13
                hairSewageCompoundData.setConcentration(String.valueOf(Double.parseDouble(arrayLine[13])));
            } catch (Exception e) {
                hairSewageCompoundData.setConcentration("0");
            }
            hairSewageCompoundData.setTrace1("");
            hairSewageCompoundData.setQuanTrace("");
            hairSewageCompoundData.setPrimaryFlags("");
            hairSewageCompoundData.setRatioFlag("");
            hairSewageCompoundDataList.add(hairSewageCompoundData);
            sewageCompoundDataList.addAll(hairSewageCompoundDataList);
//            if (retMap.containsKey(hairSewageCompoundData.getCompoundName())) {
//                List<HairSewageCompoundData> sewageCompoundDataList = retMap.get(hairSewageCompoundData.getCompoundName());
//                sewageCompoundDataList.add(hairSewageCompoundData);
//                retMap.put(hairSewageCompoundData.getCompoundName(), sewageCompoundDataList);
//            }else {
//                retMap.put(hairSewageCompoundData.getCompoundName(), hairSewageCompoundDataList);
//            }
//            String[] arrayLineTemp = currentLineContent.split("\t",-1);
//            String[] arrayLine = null;
//            if (clearTab || arrayLineTemp[0].startsWith("Compound")) {
//                arrayLine = Arrays.stream(arrayLineTemp).filter(s -> StrUtil.isNotBlank(s)).toArray(String[]::new);
//            } else {
//                arrayLine = arrayLineTemp;
//            }
//            if(arrayLine.length==1){
//                //说明这一行就是一个标题
//                //arrayLine[0].indexOf("Compound")!=-1&&arrayLine[0].indexOf(":")!=-1
//                if(arrayLine[0].startsWith("Compound")){
//                    //如果条件成立，就是化合物名称
//                    compoundName=arrayLine[0].split(":")[1].trim();
//                    // 标识后面的不用在去清除制表符\t
//                    clearTab = false;
//                    hairSewageCompoundDataList = new ArrayList<>();
//
//                    sampleCount=0;
//                }
//            }else if(arrayLine.length>1){
//                //说明现在是某种化合物数据了
//                if(arrayLine[2].equals("Name")){
//                    //说明是表头行
//                    fieldList= Arrays.asList(arrayLine);
//                    //hairSewageCompoundData.setFieldList(fieldList);
//                }else{
//                    sampleCount++;
//                    //说明是数据行
//                    hairSewageCompoundData =new HairSewageCompoundData();
//
//                    hairSewageCompoundData.setCompoundName(compoundName);
//                    hairSewageCompoundData.setFieldList(fieldList); // 表头
//                    hairSewageCompoundData.setIndexNo(Integer.parseInt(arrayLine[1]));//
//                    hairSewageCompoundData.setSampleName(arrayLine[2]);
//                    hairSewageCompoundData.setIdName(arrayLine[3]);
//                    hairSewageCompoundData.setRtTime(StrUtil.isBlank(arrayLine[4])?0:Double.parseDouble(arrayLine[4])); // 保留时间
//                    hairSewageCompoundData.setArea1(StrUtil.isBlank(arrayLine[5])?0:Double.parseDouble(arrayLine[5]));
//                    hairSewageCompoundData.setArea(StrUtil.isBlank(arrayLine[6])?0:Double.parseDouble(arrayLine[6]));
//                    hairSewageCompoundData.setRatioActual(StrUtil.isBlank(arrayLine[7])?0:Double.parseDouble(arrayLine[7]));
//                    hairSewageCompoundData.setRatioPred(StrUtil.isBlank(arrayLine[8])?0:Double.parseDouble(arrayLine[8]));
//                    hairSewageCompoundData.setConcentration(arrayLine[10]); // 溶液浓度
//                    hairSewageCompoundData.setTrace1(arrayLine[13]);
//                    hairSewageCompoundData.setQuanTrace(arrayLine[14]);
//                    hairSewageCompoundData.setPrimaryFlags(arrayLine[15]);
//                    hairSewageCompoundData.setRatioFlag(arrayLine[16]);
//
//                    hairSewageCompoundDataList.add(hairSewageCompoundData);
//                    retMap.put(compoundName, hairSewageCompoundDataList);
//                }
//
//            }
        }
        Map<String, List<HairSewageCompoundData>> map = updateInfo(sewageCompoundDataList);
        log.info("此次一共解析出{}种化合物，包含{}个检材样本", map.size(), sampleCount);
        return map;
    }

    public static Map<String, List<HairSewageCompoundData>> updateInfo(List<HairSewageCompoundData> hairSewageCompoundDataList) {
        Map<String, List<HairSewageCompoundData>> map = hairSewageCompoundDataList.stream().collect(Collectors.groupingBy(item -> item.getCompoundName()));
        return map;
    }

}
