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 PromptViewPlugin extends AstroPlugin {
    private static final String TAG = AlertViewPlugin.class.getName();

    protected DialogManager dialogManager;
    protected Bundle bundle;

    final int REQUEST_CODE = Math.abs(hashCode());

    public static final String KEY_PROMPT = "prompt_prompt";
    public static final String KEY_CANCEL_BUTTON = "prompt_cancelButton";
    public static final String KEY_SUBMIT_BUTTON = "prompt_submitButton";
    public static final String KEY_PLACEHOLDER = "prompt_placeholder";

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

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

        bundle.putString(KEY_SUBMIT_BUTTON, "prompt_submit");
        bundle.putString(KEY_CANCEL_BUTTON, "prompt_cancel");
    }

    /**
     * Sets the prompt of the popup
     */
    @RpcMethod(methodName = "setPrompt", parameterNames = {"prompt"})
    public void setPrompt(String prompt) {
        bundle.putString(KEY_PROMPT, prompt);
    }

    /**
     * Sets the text of the submit button of the popup
     */
    @RpcMethod(methodName = "setCancelButtonLabel", parameterNames = {"label"})
    public void setCancelButtonLabel(String label) {
        bundle.putString(KEY_CANCEL_BUTTON, label);
    }

    /**
     * Sets the text of the submit button of the popup
     */
    @RpcMethod(methodName = "setSubmitButtonLabel", parameterNames = {"label"})
    public void setSubmitButtonLabel(String label) {
        bundle.putString(KEY_SUBMIT_BUTTON, label);
    }

    /**
     * Sets the placeholder text of the popup
     */
    @RpcMethod(methodName = "setPlaceholder", parameterNames = {"placeholder"})
    public void setPlaceholder(String placeholder) {
        bundle.putString(KEY_PLACEHOLDER, placeholder);
    }

    /**
     * Shows the PromptView
     */
    @AsyncRpcMethod(methodName = "show", parameterNames = "options")
    public void show(final RpcResponse response, JSONObject options) throws Exception {
        dialogManager.showPromptView(getLocalizedBundle(), this.activity, response, REQUEST_CODE);
    }

    protected Bundle getLocalizedBundle() {
        Bundle localizedBundle = new Bundle();
        localizedBundle = addLocalizedStringForKey(KEY_PROMPT, localizedBundle);
        localizedBundle = addLocalizedStringForKey(KEY_CANCEL_BUTTON, localizedBundle);
        localizedBundle = addLocalizedStringForKey(KEY_SUBMIT_BUTTON, localizedBundle);
        localizedBundle = addLocalizedStringForKey(KEY_PLACEHOLDER, 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;
    }
}
