package com.simplicityapks.reminderdatepicker.lib;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * TwinTextItem to be inserted into the ArrayAdapter of the DateSpinner. The date is saved like the DatePicker.
 * The secondary text is currently not in use, so getSecondaryText() returns null.
 */
public class DateItem implements TwinTextItem{

    private final String label;
    private final int day, month, year;

    /**
     * Constructs a new DateItem holding the specified date and a label to show primarily.
     * @param label The string to return when getPrimaryText() is called.
     * @param date The date to be returned by getDate().
     */
    public DateItem(String label, Calendar date) {
        this(label, date.get(Calendar.DAY_OF_MONTH), date.get(Calendar.MONTH), date.get(Calendar.YEAR));
    }

    /**
     * Constructs a new DateItem holding the specified date and a label to to show primarily.
     * @param label The string to return when getPrimaryText() is called.
     * @param day The day of the month.
     * @param month The month of year, zero-indexed (so 11 is December).
     * @param year The year.
     */
    public DateItem(String label, int day, int month, int year) {
        this.label = label;
        this.day = day;
        this.month = month;
        this.year = year;
    }

    /**
     * Gets the current date set in this DateItem.
     * @return A new GregorianCalendar containing the date.
     */
    public Calendar getDate() {
        return new GregorianCalendar(year, month, day);
    }

    /**
     * Gets the day of the month set for this TimeItem.
     * @return The day, as int.
     */
    public int getDay() {
        return this.day;
    }

    /**
     * Gets the month set for this TimeItem.
     * @return The month, as int.
     */
    public int getMonth() {
        return this.month;
    }

    /**
     * Gets the year set for this TimeItem.
     * @return The year, as int.
     */
    public int getYear() {
        return this.year;
    }

    /**
     * Deeply compares this DateItem to the specified Object. Returns true if obj is a DateItem and
     * contains the same date (ignoring the label) or is a Calendar and contains the same date
     * ignoring hour, minute and second.
     * @param obj The Object to compare this to.
     * @return true if equal, false otherwise.
     */
    @Override
    public boolean equals(Object obj) {
        int objDay, objMonth, objYear;
        if(obj instanceof DateItem) {
            DateItem item = (DateItem) obj;
            objDay = item.getDay();
            objMonth = item.getMonth();
            objYear = item.getYear();
        }
        else if(obj instanceof Calendar) {
            Calendar cal = (Calendar) obj;
            objDay = cal.get(Calendar.DAY_OF_MONTH);
            objMonth = cal.get(Calendar.MONTH);
            objYear = cal.get(Calendar.YEAR);
        }
        else return false;
        return objDay==this.day && objMonth==this.month && objYear==this.year;
    }

    @Override
    public CharSequence getPrimaryText() {
        return label;
    }

    @Override
    public CharSequence getSecondaryText() {
        return null;
    }
}
@@ -414,19 +414,20 @@
 nal int 
-day
+year
 , month,
@@ -427,20 +427,19 @@
  month, 
-year
+day
 ;%0A%0A    /
@@ -760,28 +760,20 @@
 alendar.
-DAY_OF_MONTH
+YEAR
 ), date.
@@ -811,20 +811,28 @@
 alendar.
-YEAR
+DAY_OF_MONTH
 ));%0A    
@@ -1028,32 +1028,21 @@
 ram 
-day
+year
  The 
-day of the month
+year
 .%0A  
@@ -1122,37 +1122,48 @@
    * @param 
-year
+day
  The 
-year
+day of the month
 .%0A     */%0A  
@@ -1198,19 +1198,20 @@
 el, int 
-day
+year
 , int mo
@@ -1219,20 +1219,19 @@
 th, int 
-year
+day
 ) %7B%0A    
@@ -1271,17 +1271,19 @@
 his.
-day = day
+year = year
 ;%0A  
@@ -1317,35 +1317,33 @@
        this.
-year = year
+day = day
 ;%0A    %7D%0A%0A   

Switch DateItem parameters to match GregorianCalender