/*
 * 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 com.icesoft.faces.facelets.component.names.BeanNames;
import com.icesoft.faces.facelets.component.schedule.model.CalendarEvent;
import com.icesoft.faces.facelets.component.util.CalendarViewUtil;
import com.icesoft.faces.facelets.component.util.Util;

import java.util.Calendar;

/**
 * The Calendar bean is responsbile for storing UI level flags which help
 * build the different calendar views; year, month, week day.
 * <p/>
 *
 * @author ICEfaces Technologies Inc.
 */
public class CalendarEventBean extends CalendarEvent {

    // marks that the event spans to the next or previous day.
    private boolean isEventSpanNextDay;
    private boolean isEventSpanPreviousDay;

    // marks that the event starts or ends on this day.
    private boolean isEventStart;
    private boolean isEventEnd;

    // marks that the event starts or ends on this minute, hour.
    private boolean isEventMinuteStart;
    private boolean isEventMinuteEnd;

    // spacer, indicates that this instance has no calendar information but
    // is instead used for UI layoout purposes.
    private boolean isSpacer;

    private String localizedStart;
    private String localizedEnd;

    public CalendarEventBean() {
        super();
        isSpacer = true;
    }

    public CalendarEventBean(CalendarEvent event) {
        super(event);
    }

    public void calculateEventSpan(int year, int month, int day, int hour, int minute) {
        calculateEventSpan(year, month, day);
        // calculate start end  as we have hour and minute resolution.
        int startHour = start.get(Calendar.HOUR_OF_DAY);
        int endHour = end.get(Calendar.HOUR_OF_DAY);
        int startMinute = start.get(Calendar.MINUTE);
        int endMinute = end.get(Calendar.MINUTE);

        double startDec = startHour + (startMinute / 60.0);
        double endDec = endHour + (endMinute / 60.0);

        // using a litle voodoo here, if there are further problems consider
        // pushing time into decimal form hour.min/60 to simplify calculation.
        // slot has a start/end hour and minute
        double startSlotDec = hour + (minute / 60.0);
        double endSlotDec = startSlotDec + .5;

        // start and stop calculations, check if start marking is valid
        if (startDec >= startSlotDec && startDec <= endSlotDec) {
            isEventMinuteStart = true;
        }
        // ends tricky as we go up to the line and not past it so we are just shy
        if (endDec >= endSlotDec && endDec <= endSlotDec) {
            isEventMinuteEnd = true;
        }
    }

    public void calculateEventSpan(int year, int month, int day) {

        int startDay = start.get(Calendar.DAY_OF_MONTH);
        int endDay = end.get(Calendar.DAY_OF_MONTH);
        int startMonth = start.get(Calendar.MONTH);
        int endMonth = end.get(Calendar.MONTH);

        // previous day span flag calculation
        if (startDay < day) {
            isEventSpanPreviousDay = true;
        } else if (startDay == day && startMonth < month) {
            isEventSpanPreviousDay = true;
        }
        // next day span flag calculation
        if (endDay > day) {
            isEventSpanNextDay = true;
        } else if (endDay == day && endMonth > month) {
            isEventSpanNextDay = true;
        }

        // start and stop calculations
        if (startDay == day && month == startMonth) {
            isEventStart = true;
        }
        if (endDay == day && month == endMonth) {
            isEventEnd = true;
        }
    }

    public String getStartTime() {
        if (localizedStart == null) {
            CalendarViewUtil cvu = (CalendarViewUtil) Util.getManagedBean(BeanNames.CALENDAR_VIEW_UTIL);
            localizedStart = cvu.getLocalizedTime(start);
        }
        return localizedStart;
    }

    public String getEndTime() {
        if (localizedEnd == null) {
            CalendarViewUtil cvu = (CalendarViewUtil) Util.getManagedBean(BeanNames.CALENDAR_VIEW_UTIL);
            localizedEnd = cvu.getLocalizedTime(end);
        }
        return localizedEnd;
    }

    public boolean isEventSpanNextDay() {
        return isEventSpanNextDay;
    }

    public boolean isEventSpanPreviousDay() {
        return isEventSpanPreviousDay;
    }

    public boolean isEventEnd() {
        return isEventEnd;
    }

    public boolean isEventStart() {
        return isEventStart;
    }

    public boolean isSpacer() {
        return isSpacer;
    }

    public boolean isEventMinuteStart() {
        return isEventMinuteStart;
    }

    public boolean isEventMinuteEnd() {
        return isEventMinuteEnd;
    }
}