package org.stepic.droid.ui.adapters;

import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import org.stepic.droid.model.Option;

import java.util.Collections;
import java.util.List;

import timber.log.Timber;

public class MatchingStepEnhancedAdapter extends SortingStepEnhancedAdapter {
    int draggableUpperBound;

    public MatchingStepEnhancedAdapter(List<Option> data) {
        super(data);
        draggableUpperBound = (data.size() / 2) - 1;
    }

//    @Override
//    public ItemDraggableRange onGetItemDraggableRange(OptionViewHolder holder, int position) {
//        return new ItemDraggableRange(0, draggableUpperBound);
//    }


    @Override
    public OptionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final OptionViewHolder optionViewHolder = super.onCreateViewHolder(parent, viewType);
        optionViewHolder.container.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int localHeight = optionViewHolder.container.getMeasuredHeight();
                int localWidth = optionViewHolder.container.getMeasuredWidth();
                Timber.d("MeasuredHeight = %s", localHeight);
                if (localHeight != 0) {
                    optionViewHolder.container.getLayoutParams().height = localHeight;
                    optionViewHolder.container.getViewTreeObserver().removeOnPreDrawListener(this);
                    return true;
                } else {
                    return false;
                }
            }
        });
        return optionViewHolder;
    }

    @Override
    public boolean onCheckCanStartDrag(OptionViewHolder holder, int position, int x, int y) {
        if (position % 2 == 0) {
            return false;
        } else {
            return super.onCheckCanStartDrag(holder, position, x, y);
        }
    }

    @Override
    public void onMoveItem(int fromPosition, int toPosition) {
        if (fromPosition == toPosition) {
            return;
        }
        Collections.swap(data, toPosition, fromPosition);
        notifyDataSetChanged();
    }

    @Override
    public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
        boolean canDrop = dropPosition % 2 != 0;
        return canDrop;
    }
}

@@ -303,37 +303,8 @@
 er %7B
-%0A    int draggableUpperBound;
 %0A%0A  
@@ -390,250 +390,9 @@
     
-    draggableUpperBound = (data.size() / 2) - 1;%0A    %7D%0A%0A//    @Override%0A//    public ItemDraggableRange onGetItemDraggableRange(OptionViewHolder holder, int position) %7B%0A//        return new ItemDraggableRange(0, draggableUpperBound);%0A//    %7D%0A
+%7D
 %0A%0A  
@@ -848,88 +848,8 @@
 ();%0A
-                int localWidth = optionViewHolder.container.getMeasuredWidth();%0A
     
@@ -1133,24 +1133,38 @@
 (this);%0A    
+            %7D%0A
             
@@ -1184,85 +1184,8 @@
 ue;%0A
-                %7D else %7B%0A                    return false;%0A                %7D%0A
     
@@ -1362,20 +1362,23 @@
         
-if (
+return 
 position
@@ -1386,77 +1386,15 @@
 %25 2 
-== 0) %7B%0A            return false;%0A        %7D else %7B%0A            return
+!= 0 &&
  sup
@@ -1441,26 +1441,16 @@
  x, y);%0A
-        %7D%0A
     %7D%0A%0A 
@@ -1796,25 +1796,14 @@
     
-boolean canDrop =
+return
  dro
@@ -1826,32 +1826,8 @@
  0;%0A
-        return canDrop;%0A
     

refactor code in Matching Adapter