package com.brilliance.mda.runtime.mda.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Numerics {

    private static final Logger log = LoggerFactory.getLogger(Numerics.class);

    public static int toInt(BigDecimal numberic) {
        return numberic.intValue();
    }

    public static BigDecimal toBigDecimal(String val) {
        if (MdaUtils.isEmpty(val)) {
            return BigDecimal.ZERO;
        }
        return new BigDecimal(val);
    }

    public static int toInt(double numberic) {
        return Double.valueOf(numberic).intValue();
    }

    public static int toInt(long numberic) {
        return Long.valueOf(numberic).intValue();
    }

    public static int toInt(float numberic) {
        return Float.valueOf(numberic).intValue();
    }

    public static int toInt(String numberic) {
        return Double.valueOf(numberic).intValue();
    }

    public static double abs(double number) {
        return Math.abs(number);
    }

    public static BigDecimal abs(BigDecimal decimal) {
        return decimal.abs();
    }

    public static int abs(int number) {
        return Math.abs(number);
    }

    //需要转换代码时采用BigDecimal模式
    public static BigDecimal round(BigDecimal num, int n) {
        return num.setScale(n, RoundingMode.HALF_UP);
    }

    public static BigDecimal round(int num, int n) {
        return round(String.valueOf(num), n);
    }

    public static BigDecimal round(String num, int n) {
        return new BigDecimal(num).setScale(n, RoundingMode.HALF_UP);
    }

    public static BigDecimal round(double num, int n) {
        return round(String.valueOf(num), n);
    }

    public static int ceil(int d) {
        return (int) Math.ceil(d);
    }

    public static double ceil(double d) {
        return Math.ceil(d);
    }

    public static int ceil(BigDecimal d) {
        return (int) Math.ceil(d.doubleValue());
    }

    public static int floor(Object d) {
        if (chkNum(d)) {
            if (chkBig(d)) {
                return (int) Math.floor(((BigDecimal) d).doubleValue());
            }
            return (int) Math.floor(Double.valueOf(d.toString()));
        }
        return 0;
    }

    public static Boolean chkNum(Object o) {
        return o instanceof Integer || o instanceof Float || o instanceof Double || o instanceof Short || o instanceof Long || o instanceof BigDecimal;
    }

    public static Boolean chkBig(Object o) {
        return o instanceof BigDecimal;
    }

    public static int sgn(Object n) {
        if (chkNum(n)) {
            if (chkBig(n)) {
                BigDecimal temp = (BigDecimal) n;
                return temp.compareTo(BigDecimal.ZERO);
            } else {
                return BigDecimal.valueOf(Double.valueOf(n.toString())).compareTo(BigDecimal.ZERO);
            }
        }
        return 0;
    }

    //null
    public static BigDecimal time(int i) {
        String timeStr = new SimpleDateFormat("HH.mmssSSS").format(new Date());
        if (i < 0) {
            i = 0;
        }
        if (i > 7) {
            i = 7;
        }
        if (i > 0) {
            timeStr = timeStr.substring(0, 3 + i);
        } else {
            timeStr = timeStr.substring(0, 2);
        }
        return new BigDecimal(timeStr);
    }

    public static BigDecimal time() {
        return time(7);
    }

}
