package com.sixin.module_p.ui.gnj.xieji.list

import android.app.Activity
import android.content.Intent
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GnjUnloadListBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.ktx.showConfirmDialog
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lukouguoji.module_base.model.ScanModel
import com.sixin.module_p.R
import com.sixin.module_p.ui.gnj.xieji.add.PdaGnjXieJiAddActivity
import com.sixin.module_p.ui.gnj.xieji.details.PdaGnjXieJiDetailsActivity
import com.sixin.module_p.ui.gnj.xieji.dialog.PdaGnjXieJiFilterDialogModel
import dev.DevUtils
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

class PdaGnjXieJiListViewModel : BasePageViewModel() {

    val car = MutableLiveData("")

    val count = MutableLiveData(0)

    val itemLayoutId = R.layout.item_pda_gnj_xieji_list
    val itemViewHolder = PdaGnjXieJiListViewHolder::class.java

    private val dialogModel = PdaGnjXieJiFilterDialogModel {
        refresh()
    }

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


    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getGnjUnloadList(
                mapOf(
                    "fdate" to dialogModel.date.value,
                    "fno" to dialogModel.flightNo.value,
                    "carId" to car.value,
                    "onlyMe" to dialogModel.onlyMe.value
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
            }
        }
    }

    fun onCarChange(focus: Boolean = false) {
        if (focus) {
            return
        }
        getData()
    }

    fun onFilterClick() {
        dialogModel.show()
    }

    fun onAddClick() {
        PdaGnjXieJiAddActivity.start(DevUtils.getTopActivity(), GnjUnloadListBean())
    }

    override fun onItemClick(position: Int, type: Int) {
        val adapter = pageModel.rv!!.commonAdapter()!!
        val bean = adapter.getItem(position) as GnjUnloadListBean
        when (type) {
            R.id.ll_content -> {
                PdaGnjXieJiDetailsActivity.start(DevUtils.getTopActivity(), bean)
            }
            R.id.tv_modify -> {
                PdaGnjXieJiAddActivity.start(DevUtils.getTopActivity(), bean)
            }
            R.id.tv_delete -> {
                getTopActivity().showConfirmDialog("是否删除该航班下所有平板车信息?") {
                    launchLoadingCollect({
                        NetApply.api.anyGetForId(
                            "unloadingAirCraft/deleteByFid", bean.fid.noNull()
                        )
                    }) {
                        onSuccess = {
                            showToast(it.msg.noNull("删除成功"))
                            adapter.removeItem(position)
                            count.value = count.value?.minus(1)
                        }
                    }
                }
            }

            else -> {}
        }
    }

    fun scanClick() {
        ScanModel.startScan(getTopActivity(), Constant.RequestCode.CAR)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (data == null || resultCode != Activity.RESULT_OK) {
            return
        }
        when (requestCode) {
            Constant.RequestCode.CAR -> {
                car.value = data.getStringExtra(Constant.Result.CODED_CONTENT).noNull()
                onCarChange()
            }

            else -> {}
        }
    }
}