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

import android.content.Intent
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.FragmentRecordBinding
import com.abbidot.tracker.deprecated.ui.activity.SwipeActionActivity
import com.abbidot.tracker.util.ViewUtil
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIPullRefreshLayout

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

//    companion object {
//        @JvmStatic
//        fun newInstance(context: Context) = RecordFragment().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.recordPullLayout.pullRefreshRv, mAdapter)

        mViewBinding.recordPullLayout.onlyPullToRefresh.setOnPullListener(object :
            QMUIPullRefreshLayout.OnPullListener {
            override fun onMoveTarget(offset: Int) {
            }

            override fun onMoveRefreshView(offset: Int) {
            }

            override fun onRefresh() {
                mViewBinding.recordPullLayout.onlyPullToRefresh.postDelayed(Runnable {
                    onDataLoaded()
                    mViewBinding.recordPullLayout.onlyPullToRefresh.finishRefresh()
                }, 1000)
            }
        })

        mViewBinding.swipeBtn.setOnClickListener {
            startActivity(Intent(mContext, SwipeActionActivity::class.java))
        }

        onDataLoaded()
    }

    private fun onDataLoaded() {
        val data: MutableList<String> = ArrayList(
                listOf("Helps", "Maintain", "Liver", "Health", "Function", "Supports", "Healthy",
                        "Fat", "Metabolism", "Nuturally", "Bracket", "Refrigerator", "Bathtub",
                        "Wardrobe", "Comb", "Apron", "Carpet", "Bolster", "Pillow", "Cushion"))
        data.shuffle()
        list = mAdapter.getData()
        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)
        //刷新完成重新定位到第一个数据
        mViewBinding.recordPullLayout.pullRefreshRv.scrollToPosition(0)
    }
}