package org.stepic.droid.ui.adapters;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.h6ah4i.android.widget.advrecyclerview.draggable.DraggableItemAdapter;
import com.h6ah4i.android.widget.advrecyclerview.draggable.ItemDraggableRange;
import com.h6ah4i.android.widget.advrecyclerview.utils.AbstractDraggableItemViewHolder;

import org.stepic.droid.R;
import org.stepic.droid.model.Option;
import org.stepic.droid.ui.custom.ProgressLatexView;
import org.stepic.droid.ui.util.ViewUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SortingStepEnhancedAdapter extends RecyclerView.Adapter<SortingStepEnhancedAdapter.OptionViewHolder>
        implements DraggableItemAdapter<SortingStepEnhancedAdapter.OptionViewHolder> {

    protected final static int DEFAULT_DRAGGABLE_VIEW_TYPE = 0;
    protected final List<Option> data;

    public SortingStepEnhancedAdapter(List<Option> data) {
        super();
        this.data = data;
        setHasStableIds(true);
    }

    @Override
    public long getItemId(int position) {
        return data.get(position).getPositionId();
    }

    @Override
    public OptionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.view_sorting_option, parent, false);
        return new OptionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(OptionViewHolder holder, int position) {
        String text = data.get(position).getValue();
        holder.enhancedText.setPlainOrLaTeXText(text);
    }

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

    @Override
    public boolean onCheckCanStartDrag(OptionViewHolder holder, int position, int x, int y) {
        return ViewUtils.hitTest(holder.sortController, x, y);
    }

    @Override
    public ItemDraggableRange onGetItemDraggableRange(OptionViewHolder holder, int position) {
        // no drag-sortable range specified
        return null;
    }

    @Override
    public void onMoveItem(int fromPosition, int toPosition) {
        if (fromPosition == toPosition) {
            return;
        }
        data.add(toPosition, data.remove(fromPosition));
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
        return true;
    }

    public static class OptionViewHolder extends AbstractDraggableItemViewHolder {

        @BindView(R.id.container)
        View container;

        @Nullable
        @BindView(R.id.sort_icon)
        View sortController;

        @BindView(R.id.option_text)
        ProgressLatexView enhancedText;


        public OptionViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }

}

@@ -912,24 +912,77 @@
 wHolder%3E %7B%0A%0A
+    @Nullable%0A    private RecyclerView recyclerView;%0A
     protecte
@@ -1033,16 +1033,16 @@
 PE = 0;%0A
-
     prot
@@ -2037,16 +2037,68 @@
   return
+ recyclerView != null && recyclerView.isEnabled() &&
  ViewUti
@@ -3153,10 +3153,374 @@
 %0A    %7D%0A%0A
+    @Override%0A    public void onAttachedToRecyclerView(RecyclerView recyclerView) %7B%0A        super.onAttachedToRecyclerView(recyclerView);%0A        this.recyclerView = recyclerView;%0A    %7D%0A%0A    @Override%0A    public void onDetachedFromRecyclerView(RecyclerView recyclerView) %7B%0A        super.onDetachedFromRecyclerView(recyclerView);%0A        recyclerView = null;%0A    %7D%0A
 %7D%0A

disable dragging quiz if recycler is locked