package com.lukouguoji.gjc.viewModel

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.holder.GjcGoodsAddViewHolder
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GjcGoodsAddBean
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.finish
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.model.ScanModel
import com.lukouguoji.module_base.util.CheckUtil
import com.lukouguoji.module_base.util.Common
import java.util.Calendar
import java.util.Date

class GjcGoodsAddViewModel : BasePageViewModel() {

    // 航班日期
    var flightDate = MutableLiveData(Date().formatDate())

    // 航班号
    var flightNo = MutableLiveData<String>()

    // 板车号
    var truckNo = MutableLiveData<String>()

    // ULD编号
    var uldNo = MutableLiveData<String>()

    val itemId = R.layout.item_goods_add
    val itemHolder = GjcGoodsAddViewHolder::class.java

    // 列表数量
    val size = MutableLiveData(0)

    override fun getData() {
        launchCollect({
            NetApply.api
                .getGjcGoodsAddList(
                    mapOf(
                        "fdate" to flightDate.value,
                        "fno" to flightNo.value,
                        "carid" to truckNo.value,
                        "uld" to uldNo.value,
                    ).toRequestBody()
                )
        }) {
            onSuccess = {
                pageModel.handleDataList(it.data)
                size.value = it.data?.size ?: 0
            }
        }
    }

    override fun onItemClick(position: Int, type: Int) {
        val adapter = pageModel.rv!!.commonAdapter()!!
        val items = adapter.items
        when (type) {
            R.id.iv_show -> {
                CheckUtil.handleSingleCheck(items as List<GjcGoodsAddBean>, "show", position, true)
                adapter.notifyDataSetChanged()
            }

            R.id.tv_delete -> {
                adapter.removeItem(position)
                size.value = adapter.itemCount
            }

            else -> {}
        }
    }

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

    /**
     * 板车号 扫描
     */
    fun truckNoScan(view: View) {
        ScanModel.startScan(view.context.getActivity(), Constant.RequestCode.CAR)
    }

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

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

    /**
     * 确认点击
     */
    fun confirmClick(view: View) {
        val list = pageModel.rv?.commonAdapter()?.items?.map { (it as GjcGoodsAddBean).locId }
            ?: emptyList()
        if (list.isEmpty()) {
            showToast("请先添加货物")
            return
        }
        showLoading()
        launchCollect({
            NetApply.api
                .gjcGoodsAdd(
                    mapOf(
                        "locIdList" to list
                    ).toRequestBody()
                )
        }) {
            onSuccess = {
                if (it.verifySuccess()) {
                    FlowBus.with<Any>(ConstantEvent.GJC_HANDOVER_ADD_SUCCESS)
                        .tryEmit(true)
                    view.context.finish()
                } else {
                    showToast(it.msg.noNull("操作失败"))
                }
            }
            onComplete = {
                dismissLoading()
            }
        }
    }

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

                Constant.RequestCode.ULD -> {
                    val id = data.getStringExtra(Constant.Result.CODED_CONTENT).noNull()
                    uldNo.value = id
                }

                else -> {}
            }
        }
    }

}