package com.abbidot.tracker.base

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.qmuiteam.qmui.recyclerView.QMUISwipeAction
import com.qmuiteam.qmui.recyclerView.QMUISwipeAction.ActionBuilder
import com.qmuiteam.qmui.recyclerView.QMUISwipeViewHolder
import com.qmuiteam.qmui.util.QMUIDisplayHelper


/**
 *Created  by .yzq on 2021/11/20/020.
 * @link
 * @description:带有滑动删除菜单列表适配器，配合DiffUtil使用取消notifyDataSetChanged()方法
 */
abstract class BaseSwipeMenuAdapter<T>(context: Context, list: MutableList<T>) :
    RecyclerView.Adapter<QMUISwipeViewHolder>() {

    private var mData: MutableList<T> = list
    private val mContext: Context = context
    private val mInflater: LayoutInflater = LayoutInflater.from(context)

    private var mClickListener: BaseRecyclerAdapter.OnItemClickListener? = null
    private var mLongClickListener: BaseRecyclerAdapter.OnItemLongClickListener? = null

    // 是否显示空布局，默认显示
    private var showEmptyView = true

    //重试事件
    private var mAgainClickListener: BaseRecyclerAdapter.OnAgainClickListener? = null

    /**
     * viewType--分别为item以及空view
     */
    private val mViewTypeEmpty = 0
    private val mViewTypeItem = 1

    //菜单样式Builder
    var mActionBuilder: ActionBuilder =
        ActionBuilder().textSize(QMUIDisplayHelper.sp2px(context, 14)).textColor(Color.WHITE)
                .paddingStartEnd(QMUIDisplayHelper.dp2px(context, 14))


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QMUISwipeViewHolder {
        //空布局
        if (getEmptyLayoutId(viewType) != 0 && viewType == mViewTypeEmpty) {
            val itemView: View = mInflater.inflate(getEmptyLayoutId(viewType), parent, false)
            val swipeViewHolder = QMUISwipeViewHolder(itemView)
            if (null != mAgainClickListener) {
                itemView.setOnClickListener {
                    showEmptyView(false)
                    mAgainClickListener!!.onAgainListener()
                }
            }
            return swipeViewHolder
        } else {
            val itemView: View = mInflater.inflate(getItemLayoutId(viewType), parent, false)
            val swipeViewHolder = QMUISwipeViewHolder(itemView)
            //添加滑动菜单
            for (swipeAction in getSwipeActions()) {
                swipeViewHolder.addSwipeAction(swipeAction)
            }
            if (null != mClickListener) {
                itemView.setOnClickListener {
                    mClickListener!!.onItemClick(itemView, swipeViewHolder.adapterPosition)
                }
            }
            if (null != mLongClickListener) {
                itemView.setOnLongClickListener {
                    mLongClickListener!!.onItemLongClick(itemView, swipeViewHolder.adapterPosition)
                    true
                }
            }
            return swipeViewHolder
        }
    }

    override fun onBindViewHolder(holder: QMUISwipeViewHolder, position: Int) {
        if (mData.size > 0) bindData(holder, position, mData[position])
    }

    /**
     * 判断是否是空布局
     */
    private fun isEmptyPosition(position: Int): Boolean {
        val count = mData.size
        return position == 0 && showEmptyView && count == 0
    }

    /**
     * 设置空布局显示。默认显示
     */
    private fun showEmptyView(isShow: Boolean) {
        if (isShow != showEmptyView) {
            showEmptyView = isShow
            notifyDataSetChanged()
        }
    }

    override fun getItemCount() = if (mData.size == 0 && showEmptyView) 1 else mData.size

    override fun getItemViewType(position: Int) =
        if (isEmptyPosition(position)) mViewTypeEmpty else mViewTypeItem

    fun setData(list: MutableList<T>?) {
        if (list != null) {
            mData = list
            //出现没有数据就显示空布局
            if (mData.size == 0) {
                showEmptyView(true)
            }
        }
    }

    fun getData() = mData

    fun remove(oldList: MutableList<T>, pos: Int): MutableList<T> {
        val newList: MutableList<T> = oldList.toMutableList()
        newList.removeAt(pos)
        return newList
    }

    fun getItem(pos: Int) = mData[pos]

    /**
     * 设置显示空布局 等于0不显示空布局
     */
    abstract fun getEmptyLayoutId(viewType: Int): Int

    //菜单集合
    abstract fun getSwipeActions(): MutableList<QMUISwipeAction>
    abstract fun getItemLayoutId(viewType: Int): Int
    abstract fun bindData(holder: QMUISwipeViewHolder?, position: Int, item: T)

    fun setOnItemClickListener(listener: BaseRecyclerAdapter.OnItemClickListener?) {
        mClickListener = listener
    }

    fun setOnItemLongClickListener(listener: BaseRecyclerAdapter.OnItemLongClickListener?) {
        mLongClickListener = listener
    }

    fun setOnAgainClickListener(listener: BaseRecyclerAdapter.OnAgainClickListener?) {
        mAgainClickListener = listener
    }

}