package com.lukouguoji.aerologic.page.telegram.list

import androidx.lifecycle.MutableLiveData
import com.lukouguoji.aerologic.R
import com.lukouguoji.aerologic.page.telegram.dialog.TelegramShowDialogModel
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.TelegramBean
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.getActivity
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lukouguoji.module_base.model.PadFilterDialogModel
import com.lukouguoji.module_base.model.PadFilterModel
import com.lukouguoji.module_base.util.Common
import com.lukouguoji.module_base.util.DictUtils
import dev.DevUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils
import java.util.Calendar

class TelegramListViewModel : BasePageViewModel() {

    // 开始时间
    val startDate = MutableLiveData(DateUtils.getCurrentTime().formatDate())

    // 结束时间
    val endDate = MutableLiveData("")

    // 报文类型
    val telegramType = MutableLiveData("")
    val telegramTypeList = MutableLiveData(listOf(KeyValue("占位", "1"))).apply {
        DictUtils.getTelegramTypeList {
            value = listOf(KeyValue("全部", "")) + it
        }
    }

    // 流向类型
    val flowType = MutableLiveData("")
    val flowTypeList = MutableLiveData<List<KeyValue>>(
        listOf(
            KeyValue("全部", ""),
            KeyValue("接收", "0"),
            KeyValue("发送", "1"),
        )
    )

    // 发报地址
    val address = MutableLiveData("")

    // 检索内容
    val keyWord = MutableLiveData("")

    val itemLayoutId = R.layout.item_telegram_list
    val itemViewHolder = TelegramListViewHolder::class.java

    private val filterModel by lazy {
        PadFilterDialogModel(
            getTopActivity(), listOf(
                PadFilterModel(
                    keyWord, title = "报文检索内容", hint = "请输入报文检索内容"
                )
            )
        ) {
            refresh()
        }
    }

    val count = MutableLiveData(0)

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

    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getTelegramList(
                mapOf(
                    "pageNum" to pageModel.page,
                    "pageSize" to pageModel.limit,
                    "startTime" to startDate.value,
                    "endTime" to endDate.value,
                    "flow" to flowType.value,
                    "msgType" to telegramType.value,
                    "sendAddress" to address.value,
                    "teleContent" to keyWord.value,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
            }
        }
    }

    fun onFilterClick() {
        filterModel.show()
    }

    override fun onItemClick(position: Int, type: Int) {
        val bean = pageModel.rv!!.commonAdapter()!!.getItem(position) as TelegramBean
        launchLoadingCollect({
            NetApply.api.getTelegramDetails(bean.flow, bean.id)
        }) {
            onSuccess = {
                TelegramShowDialogModel(it.msg.noNull(bean.teleContent)).show()
            }
        }
    }

}