package com.mobify.astro.plugins;

import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.mobify.astro.AstroActivity;
import com.mobify.astro.AstroPlugin;
import com.mobify.astro.PluginResolver;
import com.mobify.astro.messaging.EventRegistrar;
import com.mobify.astro.messaging.Exceptions;
import com.mobify.astro.messaging.MessageSender;
import com.mobify.astro.messaging.RpcResponse;
import com.mobify.astro.messaging.annotations.AsyncRpcMethod;
import com.mobify.astro.utilities.DrawableUriResolver;

import java.io.IOException;

public class ImageViewPlugin extends AstroPlugin {

    private static final String TAG = ImageViewPlugin.class.getName();
    protected ImageView imageView;

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

        imageView = setUpImageView();
    }

    @Override
    public View getView() {
        return imageView;
    }

    // Instantiate image view
    ImageView setUpImageView() {
        // Set up an image view with Layout settings but no bitmap image
        ImageView imageView = new ImageView(activity);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        imageView.setLayoutParams(layoutParams);
        imageView.setScaleType(ImageView.ScaleType.CENTER);

        return imageView;
    }

    DrawableUriResolver.DrawableRunnable createCompletionRunnable(final RpcResponse response) {
        return new DrawableUriResolver.DrawableRunnable() {
            @Override
            public void run(@Nullable Drawable drawable, @Nullable Exception error) {
                if (error != null) {
                    Log.e(TAG, error.getMessage(), error);
                    messageSender.sendRpcResponseWithError(response, error);
                    return;
                }

                imageView.setImageDrawable(drawable);
                try {
                    messageSender.sendRpcResponse(response);
                } catch (Exceptions.DuplicateRpcResponseSend duplicateRpcResponseSendException) {
                    Log.e(TAG, "This response was already sent. Cannot send a response more than once.",
                            duplicateRpcResponseSendException);
                    messageSender.sendRpcResponseWithError(response, duplicateRpcResponseSendException);
                }
            }
        };
    }

    /**
     * Sets the image to be displayed, it will fill the screen horizontally and vertically.
     * @param path the path, path names are specified using the AssetManager specifications
     * @throws java.io.IOException
     */
    @AsyncRpcMethod(methodName = "setImagePath", parameterNames = "path")
    public void setImagePath(final RpcResponse response, String path) throws IOException {
        Uri uri = Uri.parse(path);
        DrawableUriResolver.DrawableRunnable completionRunnable = createCompletionRunnable(response);

        DrawableUriResolver drawableUriResolver = new DrawableUriResolver(activity);
        drawableUriResolver.getDrawable(uri, completionRunnable);
    }
}
