package com.iic.mokojin;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.iic.mokojin.models.QueueItem;
import com.iic.mokojin.operation.LeaveQueueOperation;
import com.iic.mokojin.presenters.CharacterPresenter;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnItemLongClick;
import de.timroes.android.listview.EnhancedListView;

public class PlayerQueueFragment extends Fragment {

    @InjectView(R.id.queue_list_view) EnhancedListView mQueueListView;
    QueueAdapter mQueueAdapter;

    public PlayerQueueFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView =  inflater.inflate(R.layout.fragment_player_queue, container, false);
        ButterKnife.inject(this, rootView);

        mQueueAdapter = new QueueAdapter(getActivity());
        mQueueListView.setAdapter(mQueueAdapter);
        mQueueListView.setDismissCallback(new EnhancedListView.OnDismissCallback() {
            @Override
            public EnhancedListView.Undoable onDismiss(final EnhancedListView enhancedListView, int i) {
                try {
                    new LeaveQueueOperation().run((QueueItem) mQueueListView.getItemAtPosition(i));
                    mQueueAdapter.loadObjects();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
        mQueueListView.enableSwipeToDismiss();

        return rootView;
    }

    public void onResume() {
        super.onResume();
        mQueueAdapter.loadObjects();
    }

    @OnItemLongClick(R.id.queue_list_view)
    boolean onPlayerLongClick(int position) {
        ChooseCharactersActivity.chooseCharacter(getActivity(), mQueueAdapter.getItem(position).getPlayer());
        return true;
    }

    static class QueueAdapter extends ParseQueryAdapter<QueueItem>{

        public QueueAdapter(Context context) {
            super(context, new QueryFactory<QueueItem>() {
                @Override
                public ParseQuery<QueueItem> create() {
                    ParseQuery<QueueItem> query = ParseQuery.getQuery(QueueItem.class);
                    query.orderByDescending("createdAt");
                    query.include("player.person");
                    query.include("player.characterA");
                    return query;
                }
            });
            setPaginationEnabled(false);
        }

        @Override
        public View getItemView(final QueueItem queueItem, View v, ViewGroup parent) {
            if (v == null) {
                v = View.inflate(getContext(), R.layout.player_queue_item, null);
                v.setTag(new PlayerQueueItemViewHolder(v));
            }
            super.getItemView(queueItem, v, parent);

            final PlayerQueueItemViewHolder viewHolder = (PlayerQueueItemViewHolder) v.getTag();
            viewHolder.textView.setText(queueItem.getPlayer().getPerson().getName());
            viewHolder.imageView.setImageResource(CharacterPresenter.getImageResource(getContext(), queueItem.getPlayer().getCharacterA()));

            return v;
        }

        class PlayerQueueItemViewHolder {
            @InjectView(R.id.player_name) TextView textView;
            @InjectView(R.id.player_character_image) ImageView imageView;

            public PlayerQueueItemViewHolder(View view) { ButterKnife.inject(this, view); }
        }
    }
}


@@ -524,16 +524,93 @@
 apter;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0A
 import b
@@ -633,16 +633,16 @@
 rKnife;%0A
-
 import b
@@ -1887,24 +1887,132 @@
 sition(i));%0A
+                    mQueueAdapter.dismissItem(i);%0A                    mQueueAdapter.notifyDataSetChanged();%0A
             
@@ -2680,16 +2680,70 @@
 ueItem%3E%7B
+%0A        private List%3CInteger%3E mNonDismissedPositions;
 %0A%0A      
@@ -3238,32 +3238,32 @@
             %7D);%0A
-
             setP
@@ -3282,24 +3282,1221 @@
 bled(false);
+%0A%0A            addOnQueryLoadListener(new OnQueryLoadListener%3CQueueItem%3E() %7B%0A                @Override%0A                public void onLoading() %7B%0A                    // Must be implemented%0A                %7D%0A%0A                @Override%0A                public void onLoaded(List%3CQueueItem%3E queueItems, Exception e) %7B%0A                    // Initialize the array of non-dismissed positions to include all queried items%0A%0A                    Integer%5B%5D positionsArray;%0A                    positionsArray = new Integer%5BqueueItems.size()%5D;%0A                    for (int i = 0; i %3C queueItems.size(); i++) %7B%0A                        positionsArray%5Bi%5D = i;%0A                    %7D%0A                    mNonDismissedPositions = new ArrayList(Arrays.asList(positionsArray));%0A                %7D%0A            %7D);%0A        %7D%0A%0A        @Override%0A        public int getCount() %7B%0A            return mNonDismissedPositions != null ? mNonDismissedPositions.size() : 0;%0A        %7D%0A%0A        @Override%0A        public QueueItem getItem(int position) %7B%0A            return super.getItem(mNonDismissedPositions.get(position));%0A        %7D%0A%0A        public void dismissItem(int position) %7B%0A            mNonDismissedPositions.remove(position);
 %0A        %7D%0A%0A

Allow QueueAdapter to locally dismiss items