/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */
package com.icesoft.faces.facelets.component.schedule.year;

import com.icesoft.faces.facelets.component.schedule.common.CalendarEventBean;
import com.icesoft.faces.facelets.component.schedule.month.MonthModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * The YearModel is designed to aid the UI in building the year views.  This
 * class is pretty simple in that it basically just delegates to the month
 * model for building its data structure.
 * <p/>
 *
 * @author ICEfaces Technologies Inc.
 */
public class YearModel implements Serializable {

    // month models that make up the year.
    private List months;

    // what year am I.
    private int currentYear;

    /**
     * Construction a Year model using the number of months that come back
     * from the JDK for the given Locale.
     *
     * @param year
     */
    public YearModel(final int year) {
        // create array to contain a years worth of calendars, a little work
        Calendar tmp = new GregorianCalendar(year, Calendar.JANUARY, 1);
        int maxMonths = tmp.getMaximum(Calendar.MONTH);
        months = new ArrayList(maxMonths);

        // create  month objects
        for (int month = Calendar.JANUARY; month <= maxMonths; month++) {
            months.add(new MonthModel(year, month));
        }
        // keep year info.
        currentYear = year;
    }

    /**
     * Gets the 12ish months associated with this year.
     *
     * @return list of month models for the year.
     */
    public List getMonths() {
        return months;
    }

    /**
     * Adds the given calEvent to the YearModel. This event will only add events
     * that fall in the year specified by the class.
     *
     * @param calEvent event to add to the model.
     */
    public void addEvent(CalendarEventBean calEvent) {
        // iterate over range adding each even to the respective months.
        int currentMonth;
        if (currentYear >= calEvent.getStart().get(Calendar.YEAR) &&
                currentYear <= calEvent.getEnd().get(Calendar.YEAR)) {
            System.out.println(" currentYear="+currentYear+" is within bounds, so will be added to month model");
            for (Object month : months) {
                MonthModel monthModel = (MonthModel) month;
                currentMonth = monthModel.getCurrentMonth().get(Calendar.MONTH);
                if (currentMonth == calEvent.getStart().get(Calendar.MONTH) ||
                        currentMonth == calEvent.getEnd().get(Calendar.MONTH)) {
                    Calendar tmp = new GregorianCalendar(currentYear, Calendar.JANUARY, 1);
                    monthModel.addEvent(calEvent, tmp);
                }
            }
        }
    }
}