package com.mobify.astro.plugins.headerbarplugin;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;

import com.mobify.astro.AstroPlugin;
import com.mobify.astro.PluginResolver;
import com.mobify.astro.utilities.DrawableUriResolver;

import org.json.JSONException;
import org.json.JSONObject;

public class HeaderContentItem {
    public static final String TAG = HeaderContentItem.class.getName();

    static final String ID_KEY = "id";
    static final String TITLE_KEY = "title";
    static final String IMAGE_KEY = "imageUrl";
    static final String PLUGIN_ADDRESS_KEY = "pluginAddress";
    static final String SHOULD_FLIP_ON_RTL = "shouldFlipOnRtl";

    public String id;
    boolean hidden = false;
    boolean isSystemBackIcon = false;
    boolean shouldFlipOnRtl = false;

    // A header content item's content can be one of the following:
    String contentTitle;
    Drawable contentImage;
    public AstroPlugin contentPlugin;

    static HeaderContentItem fromTitle(PluginResolver pluginResolver, Activity activity, String id, String title) throws Exception {
        try {
            JSONObject json = new JSONObject();
            json.put(ID_KEY, id);
            json.put(TITLE_KEY, title);
            return HeaderContentItem.fromJson(pluginResolver, activity, json);
        } catch (JSONException ex) {
            Log.e(TAG, "Could not serialize header content item json for title: " + title, ex);
            // rethrow. We shouldn't ever get here unless `fromJson` is changed to look for a key
            // we don't set here.
            throw ex;
        }
    }

    static HeaderContentItem fromImageUrl(PluginResolver pluginResolver, Activity activity, String id, String url, Boolean shouldFlipOnRtl) throws Exception {
        try {
            JSONObject json = new JSONObject();
            json.put(ID_KEY, id);
            json.put(IMAGE_KEY, url);
            json.put(SHOULD_FLIP_ON_RTL, shouldFlipOnRtl);
            return HeaderContentItem.fromJson(pluginResolver, activity, json);
        } catch (JSONException ex) {
            Log.e(TAG, "Could not serialize header content item json for image url: " + url, ex);
            // rethrow. We shouldn't ever get here unless `fromJson` is changed to look for a key
            // we don't set here.
            throw ex;
        }
    }

    public static HeaderContentItem fromPlugin(PluginResolver pluginResolver, Activity activity, String id, String address) throws Exception {
        try {
            JSONObject json = new JSONObject();
            json.put(ID_KEY, id);
            json.put(PLUGIN_ADDRESS_KEY, address);
            return HeaderContentItem.fromJson(pluginResolver, activity, json);
        } catch (JSONException ex) {
            Log.e(TAG, "Could not serialize header content item json for plugin address: " + address, ex);
            // rethrow. We shouldn't ever get here unless `fromJson` is changed to look for a key
            // we don't set here.
            throw ex;
        }
    }

    static HeaderContentItem fromJson(PluginResolver pluginResolver, Activity activity, JSONObject jsonObject) throws Exception {
        if (jsonObject == null) {
            return null;
        }

        try {
            HeaderContentItem headerContentItem = new HeaderContentItem();

            if (jsonObject.has(ID_KEY)) {
                headerContentItem.id = jsonObject.getString(ID_KEY);
            } else {
                String message = String.format("\"%s\" is required, got: %s", ID_KEY, jsonObject);
                Log.e(TAG, message);
                throw new Exception(message);
            }

            if (jsonObject.has(TITLE_KEY)) {
                headerContentItem.contentTitle = jsonObject.getString(TITLE_KEY);

            } else if (jsonObject.has(IMAGE_KEY)) {
                Uri drawableUri = Uri.parse(jsonObject.getString(IMAGE_KEY));

                // Note that the following call blocks, this is especially noticeable if loading
                // a networked image.
                DrawableUriResolver drawableUriResolver = new DrawableUriResolver(activity);
                headerContentItem.contentImage = drawableUriResolver.getLocalDrawable(drawableUri);

                if (jsonObject.has(SHOULD_FLIP_ON_RTL)) {
                    headerContentItem.shouldFlipOnRtl = jsonObject.getBoolean(SHOULD_FLIP_ON_RTL);
                }

            } else if (jsonObject.has(PLUGIN_ADDRESS_KEY)) {
                String pluginAddress = jsonObject.getString(PLUGIN_ADDRESS_KEY);

                headerContentItem.contentPlugin = pluginResolver.instanceForAddress(pluginAddress);

            } else {
                String message = String.format(
                        "One of \"%s\", \"%s\", or \"%s\" is required, got: %s",
                        TITLE_KEY, IMAGE_KEY, PLUGIN_ADDRESS_KEY, jsonObject
                );
                Log.e(TAG, message);
                throw new Exception(message);
            }

            return headerContentItem;
        } catch (JSONException e) {
            Log.e(TAG, String.format("Could not deserialize header content item json, got: %s", jsonObject), e);
            throw e;
        }
    }
}
