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

import com.icesoft.faces.facelets.component.util.Command;

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

public class Dialog implements Serializable {
    /**
     * Creates a dialog which has just a OK button.
     */
    public static final int OKAY_OPTION = 1;
    /**
     * Creates a dialog which has Yes and No button.
     */
    public static final int YES_NO_OPTION = 2;
    /**
     * Creates a dialog which has a Yes, No and Cancel option.
     */
    public static final int YES_NO_CANCEL_OPTION = 3;
    /**
     * Creates a dialog which has an OK and Cancel option.
     */
    public static final int OK_CANCEL_OPTION = 4;

    // option type
    protected int optionType;
    // title for dialog window
    protected String title;
    // Message for dialog body.
    protected String message;
    // Message for dialog body.
    protected String message2;
    // show hide the dialog
    protected boolean rendered;

    // dynamic include for custom content
    protected String include;
    protected String headerInclude;

    protected String cssStyle;

    private boolean isOkButtonVisible = true;
    private boolean isYesButtonVisible = true;
    private boolean isNoButtonVisible = true;
    private boolean isCancelButtonVisible = true;

    protected Command yesCommand;
    protected Command okCommand;
    protected Command noCommand;
    protected Command cancelCommand;

    public Dialog() {
    }

    /**
     * Creates a new Dialog which will not be visible by default.
     *
     * @param include    include path to dialog content
     * @param optionType option type
     */
    public Dialog(String include, int optionType) {
        this.include = include;
        setOptionType(optionType);
        rendered = false;
    }

    /**
     * Creates a new Dialog which will not be visible by default.
     *
     * @param include       include path to dialog content
     * @param headerInclude include path to dialog header content
     * @param optionType    option type
     */
    public Dialog(String include, String headerInclude, int optionType) {
        this.include = include;
        this.headerInclude = headerInclude;
        setOptionType(optionType);
        rendered = false;
    }

    /**
     * Sets the command which will be executed when the OK button is pressed.
     *
     * @param command command to execute.
     */
    public void setOKButtonCommand(Command command) {
        okCommand = command;
    }

    /**
     * Sets the command which will be executed when the yes button is pressed.
     *
     * @param command command to execute.
     */
    public void setYesButtonCommand(Command command) {
        yesCommand = command;
    }

    /**
     * Sets the command which will be executed when the no button is pressed.
     *
     * @param command command to execute.
     */
    public void setNoButtonCommand(Command command) {
        noCommand = command;
    }

    /**
     * Sets the command which will be executed when the cancel button is pressed.
     *
     * @param command command to execute.
     */
    public void setCancelButtonCommand(Command command) {
        cancelCommand = command;
    }

    /**
     * Default behavior is to close the dialog when it is either manually closed
     * or the OK button has been pressed.
     *
     * @param event jsf action event.
     */
    public void okButtonAction(ActionEvent event) {
        rendered = false;
        if (okCommand != null) {
            okCommand.execute();
        }
    }


    public void yesButtonAction(ActionEvent event) {
        rendered = false;
        if (yesCommand != null) {
            yesCommand.execute();
        }
    }

    public void noButtonAction(ActionEvent event) {
        rendered = false;
        if (noCommand != null) {
            noCommand.execute();
        }
    }

    public void cancelButtonAction(ActionEvent event) {
        rendered = false;
        if (cancelCommand != null) {
            cancelCommand.execute();
        }
    }

    public boolean isRendered() {
        return rendered;
    }

    public void setRendered(boolean rendered) {
        this.rendered = rendered;
    }

    public int getOptionType() {
        return optionType;
    }

    public void setOptionType(int optionType) {
        this.optionType = optionType;
        // set up the correct icon
        if (optionType == OKAY_OPTION) {
            isOkButtonVisible = true;
            isYesButtonVisible = false;
            isNoButtonVisible = false;
            isCancelButtonVisible = false;
        } else if (optionType == OK_CANCEL_OPTION) {
            isOkButtonVisible = true;
            isYesButtonVisible = false;
            isNoButtonVisible = false;
            isCancelButtonVisible = true;
        } else if (optionType == YES_NO_OPTION) {
            isOkButtonVisible = false;
            isYesButtonVisible = true;
            isNoButtonVisible = true;
            isCancelButtonVisible = false;
        } else if (optionType == YES_NO_CANCEL_OPTION) {
            isOkButtonVisible = false;
            isYesButtonVisible = true;
            isNoButtonVisible = true;
            isCancelButtonVisible = true;
        }
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getInclude() {
        return include;
    }

    public void setInclude(String include) {
        this.include = include;
    }

    public String getHeaderInclude() {
        return headerInclude;
    }

    public void setHeaderInclude(String headerInclude) {
        this.headerInclude = headerInclude;
    }

    public String getMessage2() {
        return message2;
    }

    public void setMessage2(String message2) {
        this.message2 = message2;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public boolean isCancelButtonVisible() {
        return isCancelButtonVisible;
    }

    public void setCancelButtonVisible(boolean cancelButtonVisible) {
        isCancelButtonVisible = cancelButtonVisible;
    }

    public boolean isOkButtonVisible() {
        return isOkButtonVisible;
    }

    public void setOkButtonVisible(boolean okButtonVisible) {
        isOkButtonVisible = okButtonVisible;
    }

    public boolean isYesButtonVisible() {
        return isYesButtonVisible;
    }

    public void setYesButtonVisible(boolean yesButtonVisible) {
        isYesButtonVisible = yesButtonVisible;
    }

    public boolean isNoButtonVisible() {
        return isNoButtonVisible;
    }

    public void setNoButtonVisible(boolean noButtonVisible) {
        isNoButtonVisible = noButtonVisible;
    }


    public String getCssStyle() {
        return cssStyle;
    }

    public void setCssStyle(String cssStyle) {
        this.cssStyle = cssStyle;
    }
}