package com.bug.lib;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.TimeZone;

import java.util.Date;

public class DateUtil {
    private static DateUtil instance = null;
    public static String ServerDateTimeFormat = "yyyy.MM.dd HH:mm:ss";
    public static String ServerDateFormat = "yyyy.MM.dd";
    public static String LocalDateTimeFormat;
    public static String LocalDateFormat;

    private DateUtil() {
    }

    static public synchronized DateUtil getInstance() {
        if (instance == null) {
            instance = new DateUtil();
            LocalDateTimeFormat = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_TIME_SHORT).getPattern();
            if(LocalDateTimeFormat.indexOf(".yy ") != -1) {
                LocalDateTimeFormat = LocalDateTimeFormat.replace(".yy ", ".yyyy ");
            }
            if(LocalDateTimeFormat.startsWith("yy.")) {
                LocalDateTimeFormat = LocalDateTimeFormat.replace("yy.", "yyyy.");
            }
            LocalDateFormat = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT).getPattern();
            if(LocalDateFormat.endsWith(".yy")) {
                LocalDateFormat = LocalDateFormat.replace(".yy", ".yyyy");
            }
            if(LocalDateFormat.startsWith("yy.")) {
                LocalDateFormat = LocalDateFormat.replace("yy.", "yyyy.");
            }
        }
        return instance;
    }
    public static Date parseServerDateTime(String value) {
        if(value != null) {
            return DateTimeFormat.getFormat(ServerDateTimeFormat + " z").parse((String) value + " UTC");
        }
        return null;
    }
    public static Date parseServerDate(String value) {
        if(value != null && value.length() > 0) {
            return DateTimeFormat.getFormat(ServerDateFormat).parse(value);
        }
        return null;
    }
    public static Date parseLocaleDate(String value) {
        if(value != null && value.length() > 0) {
            return DateTimeFormat.getFormat(LocalDateFormat).parse(value);
        }
        return null;
    }
    public static String formatToLocaleDateTime(Date date) {
        if(date != null) {
            return DateTimeFormat.getFormat(LocalDateTimeFormat).format(date);
        }
        return "";
    }
    public static String convertServerToLocaleDateTime(String value) {
        if(value != null && value.length() > 0) {
            return DateTimeFormat.getFormat(LocalDateTimeFormat).format(DateTimeFormat.getFormat(ServerDateTimeFormat + " z").parse((String) value + " UTC"));
        }
        return "";
    }
    public static String formatToLocaleDate(Date date) {
        if(date != null) {
            return DateTimeFormat.getFormat(LocalDateFormat).format(date);
        }
        return "";
    }
    public static String convertServerToLocaleDate(String value) {
        if(value != null && value.length() > 0) {
            return DateTimeFormat.getFormat(LocalDateFormat).format(DateTimeFormat.getFormat(ServerDateFormat).parse(value));
        }
        return "";
    }
    public static String formatToServerDateTime(Date date) {
        return DateTimeFormat.getFormat(ServerDateTimeFormat).format(date, TimeZone.createTimeZone(0));
    }
    public static String convertToServerDateTime(Date date) {
        return DateTimeFormat.getFormat(ServerDateTimeFormat).format(date, TimeZone.createTimeZone(0));
    }
    public static String toGlobalDate(Date date) {
        return DateTimeFormat.getFormat(ServerDateFormat).format(date);
    }
    public static String formatToServerDate(Date date) {
        return DateTimeFormat.getFormat(ServerDateFormat).format(date);
    }
    public static String toGlobalDate(String value) {
        return DateTimeFormat.getFormat(ServerDateFormat).format(DateTimeFormat.getFormat(LocalDateFormat).parse(value));
    }
    public static String convertToServerDate(String value) {
        return DateTimeFormat.getFormat(ServerDateFormat).format(DateTimeFormat.getFormat(LocalDateFormat).parse(value));
    }
}
