/*
 * 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.util.CalendarViewUtil;
import com.icesoft.faces.facelets.component.util.Util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import java.io.Serializable;
import java.util.Calendar;


/**
 * Simple bean for storing navigation state and other common state
 * information for the 4 calendar screens.  We use constants here to be a
 * little more type safe in the UI code.
 *
 * @author ICEfaces Technologies Inc.
 */
@ApplicationScoped
@ManagedBean(name = "CalViewModel")
public class CalendarViewModel implements Serializable {

    protected final Log logger = LogFactory.getLog(CalendarViewModel.class);

    public static final String DAY_VIEW = "dayView";
    public static final String WEEK_VIEW = "weekView";
    public static final String MONTH_VIEW = "monthView";
    public static final String YEAR_VIEW = "yearView";

    public static final String DAY_VIEW_FILE_NAME = "day-view.xhtml";
    public static final String WEEK_VIEW_FILE_NAME = "week-view.xhtml";
    public static final String MONTH_VIEW_FILE_NAME = "month-view.xhtml";
    public static final String YEAR_VIEW_FILE_NAME = "year-view.xhtml";

    // current date view cursors, for main navigation control.
    protected String viewName;
    protected String fileInclude;

    // date cursor, get at current date cursor.
    protected Calendar dateCursor;

    public CalendarViewModel() {
    }

    private CalendarViewModel(String currentViewName, String currentViewFileInclude) {
        CalendarViewUtil cvu = (CalendarViewUtil) Util.getManagedBean(BeanNames.CALENDAR_VIEW_UTIL);
        dateCursor = cvu.getLocalizedCalendar();
        this.viewName = currentViewName;
        this.fileInclude = currentViewFileInclude;
    }

    public int getCurrentYear() {
        return dateCursor.get(Calendar.YEAR);
    }

    public int getCurrentMonth() {
        return dateCursor.get(Calendar.MONTH);
    }

    public int getCurrentDay() {
        return dateCursor.get(Calendar.DATE);
    }

    /**
     * Factory for creating the four calendar view types, year, month, week
     * and day models.  Primarily used for view specific data.
     *
     * @param viewType view type to create
     * @return a new CalendarViewBean with the property state information.
     */
    public static CalendarViewModel getInstance(final String viewType) {
        if (DAY_VIEW.equals(viewType)) {
            return new CalendarViewModel(DAY_VIEW, DAY_VIEW_FILE_NAME);
        } else if (WEEK_VIEW.equals(viewType)) {
            return new CalendarViewModel(WEEK_VIEW, WEEK_VIEW_FILE_NAME);
        } else if (MONTH_VIEW.equals(viewType)) {
            return new CalendarViewModel(MONTH_VIEW, MONTH_VIEW_FILE_NAME);
        } else if (YEAR_VIEW.equals(viewType)) {
            return new CalendarViewModel(YEAR_VIEW, YEAR_VIEW_FILE_NAME);
        } else {
            return null;
        }
    }

    public Calendar getDateCursor() {
        return dateCursor;
    }

    public void setDateCursor(Calendar dateCursor) {
        this.dateCursor = dateCursor;
    }

    public String getFileInclude() {
        return fileInclude;
    }

    public String getViewName() {
        return viewName;
    }

    public String getDayViewName() {
        return DAY_VIEW;
    }

    public String getWeekViewName() {
        return WEEK_VIEW;
    }

    public String getMonthViewName() {
        return MONTH_VIEW;
    }

    public String getYearViewName() {
        return YEAR_VIEW;
    }
}