package com.lukouguoji.aerologic.page.message.list

import androidx.lifecycle.MutableLiveData
import com.lukouguoji.aerologic.R
import com.lukouguoji.aerologic.page.message.details.MessageDetailsActivity
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.MessageBean
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.formatDate
import com.lukouguoji.module_base.ktx.launchCollect
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lukouguoji.module_base.util.CheckUtil
import com.lukouguoji.module_base.util.Common
import com.lukouguoji.module_base.util.DictUtils
import dev.DevUtils
import dev.utils.DevFinal
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils
import okhttp3.RequestBody

class MessageListViewModel : BasePageViewModel() {

    val startDate = MutableLiveData(DateUtils.getCurrentTime().formatDate())

    val endDate = MutableLiveData("")

    // 搜索关键字
    val keyWord = MutableLiveData("")

    // 处理状态
    val status = MutableLiveData("")

    // 处理状态列表
    val statusList = listOf(
        KeyValue("全部", ""),
        KeyValue("未处理", "0"),
        KeyValue("已处理", "1"),
    )

    val itemLayoutId = R.layout.item_message_list
    val itemViewHolder = MessageListViewHolder::class.java

    val countDesc = MutableLiveData("合计: 0条, 未处理: 0条, 已处理: 0条")
    private var count = 0

    ///////////////////////////////////////////////////////////////////////////
    // 方法区
    ///////////////////////////////////////////////////////////////////////////

    override fun getData() {
        val body = mapOf(
            "pageNum" to pageModel.page,
            "pageSize" to pageModel.limit,
            "beginDate" to startDate.value,
            "endDate" to endDate.value,
            "content" to keyWord.value,
            "state" to status.value,
        ).toRequestBody()
        launchLoadingCollect({
            NetApply.api.getMessageList(
                body
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)

                count = it.total
                getCountData(body)
            }
        }
    }

    private fun getCountData(body: RequestBody) {
        launchCollect({
            NetApply.api.simplePost("msg/msgStateQuery", body)
        }) {
            onSuccess = {
                countDesc.value =
                    "合计: ${count}条, " +
                            "未处理: ${it.data?.unread.noNull("0")}条, " +
                            "已处理: ${it.data?.read.noNull("0")}条"
            }
        }
    }

    fun onAllSelectClick() {
        pageModel.rv?.commonAdapter()?.items?.let {
            val list = it as List<MessageBean>
            CheckUtil.handleAllCheck(list.filter { b -> b.state == "0" })
        }
    }

    override fun onItemClick(position: Int, type: Int) {
        val bean = pageModel.rv?.commonAdapter()?.getItem(position) as MessageBean
        MessageDetailsActivity.start(DevUtils.getTopActivity(), bean)
    }

    fun onConfirmClick() {
        val list = pageModel.rv?.commonAdapter()?.items?.filter {
            (it as MessageBean).checked.get()
        } ?: emptyList()
        if (list.isEmpty()) {
            showToast("请选择需要处理的数据")
            return
        }
        launchLoadingCollect({
            NetApply.api.anyPost(
                "msg/handleList",
                list.map { (it as MessageBean).id }.toRequestBody()
            )
        }) {
            onSuccess = {
                showToast(it.msg.noNull("处理成功"))
                refresh()
            }
        }
    }

}