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

import com.icesoft.faces.facelets.component.select.SelectInputCalendar;
import com.icesoft.faces.facelets.component.select.SelectInputTime;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class SelectDateIntervalExampleBean implements Serializable {

    SelectInputCalendar startCalendar = new SelectInputCalendar();
    SelectInputCalendar endCalendar = new SelectInputCalendar();

    SelectInputTime startCalendarWithTime = new SelectInputTime(TimeZone.getTimeZone("UTC"));
    SelectInputTime endCalendarWithTime = new SelectInputTime(TimeZone.getTimeZone("UTC"));

    public void validate(ActionEvent e) {

        // VALIDATION OF FIRST COMPONENT
        // Generate the current date to compare with the start date
        GregorianCalendar calendar = new GregorianCalendar(startCalendar.getCalendar().getTimeZone());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date ourCurrentDay = calendar.getTime();

        if (startCalendar.getCalendar().getTime().before(ourCurrentDay)) {
            String message = "Top component start date is before the current date";
            FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message);
            FacesContext facesContext = FacesContext.getCurrentInstance();
            String clientId = null; // this is a global message
            facesContext.addMessage(clientId, facesMessage);
            return;
        }
        if (startCalendar.getCalendar().getTime().after(endCalendar.getCalendar().getTime())) {
            String message = "Top component start date is after the end date";
            FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message);
            FacesContext facesContext = FacesContext.getCurrentInstance();
            String clientId = null; // this is a global message
            facesContext.addMessage(clientId, facesMessage);
            return;
        }

        // VALIDATION OF SECOND COMPONENT
        // Generate the current date to compare with the start date.  We do this
        // a second time because the tags can (and do) have different timezones.
        calendar = new GregorianCalendar(startCalendarWithTime.getCalendar().getTimeZone());
        ourCurrentDay = calendar.getTime();

        if (startCalendarWithTime.getCalendar().getTime().before(ourCurrentDay)) {
            String message = "Bottom component start date is before the current date";
            FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message);
            FacesContext facesContext = FacesContext.getCurrentInstance();
            String clientId = null; // this is a global message
            facesContext.addMessage(clientId, facesMessage);
            return;
        }
        if (startCalendarWithTime.getCalendar().getTime().after(endCalendarWithTime.getCalendar().getTime())) {
            String message = "Bottom component start date is after the end date";
            FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message);
            FacesContext facesContext = FacesContext.getCurrentInstance();
            String clientId = null; // this is a global message
            facesContext.addMessage(clientId, facesMessage);
            return;
        }

        // To keep the demo as simple as possible, we are using the messages
        // component to output our valid message as well.  In your application
        // if validation passes, there will likely be a change in the view.
        String message = "Valid";
        FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message);
        FacesContext facesContext = FacesContext.getCurrentInstance();
        String clientId = null; // this is a global message
        facesContext.addMessage(clientId, facesMessage);
    }

    public SelectInputCalendar getStartCalendar() {
        return startCalendar;
    }

    public void setStartCalendar(SelectInputCalendar startCalendar) {
        this.startCalendar = startCalendar;
    }

    public SelectInputCalendar getEndCalendar() {
        return endCalendar;
    }

    public void setEndCalendar(SelectInputCalendar endCalendar) {
        this.endCalendar = endCalendar;
    }

    public SelectInputTime getStartCalendarWithTime() {
        return startCalendarWithTime;
    }

    public void setStartCalendarWithTime(SelectInputTime startCalendarWithTime) {
        this.startCalendarWithTime = startCalendarWithTime;
    }

    public SelectInputTime getEndCalendarWithTime() {
        return endCalendarWithTime;
    }

    public void setEndCalendarWithTime(SelectInputTime endCalendarWithTime) {
        this.endCalendarWithTime = endCalendarWithTime;
    }
}
