package com.m2pool.pool.utils;

import com.m2pool.common.core.utils.StringUtils;
import com.m2pool.pool.enums.PoolProfitScale;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @Description 算力转换
 * @Date 2024/6/13 10:54
 * @Author dy
 */
public class PowerUnitUtils {

    /**
     * 算力数值转换 1000转 1KH/s 1000*1000转1MH/s 1000*1000*1000转1GH/s 1000*1000*1000*10转10TH/s
     * @param value
     * @return
     */
    public static String powerFormat(BigDecimal value, String coin){

        if(StringUtils.isNull(value)){
            return "0 H/s";
        }

        int scale = PoolProfitScale.getScaleByCoin(coin);
        //ps BigDecimal最大上限少于1000*1000*1000*1000*10 所以需要中间值 gValue
        BigDecimal gValue = value.divide(BigDecimal.valueOf(1000000000), scale, BigDecimal.ROUND_HALF_UP);
        if (gValue.compareTo(BigDecimal.valueOf(1000*1000*1000)) >= 0){
            //1000*1000*1000G以上 转换为E
            return gValue.divide(BigDecimal.valueOf(1000000000),scale,BigDecimal.ROUND_HALF_UP).toEngineeringString() + " EH/s";
        }
        else if (gValue.compareTo(BigDecimal.valueOf(1000*1000)) >= 0){
            //1000*1000G以上 1000*1000*1000G以下 转换为P
            return gValue.divide(BigDecimal.valueOf(1000000),scale,BigDecimal.ROUND_HALF_UP).toEngineeringString() + " PH/s";
        }
        else if (gValue.compareTo(BigDecimal.valueOf(10000)) >= 0){
            //10000G 以上 1000*1000G一下 则转为T 即10T以上才转换
            return gValue.divide(BigDecimal.valueOf(1000),scale,BigDecimal.ROUND_HALF_UP).toEngineeringString() + " TH/s";
        }
        else if (value.compareTo(BigDecimal.valueOf(1000*1000*1000)) >= 0){
            //1G 1G以上 10T以下 转换为G
            return gValue.setScale(scale,BigDecimal.ROUND_HALF_UP).toString() + " GH/s";
        }
        else if (value.compareTo(BigDecimal.valueOf(1000*1000)) >= 0){
            //1M 1M以上 1G一下则转换为M
            return value.divide(BigDecimal.valueOf(1000*1000), scale,BigDecimal.ROUND_HALF_UP).toEngineeringString() + " MH/s";
        }
        else if (value.compareTo(BigDecimal.valueOf(1000)) >= 0){
            //1K 1K以上 1M一下则转换为K
            return value.divide(BigDecimal.valueOf(1000), scale,BigDecimal.ROUND_HALF_UP).toEngineeringString() + " KH/s";
        }
        else {
            return value.setScale(scale,BigDecimal.ROUND_HALF_UP).toString() + " H/s";
        }
    }

    /**
     * 难度数值转换 1000转 1K 1000*1000转1M 1000*1000*1000转1G
     * @param value
     * @return
     */
    public static String difficultyFormat(BigDecimal value){

        if(StringUtils.isNull(value)){
            return "0";
        }
        if(value.compareTo(BigDecimal.valueOf(1000L * 1000L * 1000L * 1000L * 1000L)) >= 0){
            return value.divide(BigDecimal.valueOf(1000L * 1000L * 1000L * 1000L * 1000L),4,BigDecimal.ROUND_HALF_UP).toEngineeringString()+"P";
        }else if(value.compareTo(BigDecimal.valueOf(1000L * 1000L * 1000L * 1000L)) >= 0){
            return value.divide(BigDecimal.valueOf(1000L * 1000L * 1000L * 1000L),4,BigDecimal.ROUND_HALF_UP).toEngineeringString()+"T";
        } else if (value.compareTo(BigDecimal.valueOf(1000*1000*1000)) >= 0){
            return value.divide(BigDecimal.valueOf(1000*1000*1000),2,BigDecimal.ROUND_HALF_UP).toEngineeringString()+"G";
        }else if (value.compareTo(BigDecimal.valueOf(1000*1000)) >= 0){
            return value.divide(BigDecimal.valueOf(1000*1000),2,BigDecimal.ROUND_HALF_UP).toEngineeringString()+"M";
        }else if (value.compareTo(BigDecimal.valueOf(1000)) >= 0){
            return value.divide(BigDecimal.valueOf(1000),2,BigDecimal.ROUND_HALF_UP).toEngineeringString()+"K";
        }else {
            return value.setScale(2,BigDecimal.ROUND_HALF_UP).toEngineeringString();
        }
    }

    /**
     * 获取某个算力值 对应应该换算的单位 和单位符号值
     * @param value
     * @return
     */
    public static NetPowerUnit getPowerUnit(BigDecimal value){
        NetPowerUnit unit = new NetPowerUnit();
        //默认1K 以下
        unit.setUnit("H/s");
        unit.setUnitValue(BigDecimal.valueOf(1));
        if (value != null){
            //ps BigDecimal最大上限少于1000*1000*1000*1000*10 所以需要中间值 gValue
            BigDecimal gValue = value.divide(BigDecimal.valueOf(1000000000), 2, RoundingMode.HALF_UP);
            if (gValue.compareTo(BigDecimal.valueOf(1000L *1000*1000*10)) >= 0){
                //10E
                unit.setUnit("EH/s");
                unit.setUnitValue(BigDecimal.valueOf(1000L*1000*1000*1000*1000*1000));
            }
            else if (gValue.compareTo(BigDecimal.valueOf(1000*1000*10)) >= 0){
                //10P -10E

                unit.setUnit("PH/s");
                unit.setUnitValue(BigDecimal.valueOf(1000L*1000*1000*1000*1000));
            }
            else if (gValue.compareTo(BigDecimal.valueOf(1000 * 10)) >= 0){
                //10 T -  10P
                unit.setUnit("TH/s");
                unit.setUnitValue(BigDecimal.valueOf(1000L*1000*1000*1000));
            }
            else if (value.compareTo(BigDecimal.valueOf(1000L *1000*1000 * 10)) >= 0){
                //10G - 10T
                unit.setUnit("GH/s");
                unit.setUnitValue(BigDecimal.valueOf(1000L*1000*1000));
            }
            else if (value.compareTo(BigDecimal.valueOf(1000*1000*10)) >= 0){
                //10M - 10G
                unit.setUnit("MH/s");
                unit.setUnitValue(BigDecimal.valueOf(1000L*1000));
            }
            else if (value.compareTo(BigDecimal.valueOf(1000 * 10)) >= 0){
                //10K-10M
                unit.setUnit("KH/s");
                unit.setUnitValue(BigDecimal.valueOf(1000));
            }
        }


        return unit;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class NetPowerUnit{
        private String unit;
        private BigDecimal unitValue;
    }

}
