package com.smppw.common.rollrange.date;

import com.smppw.constants.Consts;
import com.smppw.common.pojo.enums.Frequency;
import org.joda.time.DateTime;

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

/**
 * @author Yu Liu
 *
 */
public class DateConvertorImpl implements DateConvertor {
	//这玩意线程不安全
//	static DateFormat dfm = new SimpleDateFormat(Consts.DEFAULT_DATE_PATTERN);
//	static {
//		dfm.setTimeZone(TimeZone.getTimeZone("GMT"));
//	}

	@Override
	public String convertToString(int index) {
		Calendar startDate = getCalendar(Consts.START_DATE);
		startDate = TimeFactory.getDateTime(Frequency.Daily).shift(startDate, index);
		return new SimpleDateFormat(Consts.DEFAULT_DATE_PATTERN).format(startDate.getTime());
	}

	private Calendar getCalendar(String dateStr) {
		Calendar c = Calendar.getInstance();
		try {
			//			System.out.println("====================>>"+dateStr);
			//			date = dfm.parse(dateStr);

			if (dateStr != null && !dateStr.equals("")) {
				Date date = DateTime.parse(dateStr).toDate();
				c.setTime(date);
			}
		} catch (Exception e) {
//			LogUtils.e("date error:"+dateStr);
//			throw new RuntimeException(e);
			e.printStackTrace();
		}
		return c;
	}

	@Override
	public int convertToIndex(String dateStr) {
		Calendar startDate = getCalendar(Consts.START_DATE);
		Calendar endDate = getCalendar(dateStr);
		return TimeFactory.getDateTime(Frequency.Daily).between(startDate, endDate);
	}

	@Override
	public int getDateOffset(String startStr, String endStr, Frequency frequency, boolean isUse5DaysPerWeek) {
		Calendar startDate = getCalendar(startStr);
		Calendar endDate = getCalendar(endStr);
		return TimeFactory.getDateTime(frequency).between(startDate, endDate);
	}

	@Override
	public String addPeriod(String date, int periods, Frequency frequency, PeriodPosition position,
			boolean isUse5DaysPerWeek) {
		Calendar startDate = getCalendar(date);
		IDateTime time = TimeFactory.getDateTime(frequency);
		startDate = time.shift(startDate, periods);
		if (position == PeriodPosition.PeriodFirst) {
			startDate = time.startDayOfPeriod(startDate);
		} else if (position == PeriodPosition.PeriodLast) {
			startDate = time.endDayOfPeriod(startDate);
			if (frequency == Frequency.Weekly && isUse5DaysPerWeek) {
				String res = new SimpleDateFormat(Consts.DEFAULT_DATE_PATTERN).format(startDate.getTime());
				return setPeriodLast(res, frequency, true);
			}
		}
		// else if(position == PeriodPosition.PeriodFixed) {
		// do nothing
		// }
		return new SimpleDateFormat(Consts.DEFAULT_DATE_PATTERN).format(startDate.getTime());
	}

	@Override
	public String setPeriodFirst(String date, Frequency frequency) {
		if(Frequency.Daily == frequency)
			return date;
		
		Calendar startDate = getCalendar(date);
		startDate = TimeFactory.getDateTime(frequency).startDayOfPeriod(startDate);
		return new SimpleDateFormat(Consts.DEFAULT_DATE_PATTERN).format(startDate.getTime());
	}

	@Override
	public String setPeriodLast(String date, Frequency frequency) {
		return setPeriodLast(date, frequency, false);
	}

	@Override
	public String setPeriodLast(String date, Frequency frequency, boolean isUse5DaysPerWeek) {
		if (frequency == Frequency.Weekly && isUse5DaysPerWeek) {
			DateTime dt = DateTime.parse(date);
			dt = dt.dayOfWeek().setCopy(5);
			return dt.toString(Consts.DEFAULT_DATE_PATTERN);
		}
		Calendar endDate = getCalendar(date);
		endDate = TimeFactory.getDateTime(frequency).endDayOfPeriod(endDate);
		return new SimpleDateFormat(Consts.DEFAULT_DATE_PATTERN).format(endDate.getTime());
	}

}
