package kcg.faics.member.vo;

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

import org.apache.commons.lang3.StringUtils;

/**
 * 과거외사경찰 VO.
 * 
 * @author kimnomin
 *
 */
public class ExMemberVO extends MemberVO {
	private static final long serialVersionUID = 9089598602982146798L;
	/**
	 * 등록일자.
	 */
	private Date cdateEx;
	/**
	 * 현재소속.
	 */
	private String nowPlace;
	/**
	 * 현재소속 명칭.
	 */
	private String nowPlaceStr;
	/**
	 * 현재계급.
	 */
	private String nowPosition;
	/**
	 * 현재계급 명칭.
	 */
	private String nowPositionStr;

	/**
	 * @return the cdateEx
	 */
	public final Date getCdateEx() {
		return cdateEx;
	}

	/**
	 * @return the nowPlace
	 */
	public final String getNowPlace() {
		return nowPlace;
	}

	/**
	 * @param nowPlace
	 *            the nowPlace to set
	 */
	public final void setNowPlace(final String nowPlace) {
		this.nowPlace = nowPlace;
	}

	/**
	 * @return the nowPlaceStr
	 */
	public final String getNowPlaceStr() {
		return nowPlaceStr;
	}

	/**
	 * @param nowPlaceStr
	 *            the nowPlaceStr to set
	 */
	public final void setNowPlaceStr(final String nowPlaceStr) {
		this.nowPlaceStr = nowPlaceStr;
	}

	/**
	 * @return the nowPosition
	 */
	public final String getNowPosition() {
		return nowPosition;
	}

	/**
	 * @param nowPosition
	 *            the nowPosition to set
	 */
	public final void setNowPosition(final String nowPosition) {
		this.nowPosition = nowPosition;
	}

	/**
	 * @return the nowPositionStr
	 */
	public final String getNowPositionStr() {
		return nowPositionStr;
	}

	/**
	 * @param nowPositionStr
	 *            the nowPositionStr to set
	 */
	public final void setNowPositionStr(final String nowPositionStr) {
		this.nowPositionStr = nowPositionStr;
	}

	/**
	 * @param cdateEx
	 *            the cdateEx to set
	 */
	public final void setCdateEx(final Date cdateEx) {
		this.cdateEx = cdateEx;
	}

	/**
	 * @param cdateEx
	 *            the cdateEx(String) to set
	 */
	public final void setCdateEx(final String cdateEx) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.KOREA);
		Date d = null;
		try {
			d = sdf.parse(cdateEx);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		this.setCdateEx(d);
	}

	/**
	 * 외사근무기간을 계산하여 반환한다. (등록일 - 현 부서 배치일)
	 * 
	 * @return 외사근무기간 (개월)
	 * @throws ParseException
	 *             Date 파싱 예외
	 */
	private int calcWorkTerm() throws ParseException {
		int term = 0;
		if (this.getCdate() != null && StringUtils.isNotBlank(this.getAdate())) {
			Date cdate = this.getCdateEx();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.KOREA);
			Date adate = sdf.parse(this.getAdate());
			term = super.calcWorkTerm(adate, cdate);
		}

		return term;
	}
	
	/**
	 * 외사근무기간을 표현할 문자열을 반환한다.
	 * 
	 * @return 외사근무기간 문자열 (?년 ?개월)
	 * @throws ParseException
	 *             Date 파싱 예외
	 */
	public String getWorkTermStr() throws ParseException {
		return super.termToStr(this.calcWorkTerm());
	}
	
	/**
	 * 총 외사경력을 표현할 문자열을 반환한다. (외사근무기간 + 과거외사경력)
	 * 
	 * @return 총 외사경력 (?년 ?개월)
	 * @throws ParseException
	 *             Date 파싱 예외
	 */
	public String getTotExcareerTermStr() throws ParseException {
		String excareer = this.getExcareer();
		int term = this.calcWorkTerm();

		if (StringUtils.isNotBlank(excareer) && StringUtils.contains(excareer, "-")) {
			String[] excareers = excareer.split("-"); 
			if (excareers.length == 2) {
				int year = Integer.parseInt(excareers[0]);
				int month = Integer.parseInt(excareers[1]);
				month += year * 12;
				term += month;
			}
		}
		
		return super.termToStr(term);
	}
}
