package com.njcn.advance.utils;

import com.njcn.common.utils.PubUtils;

/**
 * 用于处理暂降治理针对各方案下的雷达信息处理的工具类
 */
public class RadarUtil {

    private static final double[] w = {0.565, 0.2622, 0.1175, 0.0553};
    private static final int[][] a = {{1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 0, 1, 0}, {0, 0, 0, 0, 1}};

    /**
     * 获取治理前后的风险评估等级
     *
     * @param lossAverage 年评价损失金额
     * @param coefficient 系数
     */
    public static String getRadarRiskLevel(double lossAverage, double coefficient) {
        double n = 0d;
        double temp = 0d;
        double[] b = getB(lossAverage, coefficient);

        for (int[] ints : a) {
            int j = 0;
            for (; j < b.length; j++) {
                temp = temp + Math.abs(ints[j] - b[j]);
            }
            temp = 1 - temp / j;
            if (temp > n) {
                n = temp;
            }
        }
        return getLevelName(1 - n);
    }

    /**
     * 获取去风险能力
     *
     * @param lossAverage 年评价损失金额
     * @param coefficient 系数
     */
    public static double getRadarRisk(double lossAverage, double coefficient) {
        double n = 0d;
        double temp = 0d;
        double[] b = getB(lossAverage, coefficient);

        for (int[] ints : a) {
            int j = 0;
            for (; j < b.length; j++) {
                temp = temp + Math.abs(ints[j] - b[j]);
            }
            temp = 1 - temp / j;
            if (temp > n) {
                n = temp;
            }
        }
        return n * 0.9;
    }


    private static double[] getB(double x, double y) {
        double[] b = {0, 0, 0, 0, 0};
        double[] l = getLossDegree(x);
        double[] o2 = getOther3Degree(y);
        double[] o3 = o2;
        double[] o4 = o2;
        for (int i = 0; i < b.length; i++) {
            b[i] = w[0] * l[i]
                    + w[1] * o2[i]
                    + w[2] * o3[i]
                    + w[3] * o4[i];
        }
        return b;
    }

    private static double[] getLossDegree(double x) {
        double[] r = {0, 0, 0, 0, 0};
        //0A1
        if (x < 10) {
            r[0] = 1;
        } else if (x >= 10 && x < 25) {
            r[0] = (25 - x) / (25 - 10);
        }
        //1B1
        if (x >= 5 && x < 20) {
            r[1] = (x - 5) / (20 - 5);
        } else if (x >= 20 && x < 30) {
            r[1] = 1;
        } else if (x >= 30 && x < 45) {
            r[1] = (45 - x) / (45 - 30);
        }
        //2C1
        if (x >= 25 && x < 40) {
            r[2] = (x - 25) / (40 - 25);
        } else if (x >= 40 && x < 50) {
            r[2] = 1;
        } else if (x >= 50 && x < 65) {
            r[2] = (65 - x) / (65 - 50);
        }
        //3D1
        if (x >= 45 && x < 60) {
            r[3] = (x - 45) / (60 - 45);
        } else if (x >= 60 && x < 70) {
            r[3] = 1;
        } else if (x >= 70 && x < 85) {
            r[3] = (85 - x) / (85 - 70);
        }
        //4E1
        if (x >= 65 && x < 80) {
            r[4] = (x - 65) / (80 - 65);
        } else if (x >= 80) {
            r[4] = 1;
        }
        return r;
    }


    private static double[] getOther3Degree(double y) {
        double[] r = {0, 0, 0, 0, 0};
        //0A2
        if (y < 11) {
            r[0] = 1;
        } else if (y >= 1 && y < 2.5) {
            r[0] = (2.5 - y) / (2.5 - 1.0);
        }

        //1B1
        if (y >= 0.5 && y < 2.0) {
            r[1] = (y - 0.5) / (2.0 - 0.5);
        } else if (y >= 2.0 && y < 3.0) {
            r[1] = 1;
        } else if (y >= 3.0 && y < 4.5) {
            r[1] = (4.5 - y) / (4.5 - 3.0);
        }
        //2C1
        if (y >= 2.5 && y < 4.0) {
            r[2] = (y - 2.5) / (4.0 - 2.5);
        } else if (y >= 4.0 && y < 5.0) {
            r[2] = 1;
        } else if (y >= 5.0 && y < 6.5) {
            r[2] = (6.5 - y) / (6.5 - 5.0);
        }
        //3D1
        if (y >= 4.5 && y < 6.0) {
            r[3] = (y - 4.5) / (6.0 - 4.5);
        } else if (y >= 6.0 && y < 7.0) {
            r[3] = 1;
        } else if (y >= 7.0 && y < 8.5) {
            r[3] = (8.5 - y) / (8.5 - 7.0);
        }
        //4E1
        if (y >= 6.5 && y < 8.0) {
            r[4] = (y - 6.5) / (8.0 - 6.5);
        } else if (y >= 8.0) {
            r[4] = 1;
        }
        return r;
    }


    private static String getLevelName(double level) {
        String[] levelName = {"一级", "二级", "三级", "四级", "五级"};
        if (level >= 0 && level < 0.2) {
            return levelName[0];
        } else if (level >= 0.2 && level < 0.4) {
            return levelName[1];
        } else if (level >= 0.4 && level < 0.6) {
            return levelName[2];
        } else if (level >= 0.6 && level < 0.8) {
            return levelName[3];
        } else if (level >= 0.8 && level <= 1.0) {
            return levelName[4];
        }
        return "异常了！";
    }

    /**
     * 根据治理损失求方案的治理效果
     *
     * @param yearLoss       治理前损失
     * @param governYearLoss 治理后损失
     */
    public static Double getRadarResult(Double yearLoss, Double governYearLoss) {
        // 年治理效果
        double loss = yearLoss - governYearLoss;
        if (loss == 0D || yearLoss == 0) {
            return 0D;
        } else {
            return PubUtils.doubleRound(2, loss / (Math.ceil(yearLoss / 100D) * 100));
        }
    }

}
