package info.bukova.isspst;

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

import org.apache.commons.lang.time.DateUtils;

public class DateTimeUtils {
    public static Date getDate(Date value) {
        if (value == null) {
            return value;
        }

        // Keep date - truncate time
        return DateUtils.truncate(value, Calendar.DAY_OF_MONTH);
    }

    public static int getYear(Date value) {
        if (value == null) {
            return 0;
        }

        Calendar cal =Calendar.getInstance();
        cal.setTime(value);
        return cal.get(Calendar.YEAR);
    }

    public static boolean isEqualByDate(Date d1, Date d2) {
        if (d1 != null && d2 != null) {
            d1 = DateTimeUtils.getDate(d1);
            d2 = DateTimeUtils.getDate(d2);
            boolean equals = (d1.compareTo(d2) == 0);
            return equals;
        }

        return false;
    }

    public static boolean isEqualByYear(Date d1, Date d2) {
        if (d1 != null && d2 != null) {
            int year1 = DateTimeUtils.getYear(d1);
            int year2 = DateTimeUtils.getYear(d2);
            boolean equals = (year1 == year2);
            return equals;
        }

        return false;
    }
    public static boolean isEqualByDateForFilter(Date value, Date search) {
        if (search == null) {
            return true;
        } else if (value != null) {
            return DateTimeUtils.isEqualByDate(value, search);
        }

        return false;
    }

    public static String getFormatedDate(Date value) {
        if (value == null) {
            return "";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(StringUtils.localize("DateFormat"));
        String result = sdf.format(value);
        return result;
    }

    public static String getFormatedDirDate(Date value) {
        if (value == null) {
            return "";
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String result = sdf.format(value);
        return result;
    }

    public static String getFormatedDirDateDDMM(Date value) {
        if (value == null) {
            return "";
        }

        SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
        String result = sdf.format(value);
        return result;
    }

    public static String getFormatedYear(Date value) {
        if (value == null) {
            return "";
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String result = sdf.format(value);
        return result;
    }

    public static Date getCurrDateTime() {
        return new Date();
    }

    public static Date getCurrDate() {
        return DateTimeUtils.getDate(DateTimeUtils.getCurrDateTime());
    }

    public static Calendar getCalendarDelta(Date value, int calendarType, int delta) {
        if (value == null) {
            value = DateTimeUtils.getCurrDate();
        }

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(value);
        int deltaValue = calendar.get(calendarType);
        calendar.set(calendarType, deltaValue + delta);
        return calendar;
    }

    public static Calendar getCalendar(Date value) {
        if (value == null) {
            value = DateTimeUtils.getCurrDate();
        }

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(value);
        return calendar;
    }

    public static Date getDateDelta(Date value, int calendarType, int delta) {
        Calendar calendar = DateTimeUtils.getCalendarDelta(value, calendarType, delta);
        return calendar.getTime();
    }
}
