package jp.mosp.common.part;

import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import jp.mosp.common.CommonConst;
import jp.mosp.common.JapanHolidayUtility;
import jp.mosp.common.common.MospUtility;

/**
 * 祝祭日に関するPartクラス
 * @author yoshida
 *	@version	0.0.1
 */
public class HolidayPart extends BasePart {

	/**
	 * コンストラクタ
	 */
	public HolidayPart() {
		super();
	}

	/**
	 * コンストラクタ
	 * @param partInfo
	 */
	public HolidayPart(PartInfo partInfo) {
		super(partInfo);
	}

	/**
	 * 祝日名称取得
	 * <p>	2009/02/25	m.yoshida	祝日メソッド修正 </p>
	 * @param targetDate		対象年月日
	 * @return	対象年月日祝日名称
	 * @throws Exception
	 */
	public String getPublicHolidayName(Date targetDate) throws Exception {
		String holidayName = "";
		// 対象日祝日名称取得
		String targetName = getHolidayMap(MospUtility.getYear(targetDate)).get(targetDate);
		if (targetName != null) {
			holidayName = targetName;
		}
		return holidayName;
	}

	/**
	 * 祝日名称マップ生成
	 * @param year			対象年
	 * @throws Exception
	 */
	private Map<Date, String> getHolidayMap(int year) throws Exception {
		Map<Date, String> holidayMap = new TreeMap<Date, String>();
		// 祝祭日、休日設定
		String[][] aryCodeHoliday = mospcode().getMosPCodeArray(CommonConst.TID_HOLIDAY);
		Date holiday = null;
		for (int k = 0; k < aryCodeHoliday.length; k++) {
			// MM/DDから始まる休日への対応
			if (aryCodeHoliday[k][0].startsWith("0")
					|| aryCodeHoliday[k][0].startsWith("1")
			) {
				StringBuffer date = new StringBuffer();
				date.append(String.valueOf(year));
				date.append(CommonConst.SEPARATOR_DATE);
				date.append(aryCodeHoliday[k][0].split(CommonConst.SEPARATOR_DATE)[0]);
				date.append(CommonConst.SEPARATOR_DATE);
				date.append(aryCodeHoliday[k][0].split(CommonConst.SEPARATOR_DATE)[1]);
				holiday = MospUtility.getDate(date.toString());
			} else {
				holiday = MospUtility.getDate(aryCodeHoliday[k][0]);
			}
			holidayMap.put(holiday, aryCodeHoliday[k][1]);
		}
		// ハッピーマンデー設定
		String[][] aryJapanHoliday = getAryHolidaySingleYear(year);
		for (int i = 0; i < aryJapanHoliday.length; i++) {
			holiday = MospUtility.getDate(aryJapanHoliday[i][0]);
			if (holidayMap.get(holiday) == null) {
				holidayMap.put(holiday, aryJapanHoliday[i][1]);
			}
		}
		return holidayMap;
	}

	/**
	 * 単年祝祭日配列取得
	 * @param year
	 * @return	result
	 */
	private String[][] getAryHolidaySingleYear(int year) {
		String[][] result = new JapanHolidayUtility(year).getArySyuku();
		return result;
	}

}
