package com.monits.agilefant.adapter;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.monits.agilefant.AgilefantApplication;
import com.monits.agilefant.R;
import com.monits.agilefant.adapter.helper.UpdateAdapterHelper;
import com.monits.agilefant.adapter.recyclerviewholders.DailyWorkTaskItemViewHolder;
import com.monits.agilefant.adapter.recyclerviewholders.WorkItemViewHolder;
import com.monits.agilefant.adapter.recyclerviewholders.WorkItemViewHolderUpdateTracker;
import com.monits.agilefant.model.Task;
import com.monits.agilefant.model.WorkItem;
import com.monits.agilefant.recycler.DragAndDropListener;
import com.monits.agilefant.service.TaskRankUpdaterService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

/**
 * Created by edipasquale on 25/09/15.
 */
public class TasksRecyclerAdapter extends RecyclerView.Adapter<WorkItemViewHolder<Task>> implements
		WorkItemViewHolderUpdateTracker, DragAndDropListener {

	protected List<Task> taskList;
	private final List<Task> originalTaskList;
	protected final LayoutInflater inflater;
	protected final FragmentActivity context;

	private final UpdateAdapterHelper updateAdapterHelper;

	private final TaskRankUpdaterService rankUpdaterService;

	/**
	 * @param context Current context
	 * @param taskList List of task objects
	 * @param rankUpdaterService Service that manages task rank changes.
	 */
	public TasksRecyclerAdapter(final FragmentActivity context, final List<Task> taskList,
			final TaskRankUpdaterService rankUpdaterService) {
		this.context = context;
		this.taskList = taskList;
		this.originalTaskList = this.taskList;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		this.rankUpdaterService = rankUpdaterService;

		updateAdapterHelper = new UpdateAdapterHelper(this);
		AgilefantApplication.getObjectGraph().inject(this);
	}

	@Override
	public WorkItemViewHolder<Task> onCreateViewHolder(final ViewGroup parent, final int viewType) {
		final View view = inflater.inflate(R.layout.my_tasks_item, parent, false);

		return new DailyWorkTaskItemViewHolder(view, context, this);
	}

	@Override
	public void onBindViewHolder(final WorkItemViewHolder<Task> holder, final int position) {
		holder.onBindView(taskList.get(position));
	}

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

	@Override
	public void onUpdate(final WorkItem updatedWork) {
		updateAdapterHelper.updateItem(taskList, updatedWork);
	}

	public void setTasks(final List<Task> taskList) {
		this.taskList = taskList;
	}

	@Override
	public void onMove(final int fromPosition, final int toPosition) {
		notifyItemMoved(fromPosition, toPosition);
		Collections.swap(taskList, fromPosition, toPosition);

	}

	@Override
	public void onChangePosition(final int fromPosition, final int toPosition) {
		final Task currentTask = taskList.get(fromPosition);
		final Task targetTask = taskList.get(toPosition);

		rankUpdaterService.rankTaskUnder(currentTask, targetTask, taskList,
				new Response.Listener<Task>() {

					@Override
					public void onResponse(final Task arg0) {
						Toast.makeText(
							context, R.string.feedback_success_updated_task_rank, Toast.LENGTH_SHORT).show();
					}
				},
				new Response.ErrorListener() {

					@Override
					public void onErrorResponse(final VolleyError arg0) {
						Toast.makeText(
							context, R.string.feedback_failed_update_tasks_rank, Toast.LENGTH_SHORT).show();
					}
				});
	}

	@Override
	public WorkItem getItem(final int position) {
		return taskList.get(position);
	}

	/**
	 * Filter the list leaving only those that match the text sent.
	 * @param query the text used to filter
	 */
	public void filter(final String query) {
		final String queryText = query.toLowerCase(Locale.getDefault());
		taskList = new ArrayList<>(); //Clean List
		for (final Task t : originalTaskList) {
			if (t.getName().toLowerCase(Locale.getDefault()).contains(queryText)) {
				taskList.add(t);
			}
			notifyDataSetChanged();
		}
	}

	@Override
	public String toString() {
		return "TasksRecyclerAdapter{"
				+ "taskList=" + taskList
				+ '}';
	}
}

@@ -3147,24 +3147,25 @@
 Position) %7B%0A
+%0A
 %09%09final Task
@@ -3172,24 +3172,160 @@
  currentTask
+;%0A%09%09final Task targetTask;%0A%0A%09%09if (fromPosition %3C toPosition) %7B%0A%09%09%09// Moves down%0A%09%09%09currentTask = taskList.get(toPosition);%0A%09%09%09targetTask
  = taskList.
@@ -3349,31 +3349,115 @@
 ;%0A%09%09
-final 
+%7D else %7B%0A%09%09%09// Moves up%0A%09%09%09current
 Task
+ =
  ta
-rgetTask =
+skList.get(toPosition);%0A%09%09%09targetTask = toPosition == 0 ? null :
  tas
@@ -3458,34 +3458,36 @@
  : taskList.get(
-to
+from
 Position);%0A%0A%09%09ra
@@ -3480,16 +3480,20 @@
 sition);
+%0A%09%09%7D
 %0A%0A%09%09rank
@@ -3669,32 +3669,33 @@
 Toast.makeText(%0A
+%09
 %09%09%09%09%09%09%09context, 
@@ -3759,32 +3759,32 @@
 _SHORT).show();%0A
-
 %09%09%09%09%09%7D%0A%09%09%09%09%7D,%0A%09%09
@@ -3909,24 +3909,25 @@
 t.makeText(%0A
+%09
 %09%09%09%09%09%09%09conte

Fix task drag