package com.abbidot.tracker.deprecated.ui.activity

import android.widget.Toast
import androidx.recyclerview.widget.DiffUtil
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.list.RecyclerViewHolder
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.base.BaseDiffCallBack
import com.abbidot.tracker.bean.DeviceBean
import com.abbidot.tracker.databinding.ActivityDetailBinding
import com.abbidot.tracker.dialog.DialogUtils
import com.abbidot.tracker.util.ViewUtil
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog

class DetailActivity : BaseActivity<ActivityDetailBinding>(ActivityDetailBinding::inflate) {

    private lateinit var mAdapter: BaseRecyclerAdapter<DeviceBean>
    private lateinit var list: MutableList<DeviceBean>

    override fun getTopBar() = mViewBinding.titleGg.titleTopBar

    override fun initData() {
        super.initData()
        //设置状态栏黑色字体与图标
        QMUIStatusBarHelper.setStatusBarLightMode(this)
        XEventBus.post(EventName.Test, "cccccccccccccccccccc")

        setTopBarTitle(R.string.app_name)

//        list = mutableListOf(
//                DeviceBean(5, 4, "tesdfd", conState = ConState.CONNECTED, "7547sgdgdgy64e4r"),
//                DeviceBean(3, 2, "65757", conState = ConState.DISCONNECTED, "gujghj"))

        mAdapter = object : BaseRecyclerAdapter<DeviceBean>(this, 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: DeviceBean) {
                holder!!.setText(R.id.sdagfdsgg, item.deviceName)
            }
        }

        ViewUtil.instance.setRecyclerViewVerticalLinearLayout(mContext,mViewBinding.recyclerView, mAdapter)

        Toast.makeText(application, AppUtils.isDebug().toString(), Toast.LENGTH_LONG).show()

//        val newList: MutableList<DeviceBean> = ArrayList()
        mViewBinding.btnTest.setOnClickListener {
            val newList: MutableList<DeviceBean> = ArrayList()
            list = mAdapter.getData()
            for (i in 0 until list.size) {
                newList.add(list[i].copy())
            }

//            newList.add(DeviceBean(list.size, 4, Math.random().toString(),
//                    conState = ConState.CONNECTED, "7547sgdgdgy64e4r"))
            onRefreshData(newList)

            showToast("add")
        }

        mViewBinding.testRemove.setOnClickListener {
            val newList: MutableList<DeviceBean> = ArrayList()
            list = mAdapter.getData()
            for (i in 0 until list.size) {
                newList.add(list[i].copy())
            }
            newList.removeAt(2)
            onRefreshData(newList)
            DialogUtils.showTipDialog(mActivity!!, mViewBinding.testRemove, "remove succes",
                    QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
        }
    }

    private fun onRefreshData(newList: MutableList<DeviceBean>) {
        mAdapter.setData(newList)


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

                override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
                    list[oldItemPosition].deviceName == newList[newItemPosition].deviceName

            }, true)


        diffResult.dispatchUpdatesTo(mAdapter)
    }

}