package com.akexorcist.sleepingforless.view.feed;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.akexorcist.sleepingforless.R;
import com.akexorcist.sleepingforless.network.blogger.BloggerManager;
import com.akexorcist.sleepingforless.network.blogger.model.PostList;
import com.akexorcist.sleepingforless.util.content.ContentUtility;
import com.akexorcist.sleepingforless.util.content.EasterEggUtility;
import com.balysv.materialripple.MaterialRippleLayout;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.zl.reik.dilatingdotsprogressbar.DilatingDotsProgressBar;

import java.util.List;

/**
 * Created by Akexorcist on 3/10/2016 AD.
 */
public class FeedViewHolder extends RecyclerView.ViewHolder {
    public TextView tvTitle;
    public TextView tvLabel;
    public TextView tvDate;
    public ImageView ivTitle;
    public ImageView ivBookmarkIndicator;
    public MaterialRippleLayout mrlFeedButton;
    public DilatingDotsProgressBar pbFeedImageLoading;

    public FeedViewHolder(View itemView) {
        super(itemView);
        tvTitle = (TextView) itemView.findViewById(R.id.tv_feed_title);
        tvLabel = (TextView) itemView.findViewById(R.id.tv_feed_label);
        tvDate = (TextView) itemView.findViewById(R.id.tv_feed_date);
        ivTitle = (ImageView) itemView.findViewById(R.id.iv_feed_title);
        ivBookmarkIndicator = (ImageView) itemView.findViewById(R.id.iv_feed_bookmark_indicator);
        mrlFeedButton = (MaterialRippleLayout) itemView.findViewById(R.id.mrl_feed_button);
        pbFeedImageLoading = (DilatingDotsProgressBar) itemView.findViewById(R.id.pb_feed_image_loading);
    }

    public void setTitle(String title) {
        title = ContentUtility.getInstance().removeLabelFromTitle(title);

        // Easter Egg for April Fool Day
        if (EasterEggUtility.newInstance().isAprilFoolDay()) {
            title = title.replaceAll("Android", " iOS ");
            title = title.replaceAll("แอนดรอยด์", " iOS ");
        }

        tvTitle.setText(title);
    }

    public void setLabel(List<String> labelList) {
        if (labelList != null && labelList.size() > 0) {
            String resultLabel = "";
            for (String label : labelList) {
                resultLabel += label + ", ";
            }
            resultLabel.replaceAll(", $", "");

            // Easter Egg for April Fool Day
            if (EasterEggUtility.newInstance().isAprilFoolDay()) {
                resultLabel = resultLabel.replaceAll("Android", "iOS");
            }

            tvLabel.setText(resultLabel);
            tvLabel.setVisibility(View.VISIBLE);
        } else {
            tvLabel.setVisibility(View.GONE);
        }
    }

    public void setSortDate(String sortType, String publishedDate, String updateDate) {
        if (sortType != null && sortType.equalsIgnoreCase(BloggerManager.SORT_UPDATED_DATE)) {
            setDate(tvDate, updateDate, R.string.updated_date);
        } else {
            setDate(tvDate, publishedDate, R.string.published_date);
        }
    }

    private void setDate(TextView tvPublishedDate, String date, int messageResId) {
        String[] dates = date.split("T")[0].split("-");
        String publishedDetail = tvPublishedDate.getContext().getString(messageResId);
        tvPublishedDate.setText(String.format(publishedDetail, dates[2], dates[1], dates[0]));
    }

    public void setImage(List<PostList.Image> imageList) {
        if (imageList != null && !imageList.isEmpty() && imageList.size() > 0) {
            pbFeedImageLoading.showNow();
            ivTitle.setImageDrawable(null);
            ivTitle.setVisibility(View.VISIBLE);
            String url = imageList.get(0).getUrl();
            loadItemResource(ivTitle, url);
        } else {
            ivTitle.setVisibility(View.GONE);
        }
    }

    private void loadItemResource(ImageView ivTitle, String url) {
        Glide.with(ivTitle.getContext())
                .load(url)
                .crossFade(200)
                .thumbnail(0.1f)
                .override(500, 500)
                .centerCrop()
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        pbFeedImageLoading.hideNow();
                        return false;
                    }
                })
                .into(ivTitle);
    }

    public void setBookmarkIndicator(boolean isBookmark) {
        if (isBookmark) {
            ivBookmarkIndicator.setVisibility(View.VISIBLE);
        } else {
            ivBookmarkIndicator.setVisibility(View.GONE);
        }
    }

    public void setFeedClickListener(View.OnClickListener listener) {
        mrlFeedButton.setOnClickListener(listener);
    }

    public void setFeedLongClickListener(View.OnLongClickListener listener) {
        mrlFeedButton.setOnLongClickListener(listener);
    }


}

@@ -845,16 +845,74 @@
 .List;%0A%0A
+import butterknife.Bind;%0Aimport butterknife.ButterKnife;%0A%0A
 /**%0A * C
@@ -1023,119 +1023,285 @@
     
-public TextView tvTitle;%0A    public TextView tvLabel;%0A    public TextView tvDate;%0A    public ImageView ivTitle;
+@Bind(R.id.tv_feed_title)%0A    public TextView tvTitle;%0A%0A    @Bind(R.id.tv_feed_label)%0A    public TextView tvLabel;%0A%0A    @Bind(R.id.tv_feed_date)%0A    public TextView tvDate;%0A%0A    @Bind(R.id.iv_feed_title)%0A    public ImageView ivTitle;%0A%0A    @Bind(R.id.iv_feed_bookmark_indicator)
 %0A   
@@ -1338,16 +1338,49 @@
 dicator;
+%0A%0A    @Bind(R.id.mrl_feed_button)
 %0A    pub
@@ -1418,16 +1418,55 @@
 dButton;
+%0A%0A    @Bind(R.id.pb_feed_image_loading)
 %0A    pub
@@ -1594,580 +1594,39 @@
     
-tvTitle = (TextView) itemView.findViewById(R.id.tv_feed_title);%0A        tvLabel = (TextView) itemView.findViewById(R.id.tv_feed_label);%0A        tvDate = (TextView) itemView.findViewById(R.id.tv_feed_date);%0A        ivTitle = (ImageView) itemView.findViewById(R.id.iv_feed_title);%0A        ivBookmarkIndicator = (ImageView) itemView.findViewById(R.id.iv_feed_bookmark_indicator);%0A        mrlFeedButton = (MaterialRippleLayout) itemView.findViewById(R.id.mrl_feed_button);%0A        pbFeedImageLoading = (DilatingDotsProgressBar) itemView.findViewById(R.id.pb_feed_image_loading
+ButterKnife.bind(this, itemView
 );%0A 

Use ButterKnife