package cnp.ew.util;
import java.util.*;

/**
 * Provides an observable wrapper for a CpDate object.
 * Any aspects (day, month, year, hours, minutes, seconds)
 * that change are notified discretely; the different facets
 * are iterated below.
 */
public class CpDateModel extends CpDefaultObservable
{
    public static final int CHANGED_HOURS=0;
    public static final int CHANGED_MINUTES=1;
    public static final int CHANGED_SECONDS=2;
    public static final int CHANGED_DAY=3;
    public static final int CHANGED_MONTH=4;
    public static final int CHANGED_YEAR=5;

    // oldDate is used to determine what aspects of date have changed.
    CpDate date, oldDate;

    public String toString()
    {
        return "CpDateModel on " + date.toString();
    }


    public CpDateModel()
    {
        super();
    }

    public CpDateModel(CpDate newDate)
    {
        super();
        date = newDate;
    }

    public void setDate(CpDate newDate)
    {
        saveOldDate();
        date = newDate;
        notifyIfNecessary();
    }

    public CpDate getDate()
    {
        return date;
    }

    public void setHours(int hours)
    {
        saveOldDate();
        date.setHours(hours);
        notifyIfNecessary();
    }

    public void incrementHours()
    {
        saveOldDate();
        date.incrementHours();
        notifyIfNecessary();
    }

    public void decrementHours()
    {
        saveOldDate();
        date.decrementHours();
        notifyIfNecessary();
    }

    public int getHours()
    {
        return date.getHours();
    }


    public void setMinutes(int minutes)
    {
        saveOldDate();
        date.setMinutes(minutes);
        notifyIfNecessary();
    }

    public int getMinutes()
    {
        return date.getMinutes();
    }

    public void incrementMinutes()
    {
        saveOldDate();
        date.incrementMinutes();
        notifyIfNecessary();
    }

    public void decrementMinutes()
    {
        saveOldDate();
        date.decrementMinutes();
        notifyIfNecessary();
    }

    public void setMonth(int month)
    {
        saveOldDate();
        date.setMonth(month);
        notifyIfNecessary();
    }

    public int getMonth()
    {
        return date.getMonth();
    }

    public void incrementMonth()
    {
        saveOldDate();
        date.incrementMonth();
        notifyIfNecessary();
    }

    public void decrementMonth()
    {
        saveOldDate();
        date.decrementMonth();
        notifyIfNecessary();
    }

    public void setSeconds(int seconds)
    {
        saveOldDate();
        date.setSeconds(seconds);
        notifyIfNecessary();
    }

    public int getSeconds()
    {
        return date.getSeconds();
    }


    public void incrementSeconds()
    {
        saveOldDate();
        date.incrementSeconds();
        notifyIfNecessary();
    }

    public void decrementSeconds()
    {
        saveOldDate();
        date.decrementSeconds();
        notifyIfNecessary();
    }

    public void setTime(int time)
    {

        saveOldDate();
        date.setTime(time);
        notifyIfNecessary();
    }

    public void setYear(int year)
    {
        saveOldDate();
        date.setYear(year);
        notifyIfNecessary();
    }

    public int getYear()
    {
        return date.getYear();
    }

    public void incrementYear()
    {
        saveOldDate();
        date.incrementYear();
        notifyIfNecessary();
    }

    public void decrementYear()
    {
        saveOldDate();
        date.decrementYear();
        notifyIfNecessary();
    }

    public void setDay(int day)
    {
        saveOldDate();
        date.setDate(day);
        notifyIfNecessary();
    }

    public int getDay()
    {
        return date.getDate();
    }

    public void incrementDay()
    {
        saveOldDate();
        date.incrementDay();
        notifyIfNecessary();
    }

    public void decrementDay()
    {
        saveOldDate();
        date.decrementDay();
        notifyIfNecessary();
    }

    void saveOldDate()
    {
        if (date == null) {
            oldDate = null;
        } else {
            oldDate = new CpDate(date);
        }
    }

    void notifyIfNecessary()
    {
        if (oldDate == null || oldDate.getHours() != date.getHours()) {
            notifyObservers(CHANGED_HOURS);
        }

        if (oldDate == null || oldDate.getMinutes() != date.getMinutes()) {
            notifyObservers(CHANGED_MINUTES);
        }

        if (oldDate == null || oldDate.getSeconds() != date.getSeconds()) {
            notifyObservers(CHANGED_SECONDS);
        }

        if (oldDate == null || oldDate.getDate() != date.getDate()) {
            notifyObservers(CHANGED_DAY);
        }

        if (oldDate == null || oldDate.getMonth() != date.getMonth()) {
            notifyObservers(CHANGED_MONTH);
        }

        if (oldDate == null || oldDate.getYear() != date.getYear()) {
            notifyObservers(CHANGED_YEAR);
        }
    }

}