package com.lukouguoji.gjc.viewModel

import android.app.Activity.RESULT_OK
import android.content.Intent
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.activity.GjcBoxAssembleAddActivity
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GjcBoxAssembleBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.interfaces.IOnItemClickListener
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.launchCollect
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.ktx.trim
import com.lukouguoji.module_base.model.ScanModel
import com.lukouguoji.module_base.util.Common
import com.lukouguoji.module_base.util.DictUtils
import dev.DevUtils
import dev.utils.app.info.KeyValue
import java.util.*

class BoxAssembleViewModel : BasePageViewModel(), IOnItemClickListener {

    // 组装日期
    val date = MutableLiveData(Date().formatDate())

    // 平板车号
    val car = MutableLiveData("")

    // ULD编号
    val uld = MutableLiveData("")

    // 复磅状态
    val status = MutableLiveData("")

    // 截载状态
    val cutStatus = MutableLiveData("0")

    // 复磅状态列表
    val statusList = DictUtils.reWeightStatusList

    // 截载状态列表
    val cutStatusList = DictUtils.cutStatusList

    val count = MutableLiveData(0)

    /**
     * 获取数据
     */
    override fun getData() {
        launchCollect({
            NetApply.api.getBoxList(
                mapOf(
                    "carid" to car.value,
                    "uld" to uld.value,
                    "opdate" to date.value + " 00:00:00",
                    "cflag" to status.value,
                    "mcloseStatus" to cutStatus.value,
                ).trim().toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleDataList(it.data)
                count.postValue(it.data?.size)
            }

            onComplete = {
                pageModel.finishLoading()
            }
        }
    }

    /**
     * 组装日期点击
     */
    fun dateClick(view: View) {
        Common.onYearMonthDay(view.context.getActivity(), date.value) { year, month, day ->
            val calendar = Calendar.getInstance()
            calendar.set(year, month - 1, day)
            date.value = calendar.time.formatDate()
        }
    }

    /**
     * 平板车 扫描
     */
    fun carScan(view: View) {
        ScanModel.startScan(view.context.getActivity(), Constant.RequestCode.gjc_fubang_car_id)
    }

    /**
     * ULD编号 扫描
     */
    fun uldScan(view: View) {
        ScanModel.startScan(view.context.getActivity(), Constant.RequestCode.gjc_fubang_uld_id)
    }

    /**
     * 复磅状态 选中
     */
    fun statusSelected(position: Int) {
        status.value = statusList[position].value
    }

    /**
     * 截载状态 点击
     */
    fun cutStatusSelected(position: Int) {
        cutStatus.value = cutStatusList[position].value
    }

    /**
     * 搜索 点击
     */
    fun searchClick(view: View) {
        refresh()
    }

    /**
     * 添加 点击
     */
    fun addClick(view: View) {
        GjcBoxAssembleAddActivity.start(view.context, "")
    }

    /**
     * item 点击
     */
    override fun onItemClick(position: Int, type: Int) {
        val bean =
            pageModel.rv!!.commonAdapter()!!.getItem(position) as GjcBoxAssembleBean
        when (type) {
            R.id.tv_details -> {
                GjcBoxAssembleAddActivity.start(DevUtils.getTopActivity(), bean.locId.noNull())
            }

            R.id.tv_delete -> {
                Common.secondConfirmDialog(
                    DevUtils.getTopActivity().getActivity(),
                    "确认删除?"
                ) { dialog ->
                    dialog.dismiss()
                    deleteItem(bean.locId.noNull())
                }
            }

            else -> {}
        }
    }

    private fun deleteItem(id: String) {
        showLoading()
        launchCollect({
            NetApply.api.gjcBoxAssembleDelete(id)
        }) {
            onSuccess = {
                if (it.verifySuccess()) {
                    showToast(it.msg.noNull("删除成功"))
                    refresh()
                } else {
                    showToast(it.msg.noNull("删除失败"))
                }
            }

            onComplete = {
                dismissLoading()
            }
        }
    }

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

                Constant.RequestCode.gjc_fubang_uld_id -> {
                    uld.value =
                        data.getStringExtra(Constant.Result.CODED_CONTENT).noNull()
                }

                else -> {}
            }
        }
    }

}