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

import cn.hutool.core.util.StrUtil;
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.*;

/**
 * @author xy
 * @version 1.0
 * @title HairDataFileUtil  毛发数据的文件解析工具类
 * @description
 * @create 2024/1/29 14:37
 */
@Slf4j
public class HairDataFileUtil {
    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个样本的该化合物含量数据
        HairSewageCompoundData hairSewageCompoundData =null;
        List<HairSewageCompoundData> hairSewageCompoundDataList =null;
        String compoundName="";
        List<String> fieldList = null;
        boolean clearTab = true;
        while ((currentLineContent = reader.readLine()) != null) {
            if(StrUtil.isBlank(currentLineContent)) continue;//如果该行是空行，直接跳到下一行
            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;
                    //hairSewageCompoundData=new HairSewageCompoundData();
                    hairSewageCompoundDataList = new ArrayList<>();
                    //hairSewageCompoundData.setCompoundName(compoundName);
                    //hairSewageCompoundDataList.add(hairSewageCompoundData);
                    //retMap.put(compoundName,hairSewageCompoundDataList);
                    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.setQuanTrace(arrayLine[5]);
                    hairSewageCompoundData.setArea(StrUtil.isBlank(arrayLine[6])?0:Double.parseDouble(arrayLine[6]));
                    hairSewageCompoundData.setTrace1(arrayLine[7]);
                    hairSewageCompoundData.setArea1(StrUtil.isBlank(arrayLine[8])?0:Double.parseDouble(arrayLine[8]));
                    hairSewageCompoundData.setRatioActual(StrUtil.isBlank(arrayLine[9])?0:Double.parseDouble(arrayLine[9]));
                    hairSewageCompoundData.setRatioPred(StrUtil.isBlank(arrayLine[10])?0:Double.parseDouble(arrayLine[10]));
                    hairSewageCompoundData.setRatioFlag(arrayLine[11]);
                    hairSewageCompoundData.setPrimaryFlags(arrayLine[13]);
                    hairSewageCompoundData.setConcentration(arrayLine[14]);

                    hairSewageCompoundDataList.add(hairSewageCompoundData);
                    retMap.put(compoundName, hairSewageCompoundDataList);
                }

            }
        }
        log.info("此次一共解析出{}种化合物，包含{}个检材样本",retMap.size(),sampleCount);
        return retMap;
    }
}
