package org.stepic.droid.ui.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import org.stepic.droid.R;
import org.stepic.droid.model.Option;

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

import timber.log.Timber;

public class MatchingStepEnhancedAdapter extends SortingStepEnhancedAdapter {

    protected static final int NOT_DRAGGABLE_VIEW_TYPE = 1;

    public MatchingStepEnhancedAdapter(List<Option> data) {
        super(data);
    }

    @Override
    public OptionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final OptionViewHolder optionViewHolder;
        if (viewType == NOT_DRAGGABLE_VIEW_TYPE) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            View view = inflater.inflate(R.layout.view_matching_first_option_enhanced, parent, false);
            optionViewHolder = new OptionViewHolder(view);
        } else if (viewType == DEFAULT_DRAGGABLE_VIEW_TYPE) {
            optionViewHolder = super.onCreateViewHolder(parent, viewType);
        } else {
            throw new IllegalArgumentException("Illegal view type of matching adapter");
        }

        optionViewHolder.container.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int localHeight = optionViewHolder.container.getMeasuredHeight();
                Timber.d("MeasuredHeight = %s, height = %s, viewId = %s", localHeight, optionViewHolder.container.getHeight(), optionViewHolder.container.toString());
                if (localHeight != 0) {
                    optionViewHolder.container.getLayoutParams().height = localHeight;
                    optionViewHolder.container.getViewTreeObserver().removeOnPreDrawListener(this);
                }
                return true;
            }
        });
        return optionViewHolder;
    }

    @Override
    public boolean onCheckCanStartDrag(OptionViewHolder holder, int position, int x, int y) {
        return position % 2 != 0 && 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) {
        return dropPosition % 2 != 0;
    }


    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return NOT_DRAGGABLE_VIEW_TYPE;
        } else {
            return super.getItemViewType(position);
        }
    }
}

@@ -191,16 +191,62 @@
 roid.R;%0A
+import org.stepic.droid.base.MainApplication;%0A
 import o
@@ -275,16 +275,16 @@
 Option;%0A
-
 %0Aimport 
@@ -497,16 +497,41 @@
 E = 1;%0A%0A
+    int optionHeightPx;%0A%0A
     publ
@@ -578,24 +578,24 @@
 on%3E data) %7B%0A
-
         supe
@@ -603,16 +603,131 @@
 (data);%0A
+        optionHeightPx = (int) MainApplication.getAppContext().getResources().getDimension(R.dimen.option_height);%0A
     %7D%0A%0A 
@@ -1702,195 +1702,40 @@
     
-Timber.d(%22MeasuredHeight = %25s, height = %25s, viewId = %25s%22, localHeight, optionViewHolder.container.getHeight(), optionViewHolder.container.toString());%0A                if (local
+if (localHeight %3E option
 Height
- != 0
+Px
 ) %7B%0A

fix bug with not measured latex webview in sorting quiz