package org.jerrymouse.chineseCalendar.model.birthday;

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

//一般理解为阳历
public class SimpleCalendar implements Comparable<SimpleCalendar>, Cloneable {
	int day;
	int month;
	int year;

	public SimpleCalendar() {
		this(Calendar.getInstance());
	}

	public SimpleCalendar(Calendar calendar) {
		super();
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH) + 1;
		day = calendar.get(Calendar.DAY_OF_MONTH);
	}

	public SimpleCalendar(Date date) {
		super();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH) + 1;
		day = calendar.get(Calendar.DAY_OF_MONTH);
	}

	public SimpleCalendar(int year, int month, int day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}

	public SimpleCalendar(Lunar lunar) {
		super();

		year = lunar.year;
		month = lunar.month;
		day = lunar.day;
	}

	public SimpleCalendar(String source) {
		super();

		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			date = dateFormat.parse(source);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			year = calendar.get(Calendar.YEAR);
			month = calendar.get(Calendar.MONTH) + 1;
			day = calendar.get(Calendar.DAY_OF_MONTH);
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		SimpleCalendar simpleCalendar = new SimpleCalendar(year, month, day);
		return simpleCalendar;
	}

	@Override
	public int compareTo(SimpleCalendar o) {
		return getTime().compareTo(o.getTime());
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj instanceof SimpleCalendar) {
			SimpleCalendar anotherSimpleCalendar = (SimpleCalendar) obj;
			if (anotherSimpleCalendar.year == year
					&& anotherSimpleCalendar.month == month
					&& anotherSimpleCalendar.day == day)
				return true;
		}
		return false;
	}

	public int getDay() {
		return day;
	}

	public int getMonth() {
		return month;
	}

	public Date getTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(year, month - 1, day);
		return calendar.getTime();
	}

	public int getYear() {
		return year;
	}

	@Override
	public int hashCode() {
		return (int) (getTime().getTime() % Integer.MAX_VALUE);
	}

	public void setDay(int day) {
		this.day = day;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public void setYear(int year) {
		this.year = year;
	}

	// 0为一月 day为可视的值
	@Override
	public String toString() {
		return year + "-" + month + "-" + day;
	}

}
