package it.integry.integrywmsnative.core.utility;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import it.integry.integrywmsnative.core.exception.DateNotRecognizedException;
import it.integry.integrywmsnative.core.exception.TimeNotRecognizedException;

/**
 * Created by GiuseppeS on 07/03/2018.
 */

public class UtilityDate {

    public static class COMMONS_DATE_FORMATS {
        public static final String DMY_SLASH = "dd/MM/yyyy";
        public static final String YMD_SLASH = "yyyy/MM/dd";
        public static final String DM_SLASH = "dd/MM";
        public static final String MD_SLASH = "MM/dd";
        public static final String YMD_DASH = "yyyy-MM-dd";
        public static final String DMY_TIME_SLASH = DMY_SLASH + " HH:mm:ss";
        public static final String YMD_TIME_SLASH = YMD_SLASH + " HH:mm:ss";
        public static final String DM_TIME_SLASH = DM_SLASH + " HH:mm";
        public static final String MD_TIME_SLASH = MD_SLASH + " HH:mm";
        public static final String TIME = "HH:mm";
        public static final String TIME_W_SECS = "HH:mm:ss";


        public static final String DM_HUMAN = "dd MMM";
        public static final String DMY_HUMAN = "dd MMM yyyy";
        public static final String DMY_HUMAN_LONG = "dd MMMM yyyy";
    }

    public static Date recognizeDateWithExceptionHandler(String dateString)  {
        try{
            return UtilityDate.recognizeDate(dateString);
        } catch (ParseException | DateNotRecognizedException | TimeNotRecognizedException pex){
            UtilityLogger.errorMe(pex);
            return null;
        }
    }

    public static Date recognizeDate(String dateString) throws ParseException, DateNotRecognizedException, TimeNotRecognizedException {

        if (dateString == null) {
            return null;
        }

        //SE SONO PIU' DI 10 caratteri allora c'è anche l'ora

        SimpleDateFormat format = null;
        char dateSeparator;

        String onlyDateSubstring = dateString.substring(0, 10);
        if(onlyDateSubstring.contains("/")) dateSeparator = '/';
        else if(onlyDateSubstring.contains("-")) dateSeparator = '-';
        else if(onlyDateSubstring.contains(".")) dateSeparator = '.';
        else throw new DateNotRecognizedException(dateString);

        String dateFormatString = (dateString.charAt(2) == dateSeparator)
                ? "dd" + dateSeparator + "MM" + dateSeparator + "yyyy"
                : "yyyy" + dateSeparator + "MM" + dateSeparator + "dd";


        if(dateString.length() > 10){

            char timeSeparator;

            String onlyTimeSubstring = dateString.substring(10, 14);
            if(onlyTimeSubstring.contains("-")) timeSeparator = '-';
            else if(onlyTimeSubstring.contains(":")) timeSeparator = ':';
            else throw new TimeNotRecognizedException(dateString);

            String timeFormatString = "HH" + timeSeparator + "mm" + timeSeparator + "ss";

            format = new SimpleDateFormat(dateFormatString + " " + timeFormatString);


        } else {
            format = new SimpleDateFormat(dateFormatString);
        }

        //format.setTimeZone(TimeZone.getTimeZone("GMT"));

        return format.parse(dateString);
    }


    public static String formatDate(Date dateToFormat, String format) {
        if(dateToFormat != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            //sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
            return sdf.format(dateToFormat);
        }
        else return null;
    }


    public static Date getDateInstance() {
        return getCalendarInstance().getTime();
    }

    public static Calendar getCalendarInstance() {
        return Calendar.getInstance();
    }
}
