package com.gingersoft.gsa.cloud.ui.recylcler.event;

import android.graphics.Color;

import com.gingersoft.gsa.cloud.base.common.bean.TableArea;
import com.gingersoft.gsa.cloud.base.common.bean.TableItem;
import com.gingersoft.gsa.cloud.base.utils.log.LogUtil;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.base.delegate.IActivity;
import com.qmuiteam.qmui.widget.section.QMUIDefaultStickySectionAdapter;
import com.qmuiteam.qmui.widget.section.QMUISection;
import com.qmuiteam.qmui.widget.section.QMUIStickySectionAdapter;

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

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 作者：ELEGANT_BIN
 * 版本：1.6.0
 * 创建日期：2020-06-10
 * 修订历史：2020-06-10
 * 描述：
 */

public class RecyItemTouchHelperCallback<H extends QMUISection.Model<H>, T extends QMUISection.Model<T>> extends ItemTouchHelper.Callback {
    private final String TAG = getClass().getSimpleName();
    RecyclerView.Adapter mAdapter;
    boolean isSwipeEnable;
    boolean isFirstDragUnable;

    public RecyItemTouchHelperCallback(RecyclerView.Adapter adapter) {
        mAdapter = adapter;
        isSwipeEnable = true;
        isFirstDragUnable = false;
    }

    public RecyItemTouchHelperCallback(RecyclerView.Adapter adapter, boolean isSwipeEnable, boolean isFirstDragUnable) {
        mAdapter = adapter;
        this.isSwipeEnable = isSwipeEnable;
        this.isFirstDragUnable = isFirstDragUnable;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
            int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
            int swipeFlags = 0;
            return makeMovementFlags(dragFlags, swipeFlags);
        } else {
            int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
            return makeMovementFlags(dragFlags, swipeFlags);
        }
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        int fromPosition = viewHolder.getAdapterPosition();
        int toPosition = target.getAdapterPosition();
        if (isFirstDragUnable && toPosition == 0) {
            return false;
        }
        if (fromPosition < toPosition) {
            if (mAdapter instanceof QMUIStickySectionAdapter) {
                QMUIStickySectionAdapter sectionAdapter = (QMUIStickySectionAdapter) mAdapter;
                QMUIStickySectionAdapter.ViewHolder holder = (QMUIStickySectionAdapter.ViewHolder) viewHolder;
                QMUISection<H, T> qmuiSections = sectionAdapter.getSection(holder.getAdapterPosition());
                List<?> list = qmuiSections.getItemList();

                int fromIndex = sectionAdapter.getItemIndex(fromPosition);
                int toIndex = sectionAdapter.getItemIndex(toPosition);

                if ((fromIndex + 1) == list.size())
                    return false;

                for (int i = fromIndex; i < toIndex; i++) {
                    Collections.swap(list, i, i + 1);
                }
            } else {
                for (int i = fromPosition; i < toPosition; i++) {
                    Collections.swap(((DefaultAdapter) mAdapter).getInfos(), i, i + 1);
                }
            }
        } else {
            if (mAdapter instanceof QMUIStickySectionAdapter) {
                QMUIStickySectionAdapter sectionAdapter = (QMUIStickySectionAdapter) mAdapter;
                QMUIStickySectionAdapter.ViewHolder holder = (QMUIStickySectionAdapter.ViewHolder) viewHolder;
                QMUISection<H, T> qmuiSections = sectionAdapter.getSection(holder.getAdapterPosition());
                List<?> list = qmuiSections.getItemList();

                int fromIndex = sectionAdapter.getItemIndex(fromPosition);
                int toIndex = sectionAdapter.getItemIndex(toPosition);

                if ((fromIndex + 1) == list.size())
                    return false;

                for (int i = fromIndex; i > toIndex; i--) {
                    Collections.swap(list, i, i - 1);
                }
            } else {
                for (int i = fromPosition; i > toPosition; i--) {
                    Collections.swap(((DefaultAdapter) mAdapter).getInfos(), i, i - 1);
                }
            }
        }
        mAdapter.notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        int adapterPosition = viewHolder.getAdapterPosition();
        mAdapter.notifyItemRemoved(adapterPosition);
        if (mAdapter instanceof QMUIDefaultStickySectionAdapter) {
            ((QMUIDefaultStickySectionAdapter) mAdapter).getCurrentData().remove(adapterPosition);
        } else {
            ((DefaultAdapter) mAdapter).getInfos().remove(adapterPosition);
        }
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        viewHolder.itemView.setBackgroundColor(Color.WHITE);
        String ids = "";
        if (mAdapter instanceof QMUIStickySectionAdapter) {
            QMUIStickySectionAdapter adapter = (QMUIStickySectionAdapter) mAdapter;
            List<QMUISection<H, T>> qmuiSectionList = adapter.getCurrentData();
            ids = getTableIds(qmuiSectionList);
        } else {
            DefaultAdapter adapter = (DefaultAdapter) mAdapter;
            ids = getTableAreaIds(adapter.getInfos());
        }
//        mAdapter.notifyDataSetChanged();
        if (onMoveListener != null) {
            onMoveListener.onMoveFinish(ids);
        }
    }

    private String getTableIds(List<QMUISection<H, T>> qmuiSectionList) {
        StringBuilder builder = new StringBuilder();
        for (QMUISection<H, T> qmuiSections : qmuiSectionList) {
            for (T t : qmuiSections.getItemList()) {
                TableItem tableItem = (TableItem) t;
                if (tableItem.getLayoutType() == 0) {
                    builder.append(tableItem.getId());
                    builder.append(",");
                }
            }
        }
        String str = builder.toString();
        if (str.endsWith(",")) {
            str = str.substring(0, str.length() - 1);
        }
        return str;
    }

    private String getTableAreaIds(List<TableArea> tableAreaList) {
        StringBuilder builder = new StringBuilder();
        for (TableArea tableArea : tableAreaList) {
            builder.append(tableArea.getId());
            builder.append(",");
        }
        String str = builder.toString();
        if (str.endsWith(",")) {
            str = str.substring(0, str.length() - 1);
        }
        return str;
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return !isFirstDragUnable;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return isSwipeEnable;
    }

    private onMoveListener onMoveListener;

    public void setOnMoveListener(RecyItemTouchHelperCallback.onMoveListener onMoveListener) {
        this.onMoveListener = onMoveListener;
    }

    public interface onMoveListener {
        void onMoveFinish(String ids);
    }
}