/*
 * 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.month;

import com.icesoft.faces.facelets.component.schedule.common.CalendarEventBean;
import com.icesoft.faces.facelets.component.schedule.common.Slot;

import java.util.ArrayList;

/**
 * The Month slot is used by the Month views to aid in building in the UI.  The
 * main function point is that each slot contains a list of CalendarEventBean
 * for the given day.  The name Month slot is misleading in that it represents
 * a day in a month not the month.  A similar bean DaySlot is used for the week
 * and day views.
 *
 * @author ICEfaces Technologies Inc.
 */
public class MonthSlot extends Slot {

    /**
     * Creates a month slot that is defaulted to a state as not current month.
     * The UI uses this important information for styling current, prev, and
     * next days that might be in a given calendar view.
     *
     * @param year  current year the day represents
     * @param month current month the day represents
     * @param day   actual day that is displayed.
     */
    public MonthSlot(int year, int month, int day) {
        this(year, month, day, false);
    }

    /**
     * Creates a month slot that is defaulted to a state as not current month.
     * The UI uses this important information for styling current, prev, and
     * next days that might be in a given calendar view.
     *
     * @param year           current year the day represents
     * @param month          current month the day represents
     * @param day            actual day that is displayed.
     * @param isCurrentMonth true indicates Month slot is in current month; false
     *                       it is either in the previous or next month.
     */
    public MonthSlot(int year, int month, int day, boolean isCurrentMonth) {
        super(year, month, day, isCurrentMonth);
        calEvents = new ArrayList<CalendarEventBean>(10);
    }

    /**
     * Add the given event to the specific slot.
     *
     * @param calEvent calendar event.
     * @param slot     slot to insert event into.
     */
    public void addEvent(CalendarEventBean calEvent, int slot) {
        // toggle float on isEvent, we have one
        isEvent = true;

        // calculate spans,
        calEvent.calculateEventSpan(year, month, day);

        // add spaces slots.
        if (calEvents.size() < slot) {
            for (int i = calEvents.size(); i < slot; i++) {
                calEvents.add(new CalendarEventBean());
            }
        }

        // add event to list of events, be careful, it's a list not array.
        if (slot < calEvents.size()) {
            calEvents.set(slot, calEvent);
        } else {
            calEvents.add(calEvent);
        }
    }

}