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

import com.icesoft.faces.facelets.component.names.BeanNames;
import com.icesoft.faces.facelets.component.schedule.common.CalendarEventBean;
import com.icesoft.faces.facelets.component.schedule.common.Slot;
import com.icesoft.faces.facelets.component.util.CalendarViewUtil;
import com.icesoft.faces.facelets.component.util.Util;

import java.util.ArrayList;
import java.util.GregorianCalendar;

/**
 * The Day slot is used by the Month views to aid in build in the UI.  The
 * main function point is that each slot contains a list of CalendarEventBean
 * for the given day.
 *
 * @author ICEfaces Technologies Inc.
 */
public class DaySlot extends Slot {

    private int hour;
    private String formattedHour;
    private String formatted24Hour;
    private int minute;
    private String midnight;

    /**
     * Creates a day 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 hour   hours that the slot represents
     * @param minute minutes in the hour that the slot represents
     */
    public DaySlot(int year, int month, int day, int hour, int minute) {
        this(year, month, day, hour, minute, false);
    }

    /**
     * Creates a day 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 hour           hours that the slot represents
     * @param minute         minutes in the hour that the slot represents
     * @param isCurrentMonth true indicates Month slot is in current month; false
     *                       it is either in the previous or next month.
     */
    public DaySlot(int year, int month, int day, int hour, int minute, boolean isCurrentMonth) {
        super(year, month, day, isCurrentMonth);
        this.hour = hour;
        this.minute = minute;


        formattedHour = hour == 0 || hour == 12 ? "12" : String.valueOf(hour % 12);
        formatted24Hour = hour < 10 ? "0" + String.valueOf(hour) : String.valueOf(hour);

        CalendarViewUtil cvu = (CalendarViewUtil) Util.getManagedBean(BeanNames.CALENDAR_VIEW_UTIL);
        GregorianCalendar temp = new GregorianCalendar(cvu.getScheduleTimeZone());
        temp.set(year, month, day, hour, minute);
        midnight = cvu.getLocalizedMidnight(temp);

        // create storage for our events, mainly all day or time slotted.
        calEvents = new ArrayList();
    }

    /**
     * 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, hour, minute);

        // 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);
        }
    }

    public int getHour() {
        return hour;
    }

    public String getFormattedHour() {
        return formattedHour;
    }

    public int getMinute() {
        return minute;
    }

    public String getMidnight() {
        return midnight;
    }

    public String getFormatted24Hour() {
        return formatted24Hour;
    }
}