package com.udapsoft.waf.common.util;

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

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.sql.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import kr.co.hsnc.common.sql.util.RowSetUtility;
import kr.co.hsnc.common.util.ValueObject;

public class DateUtil {

	/**
	 * 기본 기간 설정
	 * 
	 * @return
	 */
	public static ValueObject getDefaultPeriod() {
		StringBuffer sql = new StringBuffer();
		try {

			sql.append(" SELECT (CASE WHEN to_char(sysdate,'DD') < '10' 								");
			sql.append("          	THEN to_char(trunc(add_months(sysdate,-1),'MONTH'),'YYYY-MM-DD') 		");
			sql.append("          	ELSE to_char(trunc(sysdate,'MONTH'),'YYYY-MM-DD') END) as start_date, ");
			sql.append(" 		(CASE WHEN to_char(sysdate,'DD') < '10' 								");
			sql.append(" 			THEN to_char(last_day(add_months(sysdate,-1)),'YYYY-MM-DD') 			");
			sql.append(" 			ELSE to_char(last_day(sysdate),'YYYY-MM-DD') END) as end_date			");
			sql.append("   FROM DUAL 																	");

			Search search = new SearchImpl();
			search.setStatement(sql.toString());

			return RowSetUtility.getValueObject(search.execute().getRow(0));

		} catch (Exception e) {
			WAFLogger.debug("[Exception]DateUtil.getDefaultPeriod()... " + e);
			return new ValueObject();
		}
	}

	public static String formatDateTime(String dateTime, String fromPattern,
			String toPattern) {
		if (dateTime == null || dateTime.trim().length() == 0)
			return "";
		SimpleDateFormat fromFormatter = new SimpleDateFormat(fromPattern,
				Locale.KOREA);
		Date date = null;
		try {
			date = fromFormatter.parse(dateTime);
		} catch (ParseException pe) {
			return dateTime;
		}
		SimpleDateFormat toFormatter = new SimpleDateFormat(toPattern, Locale.KOREA);
		return toFormatter.format(date);
	}

	public static String formatDateTime2(String dateTime, String fromPattern,
			String toPattern) {
		if (dateTime == null || dateTime.trim().length() == 0)
			return "";
		SimpleDateFormat fromFormatter = new SimpleDateFormat(fromPattern, Locale.US);
		Date date = null;
		try {
			date = fromFormatter.parse(dateTime);
		} catch (ParseException pe) {
			return dateTime;
		}
		SimpleDateFormat toFormatter = new SimpleDateFormat(toPattern, Locale.US);
		return toFormatter.format(date);
	}

	public static String formatTime(String timeString, String toPattern) {
		return formatDateTime(timeString, "HHmmss", toPattern);
	}

	/**
	 * DB서버 시간
	 * 
	 * @return
	 */
	public static String getSystemDate() {
		return getSystemDate("YYYYMMDDHH24MISS");
	}

	/**
	 * DB서버 시간
	 * 
	 * @param format
	 * @return
	 */
	public static String getSystemDate(String format) {
		Search search = new SearchImpl();
		String systemDate = "19900101";
		StringBuffer sql = new StringBuffer();
		try {
			sql.append(" SELECT TO_CHAR(SYSDATE, '" + format + "') SYS_DATE  \n");
			sql.append(" FROM   DUAL                                         \n");

			search.setStatement(sql.toString());
			systemDate = RowSetUtility.getValueObject(search.execute().getRow(0)).get("SYS_DATE");
			
		} catch (Exception e) {
			WAFLogger.error("DateUtil --> getSystemDate() : " + e.getMessage());
			WAFLogger.error("SQL : " + search.getStatement());
			WAFLogger.error(e);
		}
		return systemDate;
	}

	/**
	 * DB서버 시간
	 * 
	 * @param format
	 * @return
	 */
	public static String getAddedDate(String addMonth, String format) {

		Search search = new SearchImpl();
		String systemDate = "19900101";
		StringBuffer sql = new StringBuffer();
		try {
			sql.append(" SELECT   TO_CHAR(ADD_MONTHS(SYSDATE," + addMonth + "),'" + format + "') addedDate   	\n");
			sql.append(" FROM DUAL                                                                				\n");

			search.setStatement(sql.toString());
			systemDate = RowSetUtility.getValueObject(search.execute().getRow(0)).get("addedDate");
			
		} catch (Exception e) {
			WAFLogger.error("DateUtil --> getSystemDate() : " + e.getMessage());
			WAFLogger.error("SQL : " + search.getStatement());
			WAFLogger.error(e);
		}
		return systemDate;
	}

	/**
	 * 해당월의 끝날자
	 * 
	 * @param format
	 * @return
	 */
	public static String getLastDate(String yyyymmdd, String format) {
		Search search = new SearchImpl();
		String lastDate = "19900101";
		if (format.equals("")) {
			format = "yyyymmdd";
		}
		StringBuffer sql = new StringBuffer();

		try {
			sql.append(" SELECT   TO_CHAR(LAST_DAY(TO_DATE('" + yyyymmdd + "','YYYYMMDD')),'" + format + "') LASTDAY  \n");
			sql.append(" FROM     DUAL  \n");
			search.setStatement(sql.toString());
			lastDate = RowSetUtility.getValueObject(search.execute().getRow(0)).get("LASTDAY");
		} catch (Exception e) {
			WAFLogger.error("DateUtil --> getLastDate() : " + e.getMessage());
			WAFLogger.error("SQL : " + search.getStatement());
			WAFLogger.error(e);
		}
		return lastDate;
	}

	public static String getPrePostDate(String format, int prePostDay) {
		return getPrePostDate("", format, prePostDay);
	}

	/**
	 * 해당연월의 원하는 이전이후 날짜
	 * 
	 * @param format
	 *            , prePostDay(+-숫자)
	 * @return
	 */
	public static String getPrePostDate(String yyyymmdd, String format,
			int prePostDay) {

		Search search = new SearchImpl();
		String lastDate = "19900101";
		if (format.equals("")) {
			format = "yyyymmdd";
		}
		if (yyyymmdd.equals("")) {
			yyyymmdd = "SYSDATE";
		} else {
			yyyymmdd = "to_date('" + yyyymmdd + "','yyyymmdd')";
		}
		StringBuffer sql = new StringBuffer();

		try {
			sql.append(" SELECT   TO_CHAR(" + yyyymmdd + " + " + prePostDay + ",'" + format + "') PREPOSTDAY  \n");//2018.02.23
			sql.append(" FROM     DUAL  \n");
			search.setStatement(sql.toString());
			lastDate = RowSetUtility.getValueObject(search.execute().getRow(0)).get("PREPOSTDAY");
			
		} catch (Exception e) {
			WAFLogger.error("DateUtil --> getLastDate() : " + e.getMessage());
			WAFLogger.error("SQL : " + search.getStatement());
			WAFLogger.error(e);
		}
		return lastDate;
	}

	public static String getToday() {
		return getSystemDate("yyyyMMdd");
	}

	public static String getHour() {
		return getSystemDate("HH");
	}

	public static String getTime() {
		return getSystemDate("HHmmss");
	}

}
