package com.mobify.astro.plugins.headerbarplugin;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobify.astro.AstroActivity;
import com.mobify.astro.AstroPlugin;
import com.mobify.astro.R;
import com.mobify.astro.utilities.DrawableUtilities;
import com.mobify.astro.utilities.LocalizationUtilities;

class HeaderItemView extends FrameLayout {
    static String TAG = HeaderItemView.class.getName();

    View view = null;
    int textColor = Color.BLACK;
    int contentGravity;
    HeaderContentItem currentItem = null;

    HeaderItemView(Context context, int gravity) {
        super(context);
        this.contentGravity = gravity;
    }

    void setHeaderContentItem(HeaderContentItem headerContentItem) {
        currentItem = headerContentItem != null ? headerContentItem : new HeaderContentItem();
        LocalizationUtilities localizationUtilities = ((AstroActivity) getContext()).getLocalizationUtilities();
        if (currentItem.contentTitle != null) {
            setText(localizationUtilities.translate(currentItem.contentTitle));

        } else if (currentItem.contentImage != null) {
            Drawable drawable;
            if (localizationUtilities.rightToLeft() && currentItem.shouldFlipOnRtl) {
                drawable = DrawableUtilities.flipDrawable((BitmapDrawable) currentItem.contentImage, getResources());
            } else {
                drawable = currentItem.contentImage;
            }
            setDrawable(drawable);

        } else if (currentItem.contentPlugin != null) {
            setPlugin(currentItem.contentPlugin);
        } else {
            setEmpty();
            Log.w(TAG, "Tried to set header item to empty header content item.");
        }
    }

    private void setText(String text) {
        FrameLayout.LayoutParams layoutParams;
        if (!(view instanceof TextView)) {
            this.removeAllViews();

            layoutParams = new FrameLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT
            );

            AppCompatTextView textView = new AppCompatTextView(getContext());
            textView.setTextAppearance(getContext(), R.style.TextAppearance_AppCompat_Title);
            textView.setSingleLine();
            textView.setEllipsize(TextUtils.TruncateAt.END);

            this.addView(textView, layoutParams);
            view = textView;
        } else {
            layoutParams = (FrameLayout.LayoutParams) view.getLayoutParams();
        }

        layoutParams.gravity = contentGravity;
        AppCompatTextView textView = (AppCompatTextView)view;
        textView.setTextColor(textColor);
        textView.setText(text);
        setVisibility();
    }

    private void setDrawable(Drawable drawable) {
        if (!(view instanceof ImageView)) {
            this.removeAllViews();

            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT
            );
            layoutParams.gravity = contentGravity;

            ImageView imageView = new ImageView(getContext());
            this.addView(imageView, layoutParams);
            view = imageView;
        }

        ImageView imageView = (ImageView)view;
        tintImageWithTextColor(imageView);
        imageView.setImageDrawable(drawable);
        setVisibility();
    }

    private void setPlugin(AstroPlugin plugin) {
        if (view != plugin.getView()) {
            this.removeAllViews();

            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT
            );
            layoutParams.gravity = contentGravity;

            this.addView(plugin.getView(), layoutParams);
            view = plugin.getView();
        }

        setVisibility();
    }

    private void setEmpty() {
        this.setVisibility(View.GONE);
        this.removeAllViews();
        view = null;
    }

    private void tintImageWithTextColor(ImageView view) {
        if (currentItem.isSystemBackIcon) {
            view.setColorFilter(textColor, PorterDuff.Mode.SRC_ATOP);
        } else {
            view.setColorFilter(null);
        }
    }

    void setTextColor(int color) {
        textColor = color;

        if (view instanceof TextView) {
            TextView textView = (TextView)view;
            textView.setTextColor(color);
        } else if (view instanceof ImageView) {
            tintImageWithTextColor((ImageView)view);
        }
    }

    private void setVisibility() {
        setVisibility(currentItem.hidden ? View.GONE : View.VISIBLE);
    }
}
