package com.battlelancer.seriesguide.adapters;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.View;
import android.view.ViewGroup;
import com.battlelancer.seriesguide.R;
import com.battlelancer.seriesguide.util.TextTools;
import com.battlelancer.seriesguide.util.Utils;
import com.uwetrottmann.trakt5.entities.HistoryEntry;

/**
 * Creates a list of episodes from a list of trakt {@link com.uwetrottmann.trakt.v2.entities.HistoryEntry}.
 */
public class EpisodeHistoryAdapter extends SectionedHistoryAdapter {

    public EpisodeHistoryAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to child views to avoid
        // unnecessary calls to findViewById() on each row.
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_history, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        HistoryEntry item = getItem(position);

        // show title
        holder.title.setText(item.show == null ? null : item.show.title);
        // show poster
        String poster =
                (item.show == null || item.show.images == null || item.show.images.poster == null)
                        ? null : item.show.images.poster.thumb;
        Utils.loadSmallPoster(getContext(), holder.poster, poster);

        // timestamp
        if (item.watched_at != null) {
            CharSequence timestamp = DateUtils.getRelativeTimeSpanString(
                    item.watched_at.getMillis(), System.currentTimeMillis(),
                    DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_ALL);
            holder.timestamp.setText(timestamp);
        } else {
            holder.timestamp.setText(null);
        }

        // action type indicator
        if ("watch".equals(item.action)) {
            // marked watched
            holder.type.setImageResource(getResIdDrawableWatched());
        } else {
            // check-in, scrobble
            holder.type.setImageResource(getResIdDrawableCheckin());
        }

        // episode
        if (item.episode != null && item.episode.season != null && item.episode.number != null) {
            holder.description.setText(TextTools.getNextEpisodeString(getContext(), item.episode.season,
                    item.episode.number, item.episode.title));
        } else {
            holder.description.setText(null);
        }

        return convertView;
    }
}

@@ -73,16 +73,140 @@
 ontext;%0A
+import android.support.annotation.NonNull;%0Aimport android.support.v4.util.SparseArrayCompat;%0Aimport android.text.TextUtils;%0A
 import a
@@ -331,16 +331,126 @@
 uide.R;%0A
+import com.battlelancer.seriesguide.thetvdbapi.TvdbTools;%0Aimport com.battlelancer.seriesguide.util.ShowTools;%0A
 import c
@@ -594,16 +594,39 @@
 ryEntry;
+%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -685,43 +685,8 @@
 ink 
-com.uwetrottmann.trakt.v2.entities.
 Hist
@@ -694,16 +694,24 @@
 ryEntry%7D
+ objects
 .%0A */%0Apu
@@ -778,16 +778,73 @@
 pter %7B%0A%0A
+    private SparseArrayCompat%3CString%3E localShowPosters;%0A%0A
     publ
@@ -914,24 +914,215 @@
 xt);%0A    %7D%0A%0A
+    @Override%0A    public void setData(List%3CHistoryEntry%3E data) %7B%0A        super.setData(data);%0A        localShowPosters = ShowTools.getShowTvdbIdsAndPosters(getContext());%0A    %7D%0A%0A    @NonNull%0A
     @Overrid
@@ -1178,16 +1178,25 @@
 ertView,
+ @NonNull
  ViewGro
@@ -1699,16 +1699,107 @@
 sition);
+%0A        if (item == null) %7B%0A            return convertView; // all bets are off!%0A        %7D
 %0A%0A      
@@ -1910,16 +1910,32 @@
 w poster
+, use a TVDB one
 %0A       
@@ -1950,16 +1950,22 @@
 poster =
+ null;
 %0A       
@@ -1965,23 +1965,36 @@
         
-       
+Integer showTvdbId =
  (item.s
@@ -2019,20 +2019,17 @@
 m.show.i
-mage
+d
 s == nul
@@ -2033,44 +2033,132 @@
 null
- %7C%7C item.show.images.poster =
+)%0A                ? null%0A                : item.show.ids.tvdb;%0A        if (localShowPosters != null && showTvdbId !
 = null)
+ %7B
 %0A   
@@ -2170,59 +2170,317 @@
     
-            ? null : item.show.images.poster.thumb;
+// prefer the TVDB poster of an already added show%0A            poster = localShowPosters.get(showTvdbId);%0A            if (TextUtils.isEmpty(poster)) %7B%0A                // fall back to the first uploaded TVDB poster%0A                poster = TvdbTools.buildFallbackPosterPath(showTvdbId);%0A            %7D%0A        %7D
 %0A   
@@ -2499,16 +2499,24 @@
 oadSmall
+TvdbShow
 Poster(g

Use TVDB images on Shows history tab.