package com.monits.agilefant.service;

import com.android.volley.Request;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.monits.agilefant.model.Iteration;
import com.monits.agilefant.model.Product;
import com.monits.agilefant.model.Project;
import com.monits.volleyrequests.network.request.GsonRequest;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.inject.Inject;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;


public class BacklogServiceImpl implements BacklogService {

	private static final String GET_ALL_BACKLOGS_URL = "%1$s/ajax/menuData.action";
	private static final String GET_MY_BACKLOGS_URL = "%1$s/ajax/myAssignmentsMenuData.action";

	private final AgilefantService agilefantService;

	@SuppressFBWarnings(value = "MISSING_FIELD_IN_TO_STRING", justification = "We do not want this in the toString")
	private final Gson gson;

	/**
	 * @param agilefantService Injected via Constructor by Dagger
	 * @param gson Injected via Constructor by Dagger
	 */
	@Inject
	public BacklogServiceImpl(final AgilefantService agilefantService,
							final Gson gson) {
		this.agilefantService = agilefantService;
		this.gson = gson;
	}

	@Override
	public void getAllBacklogs(final Listener<List<Product>> listener, final ErrorListener error) {
		final String url = String.format(Locale.US, GET_ALL_BACKLOGS_URL, agilefantService.getHost());

		final Type listType = new TypeToken<ArrayList<Product>>() { }.getType();
		final GsonRequest<List<Product>> request = new GsonRequest<>(Request.Method.GET, url,
				gson, listType, listener, error, null);

		agilefantService.addRequest(request);
	}

	@Override
	public void getMyBacklogs(final Listener<List<Project>> listener, final ErrorListener error) {
		final String url = String.format(Locale.US, GET_MY_BACKLOGS_URL, agilefantService.getHost());

		final Type listType = new TypeToken<ArrayList<Project>>() { }.getType();
		final GsonRequest<List<Project>> request = new GsonRequest<>(Request.Method.GET, url,
				gson, listType,
				new Listener<List<Project>>() {
					@Override
					public void onResponse(final List<Project> projects) {
						// Fill all projects iterations parent.
						for (final Project project : projects) {
							for (final Iteration iteration : project.getIterationList()) {
								iteration.setParent(project);
							}
						}
						listener.onResponse(projects);
					}
				}, error, null);

		agilefantService.addRequest(request);
	}
}

@@ -2300,24 +2300,55 @@
 projects) %7B%0A
+%09%09%09%09%09%09if (projects != null) %7B%0A%09
 %09%09%09%09%09%09// Fil
@@ -2381,16 +2381,17 @@
 parent.%0A
+%09
 %09%09%09%09%09%09fo
@@ -2425,24 +2425,25 @@
 projects) %7B%0A
+%09
 %09%09%09%09%09%09%09for (
@@ -2508,16 +2508,17 @@
 %09%09%09%09%09%09%09%09
+%09
 iteratio
@@ -2539,16 +2539,26 @@
 oject);%0A
+%09%09%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09%09%7D

Fix NullPointerException