package com.mobify.astro.plugins;

import android.os.Bundle;
import android.support.annotation.NonNull;

import com.mobify.astro.AstroActivity;
import com.mobify.astro.AstroPlugin;
import com.mobify.astro.PluginResolver;
import com.mobify.astro.dialogs.DialogManager;
import com.mobify.astro.messaging.EventRegistrar;
import com.mobify.astro.messaging.MessageSender;
import com.mobify.astro.messaging.RpcResponse;
import com.mobify.astro.messaging.annotations.AsyncRpcMethod;
import com.mobify.astro.messaging.annotations.RpcMethod;

import org.json.JSONObject;


public class AlertViewPlugin extends AstroPlugin {
    private static final String TAG = AlertViewPlugin.class.getName();

    protected Bundle bundle;

    protected DialogManager dialogManager;
    final int REQUEST_CODE = Math.abs(hashCode());

    static final String KEY_TITLE = "dialog_title";
    static final String KEY_OK_BUTTON = "dialog_okButton";
    static final String KEY_CANCEL_BUTTON = "dialog_cancelButton";
    static final String KEY_MESSAGE = "dialog_message";

    public class AlertViewPluginException extends Exception {
        public AlertViewPluginException(String message) {
            super(TAG + ": " + message);
        }
    }

    public AlertViewPlugin(@NonNull AstroActivity activity, @NonNull PluginResolver pluginResolver,
                           @NonNull EventRegistrar eventRegistrar, @NonNull MessageSender messageSender) {
        super(activity, pluginResolver, eventRegistrar, messageSender);

        dialogManager = activity.getDialogManager();
        bundle = new Bundle();
    }

    /**
     * Sets title of DialogFragment
     */
    @RpcMethod(methodName = "setTitle", parameterNames = {"title"})
    public void setTitle(String title) {
        bundle.putString(KEY_TITLE, title);
    }

    /**
     * Sets text of DialogFragment
     */
    @RpcMethod(methodName = "setText", parameterNames = {"text"})
    public void setText(String text) {
        bundle.putString(KEY_MESSAGE, text);
    }

    /**
     * Add and set text for Positive style button in alert dialog
     * Default label is 'Ok'
     */
    @RpcMethod(methodName = "addOkButton", parameterNames = {"label"})
    public void addOkButton(String label) {
        label = label != null ? label : "alert_ok";
        bundle.putString(KEY_OK_BUTTON, label);
    }

    /**
     * Add and set text for Negative style button in alert dialog
     * Default label is 'Cancel'
     */
    @RpcMethod(methodName = "addCancelButton", parameterNames = {"label"})
    public void addCancelButton(String label) {
        label = label != null ? label : "alert_cancel";
        bundle.putString(KEY_CANCEL_BUTTON, label);
    }

    /**
     * Removes the ok button from the alert dialog
     */
    @RpcMethod(methodName = "removeOkButton")
    public void removeOkButton() {
        bundle.putString(KEY_OK_BUTTON, null);
    }

    /**
     * Removes the cancel button from the alert dialog
     */
    @RpcMethod(methodName = "removeCancelButton")
    public void removeCancelButton() {
        bundle.putString(KEY_CANCEL_BUTTON, null);
    }

    /**
     * Shows the DialogFragment
     */
    @AsyncRpcMethod(methodName = "show", parameterNames = "options")
    public void show(final RpcResponse response, JSONObject options) throws Exception {
        if (bundle.getString(KEY_OK_BUTTON) == null && bundle.getString(KEY_CANCEL_BUTTON) == null) {
            // Android allows no buttons on alertDialog, iOS will crash
            // so force a crash to keep things consistent
            throw new AlertViewPluginException("AlertView must have a button enabled.");
        }

        dialogManager.showAlertDialog(getLocalizedBundle(), this.activity, response, REQUEST_CODE);
    }

    private Bundle getLocalizedBundle() {
        Bundle localizedBundle = new Bundle();
        localizedBundle = addLocalizedStringForKey(KEY_OK_BUTTON, localizedBundle);
        localizedBundle = addLocalizedStringForKey(KEY_CANCEL_BUTTON, localizedBundle);
        localizedBundle = addLocalizedStringForKey(KEY_MESSAGE, localizedBundle);
        localizedBundle = addLocalizedStringForKey(KEY_TITLE, localizedBundle);
        return localizedBundle;
    }

    private Bundle addLocalizedStringForKey(String key, Bundle localizedBundle) {
        if (bundle.getString(key) != null) {
            localizedBundle.putString(key, activity.getLocalizationUtilities().translate(bundle.getString(key)));
        }
        return localizedBundle;
    }
}
