package org.opencps.dossiermgt.action.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.opencps.datamgt.model.Holiday;
import org.opencps.datamgt.service.HolidayLocalServiceUtil;

import com.liferay.portal.kernel.dao.orm.QueryUtil;

public class DossierOverDueUtils {

	public static final int START_HOUR_OF_DAY = 8;

	public static final int END_HOUR_OF_DAY = 18;

	public static final int END_MINUTE_OF_DAY = 0;

	public static final int START_MINUTE_OF_DAY = 0;

	public static final int START_NAP_HOUR_OF_DAY = 12;

	public static final int END_NAP_HOUR_OF_DAY = 13;

	public static final int START_NAP_MINUTE_OF_DAY = 0;

	public static final int END_NAP_MINUTE_OF_DAY = 0;

	public static Date getStepOverDue(int overDuePoint, Date date) {

		// TODO add logic here
		return null;
	}
	
	public static String getEstimateDate(int processingDay) {
		Date now = new Date();
		int start = -1, end = -1;

		Calendar c = Calendar.getInstance();
		c.setTime(now);
		c.add(Calendar.DATE, processingDay);

		List<Holiday> lstHoliday = HolidayLocalServiceUtil.getHolidaies(start, end);

		for (Holiday holiday : lstHoliday) {
			Date holidaydate = holiday.getHolidayDate();
			if ((holidaydate.compareTo(now) > 0 && holidaydate.compareTo(c.getTime()) < 0)
					|| holidaydate.compareTo(c.getTime()) == 0) {
				processingDay++;
			}
		}

		c.setTime(now);
		c.add(Calendar.DATE, processingDay);
		Date estimateDate = c.getTime();

		return dateFormat.format(estimateDate);
	}

	public static void main(String[] args) {
		System.out.println(calculateEndDate(new Date(), 5));
		
	}
	public static Date calculateEndDate(Date startDate, int duration) {
		Calendar startCal = Calendar.getInstance();

		startCal.setTime(startDate);
		
		for (int i = 1; i < duration; i++) {
			startCal.add(Calendar.DAY_OF_MONTH, 1);
			while (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
					|| startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
				startCal.add(Calendar.DAY_OF_MONTH, 1);
			}
		}

		return startCal.getTime();
	}

	public static int calculateDuration(Date startDate, Date endDate) {
		Calendar startCal = Calendar.getInstance();
		startCal.setTime(startDate);

		Calendar endCal = Calendar.getInstance();
		endCal.setTime(endDate);

		int workDays = 0;

		if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) {
			startCal.setTime(endDate);
			endCal.setTime(startDate);
		}

		do {
			startCal.add(Calendar.DAY_OF_MONTH, 1);
			if (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
					&& startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
				workDays++;
			}
		} while (startCal.getTimeInMillis() <= endCal.getTimeInMillis());

		return workDays;
	}
	
	private boolean isHoliday(Date checkDate) {

		boolean isHoliday = false;

		List<Holiday> holidays = new ArrayList<Holiday>();

		holidays = HolidayLocalServiceUtil.getHolidaies(QueryUtil.ALL_POS, QueryUtil.ALL_POS);

		for (Holiday holiday : holidays) {
			if (equalDay(checkDate, holiday.getHolidayDate()) == 0) {
				isHoliday = true;

				break;
			}
		}

		return isHoliday;
	}

	private int equalDay(Date date1, Date date2) {
		return truncateToDay(date1).compareTo(date2);
	}

	private Date truncateToDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	

	private static final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
}

@@ -2821,16 +2821,23 @@
 private 
+static 
 boolean 
@@ -2864,16 +2864,16 @@
 Date) %7B%0A
-
 %0A%09%09boole
@@ -3214,16 +3214,23 @@
 private 
+static 
 int equa
@@ -3321,16 +3321,23 @@
 private 
+static 
 Date tru
@@ -3618,18 +3618,571 @@
 e();%0A%09%7D%0A
-
 %09%0A
+%09public static Date calculateEndDateUsingHoliday(Date startDate, int duration) %7B%0A%09%09Calendar startCal = Calendar.getInstance();%0A%0A%09%09startCal.setTime(startDate);%0A%09%09%0A%09%09for (int i = 1; i %3C duration; i++) %7B%0A%09%09%09while (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY%0A%09%09%09%09%09%7C%7C startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) %7B%0A%09%09%09%09startCal.add(Calendar.DAY_OF_MONTH, 1);%0A%09%09%09%7D%0A%09%09%09while (isHoliday(startCal.getTime())) %7B%0A%09%09%09%09startCal.add(Calendar.DAY_OF_MONTH, 1);%0A%09%09%09%7D%0A%09%09%09startCal.add(Calendar.DAY_OF_MONTH, 1);%0A%09%09%7D%0A%0A%09%09return startCal.getTime();%0A%09%7D%0A
 %09%0A%0A%09priv

Add due date calculate based on holiday