package fi.cas.opplugin;
import com.google.gson.Gson;
import com.kingdee.bos.json.Kson;

import java.awt.Component;
import java.awt.Rectangle;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;



public class Utils {

    public static BigDecimal zero = new BigDecimal("0.00");
    /**
     * 日期格式化
     * @param date
     * @param format
     * @return
     */
    public static Date formatDate(String date, String format) {
        SimpleDateFormat dt = new SimpleDateFormat(format);
        try {
            return dt.parse(date);
        } catch (ParseException e) {
        }
        return null;
    }

    /**
     * 日期格式化
     * @param date
     * @param format
     * @return
     */
    public static String getData(Date date, String format) {
        SimpleDateFormat dt = new SimpleDateFormat(format);
        return dt.format(date);
    }

    public static BigDecimal toBigDecimal(Object obj)
    {
        if(obj == null)
            return BigDecimal.ZERO;
        if(obj instanceof BigDecimal)
            return (BigDecimal)obj;
        if(obj instanceof Integer)
            return new BigDecimal(((Integer)obj).toString());
        if(obj instanceof Long)
            return new BigDecimal(((Long)obj).toString());
        if(obj instanceof Double)
            return new BigDecimal(((Double)obj).doubleValue());
        if(obj.toString() == null)
            return BigDecimal.ZERO;
        String str = obj.toString().trim();
        if(str.toLowerCase().indexOf("e") > -1)
            try
            {
                return new BigDecimal(str);
            }
            catch(NumberFormatException e)
            {
                return BigDecimal.ZERO;
            }
        if(str.matches("^[+-]?\\d+[\\.\\d]?\\d*+$"))
            return new BigDecimal(str);
        else
            return BigDecimal.ZERO;
    }

    // 加
    public static BigDecimal add(Object a, Object b,int scale) {

        if (a == null)
            a = zero;

        if (b == null)
            b = zero;

        if (a instanceof BigDecimal && b instanceof BigDecimal) {
            BigDecimal aVal = (BigDecimal) a;
            BigDecimal bVal = (BigDecimal) b;
            return aVal.add(bVal).setScale(scale, BigDecimal.ROUND_HALF_UP);
        }

        return zero;
    }

    // 减
    public static BigDecimal sub(Object a, Object b,int scale) {

        if (a == null)
            a = zero;

        if (b == null)
            b = zero;

        if (a instanceof BigDecimal && b instanceof BigDecimal) {
            BigDecimal aVal = (BigDecimal) a;
            BigDecimal bVal = (BigDecimal) b;
            return aVal.subtract(bVal).setScale(scale, BigDecimal.ROUND_HALF_UP);
        }

        return zero;
    }



    public static String convertObjectToJson(Object object)  {
        if (object == null) {
            return "\"\"";
        }
        String result = null;
        try {
            result = Kson.getGson().toJson(object);
        } finally {
        }

        if (result == null) {
            return "\"\"";
        }

        if ((object instanceof String) && (result.startsWith("\""))
                && (result.endsWith("\""))) {
            result = result.substring(1, result.length() - 1);
        }

        result = processJsonResult(result);

        return result;
    }
    public static String processJsonResult(String result) {
        if (result == null) {
            return null;
        }

        result = result.replace("${quote_2312415321232}", "\"");
        result = result.replace("\"${leftbracket_2312415321232}", "{");
        result = result.replace("${leftbracket_2312415321232}", "{");
        result = result.replace("${rightbracket_2312415321232}\"", "}");
        result = result.replace("${rightbracket_2312415321232}", "}");
        return result;
    }


}
