package com.mobify.astro.plugins;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.View;

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.annotations.RpcMethod;

import org.json.JSONObject;

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

    protected Integer backgroundColor = Color.WHITE;
    final int REQUEST_CODE = Math.abs(hashCode());

    protected DialogManager dialogManager;
    protected String viewPluginAddress;
    protected View contentView;

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

        dialogManager = activity.getDialogManager();
    }

    protected boolean extractAnimatedOption(JSONObject options) throws Exception {
        String animated = "animated";
        boolean shouldAnimate = false;
        if (options != null && options.has(animated)) {
            shouldAnimate = options.getBoolean(animated);
        }
        return shouldAnimate;
    }

    protected void setBackgroundColorOnView() {
        if (contentView != null && backgroundColor != null) {
            contentView.setBackgroundColor(backgroundColor);
        }
    }

    /**
     * Sets a view to be displayed in an activity
     * @param address the address of the view being rendered in the activity
     */
    @RpcMethod(methodName = "setContentView", parameterNames = {"address"})
    public void setContentView(String address) {
        // Grab plugin manager singleton
        viewPluginAddress = address;

        contentView = pluginResolver.instanceForAddress(address).getView();
        setBackgroundColorOnView();

        dialogManager.updateView(REQUEST_CODE, contentView);
    }

    /**
     * Sets the background color for the Modal View
     * @param colorString  A Hex color string, e.g. '#448888'
     */
    @RpcMethod(methodName = "setBackgroundColor", parameterNames = "color")
    public void setBackgroundColor(String colorString) {
        // validate by parsing, if this throws it should report an error to JS
        backgroundColor =  Color.parseColor(colorString);
        setBackgroundColorOnView();
        dialogManager.updateView(REQUEST_CODE, contentView);
    }

    /**
     * Show the modal activity
     * @param options options dictionary - we handle an animated flag
     */
    @RpcMethod(methodName = "show", parameterNames = "options")
    public boolean show(JSONObject options) throws Exception {
        boolean animated = extractAnimatedOption(options);

        dialogManager.showModal(contentView, REQUEST_CODE, animated);

        return true;
    }

    /**
     * Hides the activity
     * @param options options dictionary - we handle an animated flag
     */
    @RpcMethod(methodName = "hide", parameterNames = "options")
    public boolean hide(JSONObject options) throws Exception {
        activity.hideKeyboard();
        dialogManager.removeModal(REQUEST_CODE);
        return true;
    }
}
