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

import com.icesoft.faces.application.showcase.util.FacesUtils;
import com.icesoft.faces.facelets.component.spinner.DateSpinner;
import com.icesoft.faces.facelets.component.spinner.INumberSpinnerListener;
import com.icesoft.faces.facelets.component.spinner.NumberSpinner;

import javax.faces.event.PhaseId;
import javax.faces.event.ValueChangeEvent;
import java.io.Serializable;
import java.util.Calendar;

public class DateSpinnerExampleBean implements Serializable {

    private DateSpinner spinner;
    private String dateStyle = "default";
    private String type = "both";
    private NumberSpinner incrementSpinner;

    public DateSpinnerExampleBean() {
        this.spinner = new DateSpinner();
        this.incrementSpinner = new NumberSpinner(1, 1, null, new IncrementSpinnerListener());
    }

    class IncrementSpinnerListener implements INumberSpinnerListener {

        public void valueChanged(int oldVal, int newVal) {
            spinner.setIncrementBy(newVal);
        }
    }

    public DateSpinner getSpinner() {
        return spinner;
    }

    public int getDateFieldValue() {
        return Calendar.DATE;
    }

    public int getMonthFieldValue() {
        return Calendar.MONTH;
    }

    public int getYearFieldValue() {
        return Calendar.YEAR;
    }

    public int getHourFieldValue() {
        return Calendar.HOUR;
    }

    public int getMinuteFieldValue() {
        return Calendar.MINUTE;
    }

    public int getSecondFieldValue() {
        return Calendar.SECOND;
    }

    public String getDateStyle() {
        return dateStyle;
    }

    public void setDateStyle(String dateStyle) {
        this.dateStyle = dateStyle;
    }

    public void valueChanged(ValueChangeEvent evt) {
        if (evt.getPhaseId() == PhaseId.INVOKE_APPLICATION) {
            FacesUtils.refreshPage();
        } else {
            evt.setPhaseId(PhaseId.INVOKE_APPLICATION);
            evt.queue();
        }
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public NumberSpinner getIncrementSpinner() {
        return this.incrementSpinner;
    }

}
