package org.wikipedia.feed.view;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.net.Uri;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.wikipedia.R;
import org.wikipedia.util.ResourceUtil;
import org.wikipedia.views.FaceAndColorDetectImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CardLargeHeaderView extends RelativeLayout {
    @BindView(R.id.view_card_header_large_background) View backgroundView;
    @BindView(R.id.view_card_header_large_image) FaceAndColorDetectImageView imageView;
    @BindView(R.id.view_card_header_large_title) TextView titleView;

    public CardLargeHeaderView(Context context) {
        super(context);

        resetBackgroundColor();
        inflate(getContext(), R.layout.view_card_header_large, this);
        ButterKnife.bind(this);
        imageView.setOnImageLoadListener(new ImageLoadListener());
    }

    // todo: should this use ViewUtil.loadImageUrlInto() instead? should loadImageUrlInto() set
    //       view visibility?
    @NonNull public CardLargeHeaderView setImage(@Nullable Uri uri) {
        imageView.setVisibility(uri == null ? GONE : VISIBLE);
        imageView.loadImage(uri);
        return this;
    }

    @NonNull public CardLargeHeaderView setTitle(@Nullable CharSequence title) {
        titleView.setText(title);
        return this;
    }

    @NonNull public CardLargeHeaderView onClickListener(@Nullable OnClickListener listener) {
        backgroundView.setOnClickListener(listener);
        return this;
    }

    private void resetBackgroundColor() {
        setBackgroundColor(ResourceUtil.getThemedColor(getContext(), R.attr.paper_color));
    }

    private class ImageLoadListener implements FaceAndColorDetectImageView.OnImageLoadListener {
        @Override
        public void onImageLoaded(final int bmpHeight, @Nullable final PointF faceLocation, @ColorInt final int mainColor) {
            post(new Runnable() {
                @Override
                public void run() {
                    if (!ViewCompat.isAttachedToWindow(CardLargeHeaderView.this)) {
                        return;
                    }
                    animateBackgroundColor(CardLargeHeaderView.this, mainColor);
                    if (faceLocation != null) {
                        imageView.getHierarchy().setActualImageFocusPoint(faceLocation);
                    }
                }
            });
        }

        @Override
        public void onImageFailed() {
            resetBackgroundColor();
        }

        private void animateBackgroundColor(@NonNull View view, @ColorInt int targetColor) {
            final int animDuration = 500;
            ObjectAnimator animator = ObjectAnimator.ofInt(view, "backgroundColor",
                    ResourceUtil.getThemedColor(getContext(), R.attr.paper_color),
                    targetColor);
            animator.setEvaluator(new ArgbEvaluator());
            animator.setDuration(animDuration);
            animator.setupStartValues();
            animator.start();
        }
    }
}

@@ -327,16 +327,65 @@
 llable;%0A
+import android.support.v4.content.ContextCompat;%0A
 import a
@@ -411,32 +411,32 @@
 iew.ViewCompat;%0A
-
 import android.v
@@ -544,48 +544,8 @@
 .R;%0A
-import org.wikipedia.util.ResourceUtil;%0A
 impo
@@ -1941,38 +1941,33 @@
 ndColor(
-ResourceUtil.getThemed
+ContextCompat.get
 Color(ge
@@ -1980,32 +1980,28 @@
 xt(), R.
-attr.paper_color
+color.base20
 ));%0A    
@@ -3108,30 +3108,25 @@
     
-ResourceUtil.getThemed
+ContextCompat.get
 Colo
@@ -3147,24 +3147,20 @@
 , R.
-attr.paper_color
+color.base20
 ),%0A 

fix background color of LargeHeader cards in feed.