/*
 * 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.AbstractCalendarViewController;
import com.icesoft.faces.facelets.component.schedule.common.CalendarEventBean;
import com.icesoft.faces.facelets.component.schedule.common.CalendarViewModel;
import com.icesoft.faces.facelets.component.schedule.model.CalendarEvent;

import javax.faces.event.ActionEvent;
import java.util.Calendar;
import java.util.List;

/**
 * Controller for the month calendar view which is responsible for building
 * the data structure behind the year.  The class also controls how the
 * date cursor is manipulated for this view.
 *
 * @author ICEfaces Technologies Inc.
 */
public class MonthViewController extends AbstractCalendarViewController {

    private MonthModel monthModel;

    public MonthViewController() {
        viewModel =
                CalendarViewModel.getInstance(CalendarViewModel.MONTH_VIEW);
    }

    /**
     * Decrements the calendar view one unit past the current date cursor.
     *
     * @param event jsf action event.
     */
    public void previousCalendar(ActionEvent event) {
        Calendar dateCursor = viewModel.getDateCursor();
        dateCursor.add(Calendar.MONTH, -1);
        refreshModel();
    }

    /**
     * Increments the calendar view one unit past the current date cursor.
     *
     * @param event jsf action event.
     */
    public void nextCalendar(ActionEvent event) {
        Calendar dateCursor = viewModel.getDateCursor();
        dateCursor.add(Calendar.MONTH, 1);
        refreshModel();
    }

    /**
     * Initialized the respective CalendarViewController dataStructure
     */
    public void initialize() {

        // get reference to our viewModel, so we can get the date cursor
        Calendar dateCursor = viewModel.getDateCursor();

        monthModel = new MonthModel(dateCursor.get(Calendar.YEAR),
                dateCursor.get(Calendar.MONTH));

        // ask the service layer for a new list of events.
        List events =
                calendarViewService.getCalendarMonthEvents(dateCursor);

        // add the events to the model. ONLY if they are in the dateCursor year IPCK-537
        for (Object event1 : events) {
            CalendarEvent event = (CalendarEvent) event1;
            monthModel.addEvent(new CalendarEventBean(event), dateCursor);
        }
    }

    /**
     * Disposes the respective CalendarViewController dataStructure
     */
    public void dispose() {
        monthModel = null;
    }

    public MonthModel getMonthModel() {
        return monthModel;
    }
}
