package com.thebluealliance.androidclient.renderers;

import android.support.annotation.Nullable;

import com.thebluealliance.androidclient.datafeed.APICache;
import com.thebluealliance.androidclient.helpers.DistrictHelper.DISTRICTS;
import com.thebluealliance.androidclient.helpers.EventTeamHelper;
import com.thebluealliance.androidclient.helpers.ModelType;
import com.thebluealliance.androidclient.listitems.ListItem;
import com.thebluealliance.androidclient.listitems.ModelListElement;
import com.thebluealliance.androidclient.models.BasicModel;
import com.thebluealliance.androidclient.models.District;
import com.thebluealliance.androidclient.models.Event;
import com.thebluealliance.androidclient.models.Match;
import com.thebluealliance.androidclient.models.Team;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class MyTbaModelRenderer implements ModelRenderer<Void, Void> {

    private APICache mDatafeed;

    @Inject
    public MyTbaModelRenderer(APICache datafeed) {
        mDatafeed = datafeed;
    }

    @Override @Nullable
    public ModelListElement renderFromKey(String key, ModelType.MODELS type) {
        String text;
        try {
            switch (type) {
                case EVENT:
                    Event event = mDatafeed.fetchEvent(key).toBlocking().first();
                    if (event == null) {
                        text = key;
                        break;
                    }
                    text = event.getEventYear() + " " + event.getEventShortName();
                    break;
                case TEAM:
                    Team team = mDatafeed.fetchTeam(key).toBlocking().first();
                    if (team == null) {
                        text = key;
                        break;
                    }
                    text = team.getNickname();
                    break;
                case MATCH:
                    Match match = mDatafeed.fetchMatch(key).toBlocking().first();
                    if (match == null) {
                        text = key;
                        break;
                    }
                    text = match.getEventKey() + " " + match.getTitle();
                    break;
                case EVENTTEAM:
                    String teamKey = EventTeamHelper.getTeamKey(key);
                    String eventKey = EventTeamHelper.getEventKey(key);
                    Team eTeam = mDatafeed.fetchTeam(teamKey).toBlocking().first();
                    Event eEvent = mDatafeed.fetchEvent(eventKey).toBlocking().first();
                    if (eTeam == null || eEvent == null) {
                        text = String.format("%1$s @ %2$s", teamKey, eventKey);
                        break;
                    }
                    text = String.format("%1$s @ %2$d %3$s",
                      eTeam.getNickname(),
                      eEvent.getEventYear(),
                      eEvent.getEventShortName());
                    break;
                case DISTRICT:
                    District district = mDatafeed.fetchDistrict(key).toBlocking().first();
                    if (district == null) {
                        text = key;
                        break;
                    }
                    String districtName = DISTRICTS.fromAbbreviation(district.getAbbreviation())
                      .getName();
                    text = String.format("%1$d %2$s", district.getYear(), districtName);
                    break;
                default:
                    return null;
            }
            return new ModelListElement(text, key, type);
        } catch (BasicModel.FieldNotDefinedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public @Nullable ListItem renderFromModel(Void aVoid, Void a2void) {
        return null;
    }
}

@@ -90,16 +90,64 @@
 ullable;
+%0Aimport android.support.annotation.WorkerThread;
 %0A%0Aimport
@@ -204,83 +204,8 @@
 he;%0A
-import com.thebluealliance.androidclient.helpers.DistrictHelper.DISTRICTS;%0A
 impo
@@ -1024,54 +1024,60 @@
    @
-Override @Nullable%0A    public ModelListElement
+WorkerThread @Override%0A    public @Nullable ListItem
  ren
@@ -1366,49 +1366,51 @@
     
-text = key;%0A                        break
+return new ModelListElement(key, key, type)
 ;%0A  
@@ -1453,96 +1453,29 @@
     
-text =
+return
  event.
-getEventYear() + %22 %22 + event.getEventShortName();%0A                    break
+render()
 ;%0A  
@@ -1646,49 +1646,51 @@
     
-text = key;%0A                        break
+return new ModelListElement(key, key, type)
 ;%0A  
@@ -1733,60 +1733,28 @@
     
-text = team.getNickname();%0A                    break
+return team.render()
 ;%0A  
@@ -1930,49 +1930,51 @@
     
-text = key;%0A                        break
+return new ModelListElement(key, key, type)
 ;%0A  
@@ -2017,86 +2017,29 @@
     
-text = match.getEventKey() + %22 %22 + match.getTitle();%0A                    break
+return match.render()
 ;%0A  
@@ -2537,37 +2537,76 @@
                 
-b
 re
-ak
+turn new ModelListElement(text, key, type)
 ;%0A              
@@ -2825,37 +2825,76 @@
                 
-b
 re
-ak
+turn new ModelListElement(text, key, type)
 ;%0A              
@@ -3073,169 +3073,52 @@
     
-text = key;%0A                        break;%0A                    %7D%0A                    String districtName = DISTRICTS.fromAbbreviation(district.getAbbreviation())
+return new ModelListElement(key, key, type);
 %0A   
@@ -3138,21 +3138,9 @@
     
-  .getName();
+%7D
 %0A   
@@ -3160,102 +3160,32 @@
     
-text = String.format(%22%251$d %252$s%22, district.getYear(), districtName);%0A                    break
+return district.render()
 ;%0A  
@@ -3258,66 +3258,8 @@
   %7D%0A
-            return new ModelListElement(text, key, type);%0A
     

Make myTBA items render like their parent models