package link.standen.michael.fatesheets.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

import link.standen.michael.fatesheets.R;
import link.standen.michael.fatesheets.model.Character;
import link.standen.michael.fatesheets.model.CharacterViewHolder;

/**
 * Manages a list of characters.
 */

public class CharacterArrayAdapter extends ArrayAdapter<Character> {

	private static final String TAG = CharacterArrayAdapter.class.getName();

	private final Context context;
	private final int resourceId;
	private final List<Character> items;

	public CharacterArrayAdapter(@NonNull Context context, @LayoutRes int resourceId, @NonNull List<Character> items) {
		super(context, resourceId, items);

		this.context = context;
		this.resourceId = resourceId;
		this.items = items;
	}

	public Character getItem(int index){
		return items.get(index);
	}

	@NonNull
	@Override
	public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
		View view = convertView;
		CharacterViewHolder holder;
		if (view == null){
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(resourceId, null);
			holder = new CharacterViewHolder();
			holder.setNameView((TextView) view.findViewById(R.id.character_name));
			view.setTag(holder);
		} else {
			holder = (CharacterViewHolder) view.getTag();
		}

		final Character item = getItem(position);
		if (item != null){
			holder.setCharacter(item);
			item.setHolder(holder);
			holder.getNameView().setText(item.getName());
		}
		return view;
	}
}

@@ -393,16 +393,78 @@
 .List;%0A%0A
+import link.standen.michael.fatesheets.CharacterListActivity;%0A
 import l
@@ -497,16 +497,16 @@
 eets.R;%0A
-
 import l
@@ -823,22 +823,36 @@
  final C
-ontext
+haracterListActivity
  context
@@ -963,22 +963,36 @@
 onNull C
-ontext
+haracterListActivity
  context
@@ -1287,16 +1287,22 @@
 getView(
+final 
 int posi
@@ -1706,16 +1706,16 @@
 name));%0A
-
 %09%09%09view.
@@ -1729,16 +1729,314 @@
 holder);
+%0A%0A%09%09%09view.findViewById(R.id.delete_character).setOnClickListener(new View.OnClickListener() %7B%0A%09%09%09%09@Override%0A%09%09%09%09public void onClick(View v) %7B%0A%09%09%09%09%09CharacterArrayAdapter.this.items.remove(position);%0A%09%09%09%09%09CharacterArrayAdapter.this.notifyDataSetChanged();%0A%09%09%09%09%09//TODO Delete from storage%0A%09%09%09%09%7D%0A%09%09%09%7D);
 %0A%09%09%7D els

Delete button deletes character