/*
 * MosP - Mind Open Source Project         http://www.mosp.jp/
 * Copyright (C) 1987-2009 MIND Co., Ltd.  http://www.e-mind.co.jp/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package jp.mosp.common.part;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import jp.mosp.common.CommonConst;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;
import jp.mosp.common.dto.MosPConfDto;

/**
 * 和暦に関するPartクラス
 * @author yoshida
 *	@version	0.0.1
 */
public class JPDatePart extends BasePart {

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

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

	/**
	 * 和暦表示設定
	 * @param targetDate	対象年月日
	 * @return	和暦表示
	 * @throws MospException
	 * @throws Exception
	 */
	public String getJapaneseDate(Date targetDate) throws MospException, Exception {
		return getDateString(
				mospconf().getConfList(CommonConst.CONF_ID_JP_IMP_CAL),
				cmcode().getJPCalendarGengo(),
				targetDate
		);
	}

	/**
	 * 和暦表示設定
	 * @param targetDate	対象年月日(yyyy-mm-dd)
	 * @return	和暦表示
	 * @throws MospException
	 * @throws Exception
	 */
	public String getJapaneseDate(String targetDate) throws MospException, Exception {
		return getJapaneseDate(DatePart.convHyphenDateToDate(targetDate));
	}

	/**
	 * 日付文字列取得(和暦)
	 * @param list 和暦情報リスト
	 * @param date 日
	 * @return 日付文字列
	 * @throws MospException
	 */
	private String getDateString(List<MosPConfDto> list, String[][] aryEra, Date date) throws MospException {
		if (date == null || list == null) {
			return "";
		}
		String[] strDate = getJpImperialCal(list, date);
		strDate[0] = StringPart.getCodeName(strDate[0], aryEra);
		for (int i = 1; i < strDate.length; i++) {
			strDate[i] = strDate[i].length() < 2 ? CommonConst.SUPPRESS_DATE + strDate[i] : strDate[i];
		}
		StringBuffer sb = new StringBuffer();
		sb.append(strDate[0]);
		sb.append(strDate[1]);
		sb.append(CommonConst.NAM_YEAR);
		sb.append(strDate[2]);
		sb.append(CommonConst.NAM_MONTH);
		sb.append(strDate[3]);
		sb.append(CommonConst.NAM_DAY);
		return sb.toString();
	}

	/**
	 * 和暦取得
	 * @param list 和暦情報リスト
	 * @param date 日付
	 * @return 日付配列
	 * @throws MospException 
	 */
	private String[] getJpImperialCal(List<MosPConfDto> list, Date date) throws MospException {
		String[] aryJpDate = new String[4];
		if (date == null) {
			return aryJpDate;
		}
		for (Iterator<MosPConfDto> it = list.iterator(); it.hasNext();) {
			MosPConfDto dto = it.next();
			Date startDate = getDate(dto.getConfCode ());
			Date endDate   = getDate(dto.getConfValue());
			if (startDate.compareTo(date) <= 0 && endDate.compareTo(date) >= 0) {
				int year = MospUtility.getYear(date) - MospUtility.getYear(startDate) + 1;
				aryJpDate[0] = dto.getConfId();
				aryJpDate[1] = String.valueOf(year);
				aryJpDate[2] = String.valueOf(MospUtility.getMonth(date));
				aryJpDate[3] = String.valueOf(MospUtility.getDay  (date));
				return aryJpDate;
			}
		}
		throw new MospException(MospConst.EX_DATE_INVALID);
	}

	/**
	 * 日付取得(/無し文字列用)
	 * @param strDate 日付文字列
	 * @return 日付
	 * @throws MospException 
	 */
	private Date getDate(String strDate) throws MospException {
		if (strDate == null || strDate.length() != 8) {
			throw new MospException(MospConst.EX_DATE_INVALID);
		}
		return MospUtility.getDate(
				strDate.substring(0, 4) + CommonConst.SEPARATOR_DATE +
				strDate.substring(4, 6) + CommonConst.SEPARATOR_DATE +
				strDate.substring(6, 8)
		);
	}

}
