package org.diagbot.bigdata.work;

import org.algorithm.core.AlgorithmExecutor;
import org.algorithm.factory.AlgorithmFactory;
import org.algorithm.util.AlgorithmClassify;
import org.diagbot.common.work.FeatureRate;
import org.diagbot.common.work.ResponseData;
import org.diagbot.common.work.SearchData;
import org.diagbot.nlp.feature.FeatureType;
import org.springframework.beans.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @ClassName org.diagbot.bigdata.work.AlgorithmCore
 * @Description TODO
 * @Author fyeman
 * @Date 2019/1/16/016 17:01
 * @Version 1.0
 **/
public class AlgorithmCore {
    public ResponseData algorithm(HttpServletRequest request, SearchData searchData) throws Exception {
        ResponseData responseData = new ResponseData();
        //对象拷贝至BigDataSearchData处理
        BigDataSearchData bigDataSearchData = new BigDataSearchData();
        BeanUtils.copyProperties(searchData, bigDataSearchData);
        //录入文本处理，包括提取特征、推送类型转换等
        ParamsDataProxy paramsDataProxy = new ParamsDataProxy();
        paramsDataProxy.createSearchData(request, bigDataSearchData);
        responseData.setInputs(bigDataSearchData.getInputs());
        //推送模型类型集合
        AlgorithmClassify[] classifies = bigDataSearchData.getAlgorithmClassify();
        String[] featureTypes = bigDataSearchData.getFeatureTypes();
        //推送结果处理
        ResultDataProxy resultDataProxy = new ResultDataProxy();
        for (int i = 0; i < classifies.length; i++) {
            if (classifies[i] == null) {
                continue;
            }
            //算法推理
            AlgorithmExecutor executor = AlgorithmFactory.getInstance(classifies[i]);
            Map<String, Float> featuresMap = null;
            if (executor != null) {
                featuresMap = executor.execute(bigDataSearchData.getInputs());;
            }
            List<Map.Entry<String, Float>> featuresOrderList = null;
            if (featuresMap == null) {
                featuresOrderList = new ArrayList<Map.Entry<String, Float>>();
            } else {
                //同义词转化
                featuresMap = resultDataProxy.synonymConvert(request, featuresMap);
                //大小类合并
                featuresMap = resultDataProxy.resultMerge(request, featuresMap);
                //按模型计算的概率排序
                featuresOrderList = new ArrayList<Map.Entry<String, Float>>(featuresMap.entrySet());
                Collections.sort(featuresOrderList, new Comparator<Map.Entry<String, Float>>() {
                    public int compare(Map.Entry<String, Float> o1, Map.Entry<String, Float> o2) {
                        if (o2.getValue() - o1.getValue() > 0) {
                            return 1;
                        } else if (o2.getValue() - o1.getValue() < 0) {
                            return -1;
                        } else {
                            return 0;
                        }
                    }
                });
            }
            List<FeatureRate> featureRates = resultDataProxy.proxy(request, bigDataSearchData, featuresOrderList, featureTypes[i]);


            switch (FeatureType.parse(featureTypes[i])) {
                case SYMPTOM:
                    responseData.setSymptom(featureRates);
                    break;
                case DIAG:
                    responseData.setDis(featureRates);
                    break;
                case VITAL:
                    responseData.setVitals(featureRates);
                    break;
                case LIS:
                    responseData.setLabs(featureRates);
                    break;
                case PACS:
                    responseData.setPacs(featureRates);
                    break;
                case HISTORY:
                    responseData.setHistory(featureRates);
                    break;
            }
        }
        return responseData;
    }
}
