package de.danoeh.antennapod.adapter;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MotionEvent;
import android.view.View;

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.fragment.swipeactions.SwipeActions;
import de.danoeh.antennapod.view.viewholder.EpisodeItemViewHolder;

/**
 * List adapter for the queue.
 */
public class QueueRecyclerAdapter extends EpisodeItemListAdapter {
    private static final String TAG = "QueueRecyclerAdapter";

    private final SwipeActions swipeActions;
    private boolean dragDropEnabled;


    public QueueRecyclerAdapter(MainActivity mainActivity, SwipeActions swipeActions) {
        super(mainActivity);
        this.swipeActions = swipeActions;
        dragDropEnabled = ! (UserPreferences.isQueueKeepSorted() || UserPreferences.isQueueLocked());
    }

    public void updateDragDropEnabled() {
        dragDropEnabled = ! (UserPreferences.isQueueKeepSorted() || UserPreferences.isQueueLocked());
        notifyDataSetChanged();
    }

    @Override
    @SuppressLint("ClickableViewAccessibility")
    protected void afterBindViewHolder(EpisodeItemViewHolder holder, int pos) {
        View.OnTouchListener startDragTouchListener = (v1, event) -> {
            if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                Log.d(TAG, "startDrag()");
                swipeActions.startDrag(holder);
            }
            return false;
        };

        if (!dragDropEnabled || inActionMode()) {
            holder.dragHandle.setVisibility(View.GONE);
            holder.dragHandle.setOnTouchListener(null);
            holder.coverHolder.setOnTouchListener(null);
        } else {
            holder.dragHandle.setVisibility(View.VISIBLE);
            holder.dragHandle.setOnTouchListener(startDragTouchListener);
            holder.coverHolder.setOnTouchListener(startDragTouchListener);
        }

        holder.isInQueue.setVisibility(View.GONE);
    }

    @Override
    public void onCreateContextMenu(final ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.queue_context, menu);
        super.onCreateContextMenu(menu, v, menuInfo);

        if (!inActionMode()) {
            menu.findItem(R.id.multi_select).setVisible(true);
            final boolean keepSorted = UserPreferences.isQueueKeepSorted();
            if (getItem(0).getId() == getLongPressedItem().getId() || keepSorted) {
                menu.findItem(R.id.move_to_top_item).setVisible(false);
            }
            if (getItem(getItemCount() - 1).getId() == getLongPressedItem().getId() || keepSorted) {
                menu.findItem(R.id.move_to_bottom_item).setVisible(false);
            }
        } else {
            menu.findItem(R.id.move_to_top_item).setVisible(false);
            menu.findItem(R.id.move_to_bottom_item).setVisible(false);
        }
    }
}

@@ -224,16 +224,54 @@
 .View;%0A%0A
+import androidx.core.view.ViewCompat;%0A
 import d
@@ -293,16 +293,16 @@
 apod.R;%0A
-
 import d
@@ -1388,292 +1388,8 @@
 ) %7B%0A
-        View.OnTouchListener startDragTouchListener = (v1, event) -%3E %7B%0A            if (event.getActionMasked() == MotionEvent.ACTION_DOWN) %7B%0A                Log.d(TAG, %22startDrag()%22);%0A                swipeActions.startDrag(holder);%0A            %7D%0A            return false;%0A        %7D;%0A%0A
     
@@ -1732,105 +1732,952 @@
 ner(
-startDragTouchListener);%0A            holder.coverHolder.setOnTouchListener(startDragTouchListener
+(v1, event) -%3E %7B%0A                if (event.getActionMasked() == MotionEvent.ACTION_DOWN) %7B%0A                    Log.d(TAG, %22startDrag()%22);%0A                    swipeActions.startDrag(holder);%0A                %7D%0A                return false;%0A            %7D);%0A            holder.coverHolder.setOnTouchListener((v1, event) -%3E %7B%0A                if (event.getActionMasked() == MotionEvent.ACTION_DOWN) %7B%0A                    boolean isLtr = ViewCompat.getLayoutDirection(holder.itemView) == ViewCompat.LAYOUT_DIRECTION_LTR;%0A                    float factor = isLtr ? 1 : -1;%0A                    if (factor * event.getX() %3C factor * 0.5 * v1.getWidth()) %7B%0A                        Log.d(TAG, %22startDrag()%22);%0A                        swipeActions.startDrag(holder);%0A                    %7D else %7B%0A                        Log.d(TAG, %22Ignoring drag in right half of the image%22);%0A                    %7D%0A                %7D%0A                return false;%0A            %7D
 );%0A 

Make it harder to accidentally drag episodes in the queue