package org.springblade.common.utils;

public class UnitUtil {

    public static Double convEnergy(Double val, String unit) {
        if (unit == null || val == null) {
            return null;
        }
        String s = unit.toLowerCase();
        switch (s) {
            case "kwh":
                return val;
            case "mwh":
                return val * 1000;
            case "gwh":
                return val * 1000 * 1000;
            case "twh":
                return val * 1000 * 1000 * 1000;
            case "pwh":
                return val * 1000 * 1000 * 1000 * 1000;
        }
        throw new RuntimeException("invalid unit: " + unit);
    }

    public static Double convPower(Double val, String unit) {
        if (unit == null || val == null) {
            return null;
        }
        String s = unit.toLowerCase();
        if (s.endsWith("p")) {
            s = s.substring(0, s.length() - 1);
        }
        switch (s) {
            case "w":
                return val / 1000;
            case "kw":
                return val;
            case "mw":
                return val * 1000;
            case "gw":
                return val * 1000 * 1000;
            case "tw":
                return val * 1000 * 1000 * 1000;
            case "pw":
                return val * 1000 * 1000 * 1000 * 1000;
        }
        throw new RuntimeException("invalid unit: " + unit);
    }

    public static Double convVa(Double v, String unit) {
        if (unit == null || v == null) {
            return null;
        }
        String s = unit.toLowerCase();
        if (s.endsWith("w")) {
            return convPower(v, unit);
        }
        switch (s) {
            case "va":
                return v;
            case "kva":
                return v * 1000;
            case "mva":
                return v * 1000 * 1000;
            case "gva":
                return v * 1000 * 1000 * 1000;
        }
        throw new RuntimeException("invalid unit: " + unit);
    }

    public static Double convHz(Double fac, String facStr) {
        if (facStr == null || fac == null) {
            return null;
        }
        String s = facStr.toLowerCase();
        switch (s) {
            case "hz":
                return fac;
            case "khz":
                return fac * 1000;
            case "mhz":
                return fac * 1000 * 1000;
            case "ghz":
                return fac * 1000 * 1000 * 1000;
        }
        throw new RuntimeException("invalid unit: " + facStr);
    }
}
