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

import java.io.Serializable;
import java.util.List;

/**
 * The Month slot is used by the Month views to aid in building 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 abstract class Slot implements Serializable {

    // slot has at least one event
    protected boolean isEvent;

    // controls for current month
    protected boolean isCurrentMonth;

    // list of calEvents associated with this day.
    protected List calEvents;

    // slot in calendar
    protected int year;
    protected int month;
    protected int day;

    /**
     * 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 Slot(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 Slot(int year, int month, int day, boolean isCurrentMonth) {
        this.year = year;
        this.month = month;
        this.day = day;
        this.isCurrentMonth = isCurrentMonth;
    }

    /**
     * Idicates if this slot contains any events.
     *
     * @return true if one or more events; otherwise, false.
     */
    public boolean isEvent() {
        return isEvent;
    }

    /**
     * Gets the first free slot that an event can be inserted into.
     *
     * @return
     */
    public int getFirstFreeSlot() {
        // look for first spacer opening, and try and fit it in
        int freeSlot = calEvents.size();
        if (freeSlot > 0) {
            for (int i = 0, max = calEvents.size(); i < max; i++) {
                if (((CalendarEventBean) calEvents.get(i)).isSpacer()) {
                    return i;
                }
            }
        }
        return freeSlot;
    }


    /**
     * Add the given event to the specific slot.
     *
     * @param calEvent calendar event.
     * @param slot     slot to insert event into.
     */
    public abstract void addEvent(CalendarEventBean calEvent, int slot);

    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }

    public boolean isCurrentMonth() {
        return isCurrentMonth;
    }

    public List getCalEvents() {
        return calEvents;
    }
}