package com.mgaetan89.showsrage.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mgaetan89.showsrage.R;
import com.mgaetan89.showsrage.helper.DateTimeHelper;
import com.mgaetan89.showsrage.model.History;
import com.mgaetan89.showsrage.network.SickRageApi;

import java.util.Collections;
import java.util.List;

public class HistoriesAdapter extends RecyclerView.Adapter<HistoriesAdapter.ViewHolder> {
	@NonNull
	private List<History> histories = Collections.emptyList();

	public HistoriesAdapter(@Nullable List<History> histories) {
		if (histories == null) {
			this.histories = Collections.emptyList();
		} else {
			this.histories = histories;
		}
	}

	@Override
	public int getItemCount() {
		return this.histories.size();
	}

	@Override
	public void onBindViewHolder(ViewHolder holder, int position) {
		History history = this.histories.get(position);

		if (holder.date != null) {
			holder.date.setText(DateTimeHelper.getRelativeDate(history.getDate(), "yyyy-MM-dd hh:mm", 0));
		}

		if (holder.logo != null) {
			holder.logo.setContentDescription(history.getShowName());

			Glide.with(holder.logo.getContext())//
					.load(SickRageApi.getInstance().getPosterUrl(history.getTvDbId()))//
					.into(holder.logo);
		}

		if (holder.name != null) {
			holder.name.setText(holder.name.getResources().getString(R.string.show_name_episode, history.getShowName(), history.getSeason(), history.getEpisode()));
		}

		if (holder.statusProvider != null) {
			String provider = history.getProvider();
			int status = getTranslatedStatus(history.getStatus());

			if ("-1".equals(provider)) {
				if (status == 0) {
					holder.statusProvider.setText(history.getStatus());
				} else {
					holder.statusProvider.setText(status);
				}
			} else {
				holder.statusProvider.setText(holder.statusProvider.getResources().getString(R.string.status_from, status, provider));
			}
		}
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_histories_list, parent, false);

		return new ViewHolder(view);
	}

	@StringRes
	/* package */ static int getTranslatedStatus(String status) {
		if (status != null) {
			String normalizedStatus = status.toLowerCase();

			switch (normalizedStatus) {
				case "downloaded":
					return R.string.downloaded;

				case "snatched":
					return R.string.snatched;
			}
		}

		return 0;
	}

	public static class ViewHolder extends RecyclerView.ViewHolder {
		@Nullable
		public TextView date;

		@Nullable
		public ImageView logo;

		@Nullable
		public TextView name;

		@Nullable
		public TextView statusProvider;

		public ViewHolder(View view) {
			super(view);

			this.date = (TextView) view.findViewById(R.id.episode_date);
			this.logo = (ImageView) view.findViewById(R.id.episode_logo);
			this.name = (TextView) view.findViewById(R.id.episode_name);
			this.statusProvider = (TextView) view.findViewById(R.id.episode_status_provider);
		}
	}
}

@@ -35,16 +35,54 @@
 apter;%0A%0A
+import android.content.res.Resources;%0A
 import a
@@ -2121,24 +2121,46 @@
  else %7B%0A%09%09%09%09
+Resources resources = 
 holder.statu
@@ -2169,24 +2169,37 @@
 rovider.
-setText(
+getResources();%0A%0A%09%09%09%09
 holder.s
@@ -2212,28 +2212,33 @@
 rovider.
-getR
+setText(r
 esources
 ().getSt
@@ -2221,34 +2221,32 @@
 etText(resources
-()
 .getString(R.str
@@ -2262,22 +2262,43 @@
 s_from, 
+resources.getString(
 status
+)
 , provid

Fix history