package com.viettel.campaign.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.math.BigDecimal;
import java.sql.Clob;
import java.sql.SQLException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DataUtil {

    private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(DataUtil.class);

    public static boolean isNullOrZero(Long value) {
        return (value == null || value.equals(0L));
    }

    public static boolean isNullOrEmpty(String value) {
        return (value == null || value.isEmpty());
    }

    public static boolean isNullOrZero(BigDecimal value) {
        return (value == null || value.compareTo(BigDecimal.ZERO) == 0);
    }

    public static Long safeToLong(Object obj1, Long defaultValue) {
        Long result = defaultValue;
        if (obj1 != null) {
            try {
                result = Long.parseLong(obj1.toString());
            } catch (Exception ex) {
                logger.error(ex.getMessage(), ex);
            }
        }

        return result;
    }

    public static Long safeToLong(Object obj1) {
        Long result = null;
        if (obj1 != null) {
            try {
                result = Long.parseLong(obj1.toString());
            } catch (Exception ex) {
                logger.error(ex.getMessage(), ex);
            }
        }

        return result;
    }

    public static BigDecimal safeToBigDecimal(Object obj1) {
        BigDecimal result = new BigDecimal(0);
        if (obj1 != null) {
            try {
                result = BigDecimal.valueOf(Long.parseLong(obj1.toString()));
            } catch (Exception ex) {
                logger.error(ex.getMessage(), ex);
            }
        }

        return result;
    }

    public static Double safeToDouble(Object obj1) {
        Double result = null;
        if (obj1 != null) {
            try {
                result = Double.parseDouble(obj1.toString());
            } catch (Exception ex) {
                logger.error(ex.getMessage(), ex);
            }
        }

        return result;
    }

    public static Short safeToShort(Object obj1) {
        Short result = 0;
        if (obj1 != null) {
            try {
                result = Short.parseShort(obj1.toString());
            } catch (Exception ex) {
                logger.error(ex.getMessage(), ex);
            }
        }

        return result;
    }

    public static int safeToInt(Object obj1) {
        int result = 0;
        if (obj1 == null) {
            return 0;
        }
        try {
            result = Integer.parseInt(obj1.toString());
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }

        return result;
    }

    public static String safeToString(Object obj1) {
        if (obj1 == null) {
            return "";
        }

        return obj1.toString();
    }

    public static Date safeToDate(Object obj1) {
        Date result = null;
        if (obj1 == null) {
            return null;
        }
        try {
            result = (Date) obj1;
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
        return result;
    }

    public static boolean safeEqual(Long obj1, Long obj2) {
        if (obj1 == obj2) {
            return true;
        }
        return ((obj1 != null) && (obj2 != null) && (obj1.compareTo(obj2) == 0));
    }

    public static boolean safeEqual(String obj1, String obj2) {
        if (obj1 == obj2) {
            return true;
        }
        return ((obj1 != null) && (obj2 != null) && obj1.equals(obj2));
    }

    public static String convertDateToStringDDMMYYYYHHMISS(Date datetime) {
        String s = null;
        if (datetime != null) {
            Format formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            s = formatter.format(datetime);
        }
        return s;
    }

    public static Date convertStringToDateDDMMYYYYHHMISS(String str) {
        Date dateTime = null;
        try {
            if (str != null) {
                SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                dateTime = formatter.parse(str);
            }
        } catch (Exception e) {
            logger.error(e);
        }
        return dateTime;
    }

    public static String safeToClob(Clob clobValue) {
        String result = null;
        if (clobValue != null) {
            StringBuilder sb = new StringBuilder();
            try {
                Reader reader = clobValue.getCharacterStream();
                BufferedReader br = new BufferedReader(reader);
                String line;
                while (null != (line = br.readLine())) {
                    sb.append(line);
                }
                br.close();
            } catch (SQLException | IOException e) {
                logger.error(e.getMessage(), e);
            }
            result = sb.toString();
        }

        return result;
    }

    public static Date toDatefromStringFormat(String value, String format) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            date = sdf.parse(value);
            if (!value.equals(sdf.format(date))) {
                date = null;
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
        return date;
    }

    public static boolean isNumber(String strId) {
        return strId.matches("-?\\d+(\\.\\d+)?");
    }

    public static boolean isLong(String strId) {
        return strId.matches("\\d*");
    }

    public static String standardPhone(String mobilePhone) {
        if (mobilePhone != null) {
            if (mobilePhone.startsWith("0")) {
                mobilePhone = mobilePhone.substring(1, mobilePhone.length());
            } else if (mobilePhone.startsWith("84")) {
                mobilePhone = mobilePhone.substring(2, mobilePhone.length());
            }
        }
        return mobilePhone;
    }

    public static String tohhmmss(Long conferenctime) {
        String fmt = "0";
        try {
            if (conferenctime == 0) {
                return "0";
            }
            // conferencetime: tinh theo s
            long mmt = conferenctime / 60;
            long ss = conferenctime % 60;
            long mm = mmt % 60;
            long hh = mmt / 60;
            fmt = replace(hh) + ":" + replace(mm) + ":" + replace(ss);
        } catch (Exception ex) {
            logger.error(ex);
        }
        return fmt;
    }

    public static String replace(long m) {
        String mt = String.valueOf(m);
        mt = (m >= 10) ? mt : '0' + mt;
        return mt;
    }

    public static String getSafeFileName(String input) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != '/' && c != '\\' && c != 0) {
                sb.append(c);
            }
        }
        return sb.toString();
    }
//    public static void main(String[] args) {
////        Date rs = DataUtil.toDatefromStringFormat("20170702", "yyyyMMdd");
////        System.out.println(rs);
//        String rs = standardPhoneNew("000953478578358");
//        System.out.println(rs);
//    }

    public static String standardPhoneNew(String input) {
        if (input == null) {
            return null;
        }
        while (input.startsWith("0")) {
            input = input.substring(1, input.length());
        }
        return input;
    }

    public static boolean isNullOrZero(Short value) {
        return (value == null || value.equals(Short.parseShort("0")));
    }
    public static boolean isNullOrZero(Integer value) { return (value == null || value.equals(Integer.parseInt("0"))); }
}
