package iail.mdk.model.utils;

import java.util.HashMap;
import java.util.Map;


/**
 * @description:
 * @author: dzd 
 * @date: 2024/10/12 9:19
 **/
public class AlgsUtils {
    private HashMap<String, Object> model = new HashMap();
    private HashMap<String, Object> eval_pre = new HashMap();
    private HashMap<String, Object> train_result_models = new HashMap();

    public AlgsUtils() {
    }

    public HashMap<String, Object> createPredictHashmap(HashMap<String, Object> models) {
        if (models.containsKey("model")) {
            String aaa;
            if (((String) ((HashMap) models.get("model")).get("param1")).isEmpty()) {
                aaa = "error";
                this.model.put("param1", aaa);
            } else {
                aaa = (String) ((HashMap) models.get("model")).get("param1");
                this.model.put("param1", aaa);
            }
        } else {
            this.model = models;
        }

        return this.model;
    }

    public HashMap<String, Object> createPredictHashmapplus(HashMap<String, Object> models) {
        if (models.containsKey("models")) {
            String aaa;
            if (((String) ((HashMap) models.get("models")).get("paramFile")).isEmpty()) {
                aaa = "error";
                this.model.put("param1", aaa);
            } else {
                aaa = (String) ((HashMap) models.get("models")).get("paramFile");
                this.model.put("paramFile", aaa);
                if (((HashMap) models.get("models")).containsKey("dim")) {
                    Object dim = ((HashMap) models.get("models")).get("dim");
                    this.model.put("dim", dim);
                }
            }
        } else {
            this.model = models;
        }

        return this.model;
    }

    public HashMap<String, Object> reverseModels(HashMap<String, Object> train_result) {
        if (train_result.containsKey("models")) {
            this.train_result_models = (HashMap) train_result.get("models");
            if (((HashMap) train_result.get("models")).containsKey("dim")) {
                double dim = Double.parseDouble((String) ((HashMap) train_result.get("models")).get("dim"));
                this.train_result_models.put("dim", dim);
            }

            train_result.put("models", this.train_result_models);
        }

        return train_result;
    }

    public int[] getColAndRow(double[][] arr) {
        int row = arr.length;
        int col = arr[0].length;
        int[] result = new int[]{row, col};
        return result;
    }

    public double[][] getMathergeArr(double[][] data, double[][] refs) {
        int[] dataRowAndCol = this.getColAndRow(data);
        int rowData = dataRowAndCol[0];
        int colData = dataRowAndCol[1];
        int[] refsRowAndCol = this.getColAndRow(refs);
        int rowrefs = refsRowAndCol[0];
        int colrefs = refsRowAndCol[1];
        double[][] newData = new double[rowData + rowrefs][colData];

        int i;
        int j;
        for (i = 0; i < rowData; ++i) {
            for (j = 0; j < colData; ++j) {
                newData[i][j] = data[i][j];
            }
        }

        for (i = 0; i < rowrefs; ++i) {
            for (j = 0; j < colrefs; ++j) {
                newData[i + rowData][j] = refs[i][j];
            }
        }

        return newData;
    }

    public HashMap<String, Object> reverseResult(HashMap<String, Object> result) {
        if (null == result) {
            return result;
        }
        String code = reverseResultCode(result);
        if (!"100".equals(code)) {
            return result;
        }
		reverseResultValues(result);
        reverseTest(result);
        reverseEval(result);
        reverseOptdParams(result);
        return result;
    }

	private void reverseResultValues(HashMap<String, Object> result) {
        // 将result中的Double数组转为double数组
        if (result.containsKey("result")) {
            HashMap<String, Object> resultValues = (HashMap) result.get("result");
            for (Map.Entry<String, Object> entry : resultValues.entrySet()) {
                if (entry.getValue() instanceof Double[]) {
                    //一维数组
                    Double[] value = (Double[]) entry.getValue();
                    double[] value1 = new double[value.length];
                    for (int i = 0; i < value.length; i++) {
                        Double d = value[i];
                        if (Double.isNaN(d)) {
                            value1[i] = new Double(0.0).doubleValue();
                        } else {
                            value1[i] = d.doubleValue();
                        }
                    }
                    resultValues.put(entry.getKey(),value1);
                }

                if (entry.getValue() instanceof Double[][]) {
                    //二维数组
                    Double[][] value = (Double[][]) entry.getValue();
                    double[][] value1 = new double[value.length][];
                    for (int i = 0; i < value.length; i++) {
                        value1[i] = new double[value[i].length];
                        for (int j = 0; j < value[i].length; j++) {
                            Double d = value[i][j];
                            if (Double.isNaN(d)) {
                                value1[i][j] = new Double(0.0).doubleValue();
                            } else {
                                value1[i][j] = d.doubleValue();
                            }
                        }
                    }
                    resultValues.put(entry.getKey(),value1);
                }
            }
        }
    }


    private String reverseResultCode(HashMap<String, Object> result) {
        String code = result.containsKey("status_code") ? String.valueOf(result.get("status_code")) : "400";
        result.put("status_code", code);
        return code;
    }

    private void reverseTest(HashMap<String, Object> result) {
        if (result.containsKey("test")) {
            HashMap<String, Object> test = (HashMap) result.get("test");
            Double[][] realValue = (Double[][]) test.get("realValue");
            Double[][] predictValue = (Double[][]) test.get("predictValue");
            double[][] realValue1 = new double[realValue.length][2];
            double[][] predictValue1 = new double[predictValue.length][2];
            for (int i = 0; i < realValue.length; i++) {
                for (int j = 0; j < realValue[i].length; j++) {
                    Double d = (Double) realValue[i][j];
                    if (Double.isNaN(d)) {
                        realValue1[i][j] = new Double(0.0).doubleValue();
                    } else {
                        realValue1[i][j] = d.doubleValue();
                    }

                }
            }
            for (int i = 0; i < predictValue.length; i++) {
                for (int j = 0; j < predictValue[i].length; j++) {
                    Double d = (Double) predictValue[i][j];
                    if (Double.isNaN(d)) {
                        predictValue1[i][j] = new Double(0.0).doubleValue();
                    } else {
                        predictValue1[i][j] = d.doubleValue();
                    }

                }
            }
            HashMap<String, double[][]> map = new HashMap<>();
            map.put("realValue", realValue1);
            map.put("predictValue", predictValue1);

            result.put("test", map);
        }
    }

    public void reverseEval(HashMap<String, Object> result) {
        if (result.containsKey("eval")) {
            HashMap<String,Object> eval = (HashMap<String,Object>) result.get("eval");
            HashMap<String, String> evalMap = new HashMap<>(eval.size());

            for (HashMap.Entry<String, Object> entry : eval.entrySet()) {
                evalMap.put(entry.getKey(),String.valueOf(entry.getValue()));
            }

            result.put("eval",evalMap);
        }
    }

    private void reverseOptdParams(HashMap<String, Object> result) {
        if (result.containsKey("paramsopt")) {
            HashMap<String,Object> params = (HashMap<String,Object>) result.get("paramsopt");
            HashMap<String, String> newParams = new HashMap<>(params.size());

            for (HashMap.Entry<String, Object> entry : params.entrySet()) {
                newParams.put(entry.getKey(),String.valueOf(entry.getValue()));
            }

            result.put("optdParams",newParams);
        }
    }
}