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

import com.icesoft.faces.facelets.component.dialog.Dialog;
import com.icesoft.faces.facelets.component.dialog.DialogState;
import com.icesoft.faces.facelets.component.dialog.DialogTypes;
import com.icesoft.faces.facelets.component.names.BeanNames;
import com.icesoft.faces.facelets.component.util.Command;
import com.icesoft.faces.facelets.component.util.Util;

import javax.faces.event.ActionEvent;
import java.io.Serializable;

public class DialogExampleBean implements Serializable {

    // The following field provides GUI evidence of the Command firing.
    private String commandFired = "";

    public String getCommandFired() {
        return commandFired;
    }

    public void setCommandFired(String commandFired) {
        this.commandFired = commandFired;
    }

    /**
     * ActionListener demonstrating configuration of the ice-cc:dialog to an "OK"
     * Dialog.
     *
     * @param event ActionEvent
     */
    public void okayActionListener(ActionEvent event) {

        displayOKDialog("The Okay Dialog",
                "Informational Dialog, no Commands fired.",
                null);
    }

    /**
     * ActionListener demonstrating configuration of the ice-cc:dialog to an
     * "Yes/No/Cancel" Dialog.
     *
     * @param event ActionEvent
     */
    public void yesNoCancelActionListener(ActionEvent event) {

        setCommandFired("");

        Command yesCommand = new Command() {
            public void execute() {
                setCommandFired("Yes Button!");
            }
        };

        Command noCommand = new Command() {
            public void execute() {
                setCommandFired("No Button!");
            }
        };

        displayYesNoCancelDialog("The Yes/No/Cancel Dialog",
                "Fire the Yes Command, No Command, or Cancel.",
                yesCommand, noCommand);
    }

    /**
     * ActionListener demonstrating configuration of the ice-cc:dialog to an
     * "Error" Dialog.
     *
     * @param event ActionEvent
     */
    public void errorActionListener(ActionEvent event) {
        try {
            throw new UnsupportedOperationException("Exception Message.");
        } catch (UnsupportedOperationException uoe) {
            displayErrorDialog("Error Dialog",
                    "Application Message.",
                    uoe.getMessage(), null);
        }

    }

    /**
     * Utility method to display a general dialog with a selected title
     * and message.
     *
     * @param title   title of dialog
     * @param message message on dialog
     * @param command command to execute if the OK button is selected.
     */
    protected void displayOKDialog(String title,
                                   String message,
                                   Command command) {
        // get reference to the dialog controller
        DialogState dialogState = (DialogState) Util.getManagedBean(
                BeanNames.DIALOG_STATE);
        DialogTypes dialogTypes = (DialogTypes) Util.getManagedBean(
                BeanNames.DIALOG_TYPES);
        Dialog dialog = (Dialog) dialogTypes.get(ApplicationDialogTypes.BASIC_DIALOG);
        dialogState.setCurrentDialog(dialog);

        dialog.setOptionType(Dialog.OKAY_OPTION);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setOKButtonCommand(command);
        dialog.setRendered(true);
    }

    /**
     * Utility method to display a general dialog with a selected title
     * and message.
     *
     * @param title   title of dialog
     * @param message message on dialog
     * @param command command to execute if the OK button is selected.
     */
    protected void displayOKCancelDialog(String title,
                                         String message,
                                         Command command) {
        // get reference to the dialog controller
        DialogState dialogState = (DialogState) Util.getManagedBean(
                BeanNames.DIALOG_STATE);
        DialogTypes dialogTypes = (DialogTypes) Util.getManagedBean(
                BeanNames.DIALOG_TYPES);
        Dialog dialog = (Dialog) dialogTypes.get(ApplicationDialogTypes.BASIC_DIALOG);
        dialogState.setCurrentDialog(dialog);

        dialog.setOptionType(Dialog.OK_CANCEL_OPTION);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setOKButtonCommand(command);
        dialog.setRendered(true);
    }

    /**
     * Utility method to display a general yes, no and cancel dialog with
     * a selected title and message.
     *
     * @param title      title of dialog
     * @param message    message to display to user.
     * @param yesCommand executed command if yes button is pressed.
     * @param noCommand  executed command if no button is pressed.
     */
    protected void displayYesNoCancelDialog(String title,
                                            String message,
                                            Command yesCommand,
                                            Command noCommand) {
        // get reference to the dialog controller
        DialogState dialogState = (DialogState) Util.getManagedBean(
                BeanNames.DIALOG_STATE);
        DialogTypes dialogTypes = (DialogTypes) Util.getManagedBean(
                BeanNames.DIALOG_TYPES);
        Dialog dialog = (Dialog) dialogTypes.get(ApplicationDialogTypes.BASIC_DIALOG);
        dialogState.setCurrentDialog(dialog);

        dialog.setOptionType(Dialog.YES_NO_CANCEL_OPTION);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setYesButtonCommand(yesCommand);
        dialog.setNoButtonCommand(noCommand);
        dialog.setRendered(true);
    }

    /**
     * Utility method to display a general error dialog with okay button
     * and two messages (typically an application generated message and the
     * Exception message).
     *
     * @param message     initial message in dialog
     * @param message2    expanded error message
     * @param okayCommand executed command when okay is pressed.
     */
    protected void displayErrorDialog(String title,
                                      String message,
                                      String message2,
                                      Command okayCommand) {
        DialogState dialogState = (DialogState) Util.getManagedBean(BeanNames.DIALOG_STATE);
        DialogTypes dialogTypes = (DialogTypes) Util.getManagedBean(
                BeanNames.DIALOG_TYPES);
        Dialog dialog = (Dialog) dialogTypes.get(ApplicationDialogTypes.GENERAL_ERROR);
        dialogState.setCurrentDialog(dialog);

        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setMessage2(message2);
        dialog.setOKButtonCommand(okayCommand);
        dialog.setRendered(true);
    }


}