package com.plugin.openbrowser;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class ChromeClient extends WebChromeClient {
    protected Activity mActivity;

    public interface CustomAlertListener {
        void onPositiveClick(DialogInterface var1, String var2);
    }

    public interface CustomDialogListener {
        void onPositiveClick(DialogInterface var1, String var2);
        void onNegativeClick(DialogInterface var1, String var2);
    }

    public ChromeClient(Activity activity) {
        this.mActivity = activity;
    }

    public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
        showAlert("", message, new CustomAlertListener() {
            public void onPositiveClick(DialogInterface dialog, String data) {
                result.confirm();
                dialog.dismiss();
            }
        });
        return true;
    }

    public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
        showConfirm("", message, new CustomDialogListener() {
            public void onPositiveClick(DialogInterface dialog, String data) {
                result.confirm();
                dialog.dismiss();
            }

            public void onNegativeClick(DialogInterface dialog, String data) {
                result.cancel();
                dialog.dismiss();
            }
        }, true);
        return true;
    }

    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
        return false;
    }

    public void showAlert(final String title, final String message, final CustomAlertListener listener) {
        if (this.mActivity != null) {
            this.mActivity.runOnUiThread(new Runnable() {
                public void run() {
                    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(mActivity);
                    if (title != null && !title.equals("")) {
                        alertBuilder.setTitle(title);
                    }

                    if (message != null && !message.equals("")) {
                        alertBuilder.setMessage(message);
                    }

                    String confirm = mActivity.getString(R.string.ok);
                    alertBuilder.setPositiveButton(confirm, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            if (listener != null) {
                                listener.onPositiveClick(dialog, "");
                            }
                            dialog.dismiss();
                        }
                    });
                    alertBuilder.setCancelable(false);
                    alertBuilder.show();
                }
            });
        }
    }

    public void showConfirm(final String title, final String message, final CustomDialogListener listener, boolean cancelable) {
        if (this.mActivity != null) {
            this.mActivity.runOnUiThread(new Runnable() {
                public void run() {
                    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(mActivity);
                    if (title != null && !title.equals("")) {
                        alertBuilder.setTitle(title);
                    }

                    if (message != null && !message.equals("")) {
                        alertBuilder.setMessage(message);
                    }

                    alertBuilder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            if (listener != null) {
                                listener.onPositiveClick(dialog, "");
                            }
                        }
                    });
                    alertBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                        public void onCancel(DialogInterface dialog) {
                            if (listener != null) {
                                listener.onNegativeClick(dialog, "");
                            }
                        }
                    });
                    alertBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            if (listener != null) {
                                listener.onNegativeClick(dialog, "");
                            }
                        }
                    });
                    alertBuilder.setCancelable(false);
                    alertBuilder.show();
                }
            });
        }
    }
}
