package com.casic.br.fragment

import android.os.CountDownTimer
import android.os.Handler
import androidx.recyclerview.widget.DividerItemDecoration
import com.casic.br.R
import com.casic.br.adapter.MessageListAdapter
import com.casic.br.extensions.initLayoutImmersionBar
import com.casic.br.extensions.showEmptyPage
import com.casic.br.model.LocaleMessageBean
import com.casic.br.utils.DataBaseManager
import com.pengxh.kt.lite.base.KotlinBaseFragment
import com.pengxh.kt.lite.utils.WeakReferenceHandler
import kotlinx.android.synthetic.main.fragment_message.*
import kotlinx.android.synthetic.main.include_empty_view.*
import kotlinx.android.synthetic.main.include_main_title.*

class MessagePageFragment : KotlinBaseFragment() {

    private lateinit var weakReferenceHandler: WeakReferenceHandler
    private lateinit var messageAdapter: MessageListAdapter
    private var dataBeans: MutableList<LocaleMessageBean> = ArrayList()
    private var pageIndex = 0 // 本地数据库分页从0开始
    private var isRefresh = false
    private var isLoadMore = false
    private var userId = ""

    override fun initLayoutView(): Int = R.layout.fragment_message

    override fun setupTopBarLayout() {
        requireContext().initLayoutImmersionBar(rootView)
        leftTitleView.text = "消息"
    }

    override fun initData() {
        weakReferenceHandler = WeakReferenceHandler(callback)
    }

    override fun observeRequestState() {

    }

    override fun initEvent() {
        messageLayout.setOnRefreshListener {
            isRefresh = true
            object : CountDownTimer(1000, 500) {
                override fun onTick(millisUntilFinished: Long) {

                }

                override fun onFinish() {
                    isRefresh = false
                    dataBeans.clear()
                    pageIndex = 0
                    dataBeans = DataBaseManager.instance.queryMessageByPage(userId, pageIndex)

                    it.finishRefresh()
                    weakReferenceHandler.sendEmptyMessage(2022070401)
                }
            }.start()
        }

        messageLayout.setOnLoadMoreListener {
            isLoadMore = true
            object : CountDownTimer(1000, 500) {
                override fun onTick(millisUntilFinished: Long) {

                }

                override fun onFinish() {
                    isLoadMore = false
                    pageIndex++
                    dataBeans.addAll(
                        DataBaseManager.instance.queryMessageByPage(userId, pageIndex)
                    )
                    it.finishLoadMore()
                    weakReferenceHandler.sendEmptyMessage(2022082301)
                }
            }.start()
        }
    }

    override fun onResume() {
        //默认加载第一页
        dataBeans = DataBaseManager.instance.queryMessageByPage(userId, pageIndex)
        weakReferenceHandler.sendEmptyMessage(2022082301)
        super.onResume()
    }

    private val callback = Handler.Callback {
        if (it.what == 2022082301) {
            if (isRefresh || isLoadMore) {
                messageAdapter.notifyDataSetChanged()
            } else {
                if (dataBeans.size == 0) {
                    emptyView!!.showEmptyPage("没有任何消息") {
                        pageIndex = 0
                        dataBeans = DataBaseManager.instance.queryMessageByPage(userId, pageIndex)
                        weakReferenceHandler.sendEmptyMessage(2022082301)
                    }
                } else {
                    emptyView!!.hide()
                    messageAdapter = MessageListAdapter(requireContext(), dataBeans)
                    messageRecyclerView.addItemDecoration(
                        DividerItemDecoration(
                            requireContext(), DividerItemDecoration.VERTICAL
                        )
                    )
                    messageRecyclerView.adapter = messageAdapter
                    messageAdapter.setOnItemClickListener(object :
                        MessageListAdapter.OnItemClickListener {
                        override fun onDeleteClicked(position: Int) {
//                            DataBaseManager.instance.deleteLocaleMessageById(
//                                userId, dataBeans[position].messageId
//                            )
//                            dataBeans.removeAt(position)
//                            messageAdapter.notifyItemRemoved(position)
//                            messageAdapter.notifyItemRangeChanged(
//                                position, dataBeans.size - position
//                            )
                        }

                        override fun onShowMoreClicked(position: Int) {
//                            deviceViewModel.obtainDeviceDetail(dataBeans[position].deviceCode)
//                            //更改数据库里通知状态
//                            DataBaseManager.instance.updateLocaleMessageById(
//                                userId, dataBeans[position].messageId
//                            )
//                            messageAdapter.notifyItemChanged(position)
//                            messageAdapter.notifyItemRangeChanged(
//                                position, dataBeans.size - position
//                            )
                        }
                    })
                }
            }
        }
        true
    }
}