/*Copyright 2011 indiandragon

 This file is part of j2me2AB.

 J2meAB is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 j2me2AB is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with j2me2AB.  If not, see <http://www.gnu.org/licenses/>.
 */
package javax.microedition.lcdui;

import javax.microedition.lcdui.Instances;

import android.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;

public class Alert extends Screen {

    public static final Command DISMISS_COMMAND = new Command("OK", Command.OK, 0);
    public static final int FOREVER = -1;
    public static final int DEFAULT_TIMEOUT = 3000;
    private String title;
    private String alertText;
    private Image alertImage;
    private AlertType alertType;
    public static int timeout = 0;
    public Displayable nextdisplayable = null;
    private final int DIALOG_YES_NO_MESSAGE = 1;
    private final int DIALOG_YES_NO_LONG_MESSAGE = 2;
    private final int DIALOG_LIST = 3;
    private final int DIALOG_PROGRESS = 4;
    private final int DIALOG_SINGLE_CHOICE = 5;
    private final int DIALOG_MULTIPLE_CHOICE = 6;
    private final int DIALOG_TEXT_ENTRY = 7;

    public Alert(String title) {
        this(title, null, null, AlertType.INFO);
    }

    public Alert(String title, String alertText, Image alertImage,
            AlertType alertType) {
        this.title = title;
        this.alertText = alertText;
        this.alertImage = alertImage;
        this.alertType = alertType;
    }

    public int getDefaultTimeout() {
        return DEFAULT_TIMEOUT;
    }

    public int getTimeout() {
        return timeout;
    }

    public String getTitle() {
        return title;
    }

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

    public String getString() {
        return alertText;
    }

    public void setString(String alertText) {
        this.alertText = alertText;
    }

    public Image getImage() {
        return alertImage;
    }

    public void setImage(Image alertImage) {
        this.alertImage = alertImage;
    }

    public AlertType getType() {
        return alertType;
    }

    public void setType(AlertType alertType) {
        this.alertType = alertType;
    }

    public void setTimeout(int time) {
        this.timeout = time;
    }

    public void ShowAlert() {

        AlertDialog.Builder alertdialog = new AlertDialog.Builder(
                UIGlobal.APP_CONTEXT);
        alertdialog.setTitle(title);
        alertdialog.setMessage(alertText);
        String strOKText = "";
        Command OKCommand = null;
        String strCancelText = "";
        Command CANCELCommand = null;

        if (this.aListCommands.size() == 2) {
            OKCommand = (Command) this.aListCommands.get(0);
            strOKText = OKCommand.getLabel();
            CANCELCommand = (Command) this.aListCommands.get(1);
            strCancelText = CANCELCommand.getLabel();

        } else if (this.aListCommands.size() == 1) {
            OKCommand = (Command) this.aListCommands.get(0);
            strOKText = OKCommand.getLabel();
            CANCELCommand = null;
            strCancelText = "Cancel";
        } else if (this.aListCommands.size() == 0) {
            strOKText = "OK";
        }

        final Command DialogOKComamnd = OKCommand;
        final Command DialogCANCELComamnd = CANCELCommand;
        
            if (DialogOKComamnd == null) {
            alertdialog.setNeutralButton(strOKText,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            if (nextdisplayable != null) {
                                Instances.getUIInstance().setCurrent( nextdisplayable);
                                
                            }
                            if (commandListener != null) {
                                commandListener.commandAction(Alert.DISMISS_COMMAND,nextdisplayable);
                            }
                        }
                    });
        } else {
            alertdialog.setNeutralButton(strOKText,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            if (commandListener != null) {
                                commandListener.commandAction(DialogOKComamnd,nextdisplayable);
                            } else {
                                if (nextdisplayable != null) {
                                    Instances.getUIInstance().setCurrent(
                                            nextdisplayable);
                                }
                            }
                        }
                    });

            alertdialog.setNegativeButton(strCancelText,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            if (commandListener != null
                                    && DialogCANCELComamnd != null) {
                                commandListener.commandAction(
                                        DialogCANCELComamnd, nextdisplayable);
                            } else {
                                if (nextdisplayable != null) {
                                    Instances.getUIInstance().setCurrent(
                                            nextdisplayable);
                                }
                            }
                        }
                    });
        }
        alertdialog.show();

    }

    public Displayable getNextdisplayable() {
        return nextdisplayable;
    }

    public void setNextdisplayable(Displayable nextdisplayable) {
        this.nextdisplayable = nextdisplayable;
    }
}
