package com.its.app.utils;

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

public class TimeUtils {

	public static final int TYPE_CURR_05MIN = 0;
	public static final int TYPE_PRCS_05MIN = 1;
	public static final int TYPE_PRCS_15MIN = 2;
	public static final int TYPE_PRCS_30MIN = 3;
	public static final int TYPE_PRCS_HOUR  = 4;
	public static final int TYPE_PRCS_DAY   = 5;
	public static final int TYPE_PRCS_MONTH = 6;
	public static final int TYPE_PRCS_YEAR  = 7;

	/* 현재 시각을 문자열로 리턴 */
	public static String getCurrentTimeString() {
		Calendar cal = Calendar.getInstance();
		return new SimpleDateFormat("yyyyMMddHHmmss").format(cal.getTime());
	}
	public static String getCurrentTimeString(String parmaFmt) {
		Calendar cal = Calendar.getInstance();
		return new SimpleDateFormat(parmaFmt).format(cal.getTime());
	}
	public static Date getCurrentDate() {
		Calendar cal = Calendar.getInstance();
		return cal.getTime();
	}

	public static Date stringToDate(String paramTime) {
		SimpleDateFormat transFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		Date to = null;
		try {
			to = transFormat.parse(paramTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return to;
	}

	public static String dateToString(Date paramDt, String paramFmt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(paramDt);
		return new SimpleDateFormat(paramFmt).format(cal.getTime());
	}
	public static String dateToString(Date paramDt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(paramDt);
		return new SimpleDateFormat("yyyyMMddHHmmss").format(cal.getTime());
	}

	/* 현재 시각을 second 로 리턴 */
	public static long getCurrentTimeSeconds() {
		Calendar cal = Calendar.getInstance();
		return Math.round(cal.getTimeInMillis() / 1000.0);
	}
	/* 현재 시각을 milli second 로 리턴 */
	public static long getCurrentTimeMilliSeconds() {
		Calendar cal = Calendar.getInstance();
		return cal.getTimeInMillis();
	}

	/* 정주기 현재 5분 시각 */
	public static String getFiveMinString(Date paramDt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(paramDt);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		int min = cal.get(Calendar.MINUTE);
		cal.add(Calendar.MINUTE, -(min % 5));
		return new SimpleDateFormat("yyyyMMddHHmmss").format(cal.getTime());
	}
	public static String getFiveMinString() {
		Calendar cal = Calendar.getInstance();
		return getFiveMinString(cal.getTime());
	}

	private static String getItsTimeString(Date paramDt, int cycle, int period) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(paramDt);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		int min = cal.get(Calendar.MINUTE);
		cal.add(Calendar.MINUTE, -(min % cycle) - (cycle * period));
		return new SimpleDateFormat("yyyyMMddHHmmss").format(cal.getTime());
	}

	private static String getItsTimeToString(Date paramDt, int type) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(paramDt);
		switch(type) {
			case TYPE_CURR_05MIN:
			case TYPE_PRCS_05MIN: cal.add(Calendar.MINUTE, 5); break;
			case TYPE_PRCS_15MIN: cal.add(Calendar.MINUTE, 15); break;
			case TYPE_PRCS_30MIN: cal.add(Calendar.MINUTE, 30); break;
			case TYPE_PRCS_HOUR:  cal.add(Calendar.HOUR_OF_DAY, 1); break;
			case TYPE_PRCS_DAY:   cal.add(Calendar.DATE, 1); break;
			case TYPE_PRCS_MONTH: cal.add(Calendar.MONTH, 1); break;
			case TYPE_PRCS_YEAR: cal.add(Calendar.YEAR, 1); break;
		}
		cal.add(Calendar.SECOND, -1);
		return new SimpleDateFormat("yyyyMMddHHmmss").format(cal.getTime());
	}

	public static String getTime(int type) {
		Calendar cal = Calendar.getInstance();
		return getTime(cal.getTime(), type);
	}
	public static String getTime(String paramStr, int type) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(stringToDate(paramStr));
		return getTime(cal.getTime(), type);
	}
	public static String getTime(Date paramDt, int type) {
		switch(type) {
			case TYPE_CURR_05MIN: return getItsTimeString(paramDt, 5, 0);
			case TYPE_PRCS_05MIN: return getItsTimeString(paramDt, 5, 1);
			case TYPE_PRCS_15MIN: return getItsTimeString(paramDt, 15, 1);
			case TYPE_PRCS_30MIN: return getItsTimeString(paramDt, 30, 1);
			case TYPE_PRCS_HOUR:  return getItsTimeString(paramDt, 60, 1);
			case TYPE_PRCS_DAY:   return getItsTimeString(paramDt, 60*24, 1).substring(0, 8) + "000000";
			case TYPE_PRCS_MONTH: return getItsTimeString(paramDt, 60*24, 1).substring(0, 6) + "01000000";
			case TYPE_PRCS_YEAR:  return getItsTimeString(paramDt, 60*24, 1).substring(0, 4) + "0101000000";
		}
		return "";
	}
	public static String getToTime(int type) {
		String prcsTime = getTime(type);
		return getToTime(prcsTime, type);
	}
	public static String getToTime(String paramStr, int type) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(stringToDate(paramStr));
		return getToTime(cal.getTime(), type);
	}
	public static String getToTime(Date paramDt, int type) {
		return getItsTimeToString(paramDt, type);
	}

	public static String getCurrMin() {
		Calendar cal = Calendar.getInstance();
		return getItsTimeString(cal.getTime(), 5, 0);
	}
	public static String getCurrMin(String paramStr) {
		return getItsTimeString(stringToDate(paramStr), 5, 0);
	}
	public static String getCurrMin(Date paramDt) {
		return getItsTimeString(paramDt, 5, 0);
	}

	public static String get05Min() {
		Calendar cal = Calendar.getInstance();
		return getItsTimeString(cal.getTime(), 5, 1);
	}
	public static String get05Min(String paramStr) {
		return getItsTimeString(stringToDate(paramStr), 5, 1);
	}
	public static String get05Min(Date paramDt) {
		return getItsTimeString(paramDt, 5, 1);
	}

	public static String get15Min() {
		Calendar cal = Calendar.getInstance();
		return getItsTimeString(cal.getTime(), 15, 1);
	}
	public static String get15Min(String paramStr) {
		return getItsTimeString(stringToDate(paramStr), 15, 1);
	}
	public static String get15Min(Date paramDt) {
		return getItsTimeString(paramDt, 15, 1);
	}

	public static String getHour() {
		Calendar cal = Calendar.getInstance();
		return getItsTimeString(cal.getTime(), 60, 1);
	}
	public static String getHour(String paramStr) {
		return getItsTimeString(stringToDate(paramStr), 60, 1);
	}
	public static String getHour(Date paramDt) {
		return getItsTimeString(paramDt, 60, 1);
	}

	public static String getDay() {
		Calendar cal = Calendar.getInstance();
		return getItsTimeString(cal.getTime(), 60*24, 1).substring(0, 8) + "000000";
	}
	public static String getDay(String paramStr) {
		return getItsTimeString(stringToDate(paramStr), 60*24, 1).substring(0, 8) + "000000";
	}
	public static String getDay(Date paramDt) {
		return getItsTimeString(paramDt, 60*24, 1).substring(0, 8) + "000000";
	}

	public static String getToString(Date paramDt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(paramDt);
		cal.add(Calendar.SECOND, -1);	/* 주어진 시각에서 1초를 뺀 시각 */
		return new SimpleDateFormat("yyyyMMddHHmmss").format(cal.getTime());
	}

	public static String getFiveMinToString(Date paramDt) {
		String strFiveMin = getFiveMinString(paramDt);
		Date   dtFiveMin = stringToDate(strFiveMin);
		return getToString(dtFiveMin);
	}
	public static String getFiveMinToString() {
		Calendar cal = Calendar.getInstance();
		return getFiveMinToString(cal.getTime());
	}


}
