package com.abbidot.tracker.ui.fragment.demo

import android.content.Context
import androidx.recyclerview.widget.DiffUtil
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.list.RecyclerViewHolder
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseDiffCallBack
import com.abbidot.tracker.base.BaseFragment
import com.abbidot.tracker.databinding.FragmentMineBinding
import com.abbidot.tracker.util.ViewUtil
import com.qmuiteam.qmui.widget.pullLayout.QMUIPullLayout


class MineFragment : BaseFragment<FragmentMineBinding>(FragmentMineBinding::inflate) {
    private lateinit var mAdapter: BaseRecyclerAdapter<String>
    private lateinit var list: MutableList<String>

    companion object {
        @JvmStatic
        fun newInstance(context: Context) = MineFragment().apply {
            mContext = context
        }
    }

    override fun initData() {
        list = mutableListOf()
        mAdapter = object : BaseRecyclerAdapter<String>(mContext!!, list) {
            override fun getEmptyLayoutId(viewType: Int) = R.layout.layout_empty_view

            override fun getItemLayoutId(viewType: Int) = R.layout.layout_list_item

            override fun bindData(holder: RecyclerViewHolder?, position: Int, item: String) {
                holder!!.setText(R.id.sdagfdsgg, item)
            }
        }

        ViewUtil.instance.setRecyclerViewDecorationLinearLayoutManager(mContext!!,
                mViewBinding.minePullLoad.pullRefreshAndLoadMoreRv, mAdapter)

        mViewBinding.minePullLoad.pullRefreshAndLoadMoreLayout.setActionListener { pullAction ->
            mViewBinding.minePullLoad.pullRefreshAndLoadMoreLayout.postDelayed({
                if (pullAction.pullEdge == QMUIPullLayout.PULL_EDGE_TOP) {
                    onRefreshData()
                } else if (pullAction.pullEdge == QMUIPullLayout.PULL_EDGE_BOTTOM) {
                    onLoadMore()
                }
                mViewBinding.minePullLoad.pullRefreshAndLoadMoreLayout.finishActionRun(pullAction)
            }, 1500)
        }

        mAdapter.setOnAgainClickListener(object : BaseRecyclerAdapter.OnAgainClickListener {
            override fun onAgainListener() {
                showLoading(true)
                mViewBinding.minePullLoad.pullRefreshAndLoadMoreRv.postDelayed(Runnable {
                    onRefreshData()
                    showLoading(false)
                }, 1500)

            }
        })
        showToast(R.string.txt_net_error)
    }

    private fun onLoadMore() {
        val id = System.currentTimeMillis()
        list = mAdapter.getData()
        val data: MutableList<String> = list.toMutableList()
        for (i in 0..9) {
            data.add("onLoadMore-$id-$i")
        }
        mAdapter.setData(data)

        val diffResult = DiffUtil.calculateDiff(object : BaseDiffCallBack<String>(list, data) {
            override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
                list[oldItemPosition] == data[newItemPosition]

            override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
                list[oldItemPosition] == data[newItemPosition]

        }, true)

        diffResult.dispatchUpdatesTo(mAdapter)
    }

    private fun onRefreshData() {
        val data: MutableList<String> = ArrayList()
        val id = System.currentTimeMillis()
        for (i in 0..20) {
            data.add("onRefreshData-$id-$i")
        }
        list = mAdapter.getData()
        mAdapter.setData(data)
        if (list.size == 0) {
            //防止空布局报错
            mAdapter.notifyDataSetChanged()
            return
        }

        val diffResult = DiffUtil.calculateDiff(object : BaseDiffCallBack<String>(list, data) {
            override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
                list[oldItemPosition] == data[newItemPosition]

            override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
                list[oldItemPosition] == data[newItemPosition]

        }, true)

        diffResult.dispatchUpdatesTo(mAdapter)

        //刷新完成重新定位到第一个数据
//        mViewBinding.recordPullLayout.pullRefreshRv.scrollToPosition(0)
    }
}