package com.lukouguoji.aerologic.page.car.list

import android.app.Activity
import android.content.Intent
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import com.gainscha.sdk2.e
import com.lukouguoji.aerologic.R
import com.lukouguoji.aerologic.page.car.add.CarDetailsActivity
import com.lukouguoji.aerologic.page.car.chart.CarChartActivity
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.FlatcarBean
import com.lukouguoji.module_base.bean.PrinterNum
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lukouguoji.module_base.model.BluetoothDialogModel
import com.lukouguoji.module_base.model.ScanModel
import com.lukouguoji.module_base.util.DictUtils
import com.lukouguoji.module_base.util.PrinterUtils
import dev.DevUtils
import dev.utils.app.info.KeyValue


class CarListViewModel : BasePageViewModel() {

    // 位置
    val location = MutableLiveData("")

    // 状态
    val status = MutableLiveData("")
    //打印份数
    val printNum = MutableLiveData("1")
    val dataBean = MutableLiveData(PrinterNum())

    // 车号
    val carStart = MutableLiveData("")
    val carEnd = MutableLiveData("")

    // 位置列表
    val locationList = MutableLiveData<List<KeyValue>>(listOf(KeyValue("占位", ""))).apply {
        DictUtils.getCarLocationList {
            value = it
        }
    }

    // 状态列表
    val statusList = MutableLiveData<List<KeyValue>>(
        listOf(
            KeyValue("全部", ""),
            KeyValue("正常", "0"),
            KeyValue("故障", "1"),
        )
    )

    // 合计数量
    val count = MutableLiveData(0)

    val itemLayoutId = R.layout.item_car_list
    val itemViewHolder = CarListViewHolder::class.java

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

    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getCarList(
                mapOf(
                    "pageNum" to pageModel.page,
                    "pageSize" to pageModel.limit,
                    "position" to location.value,
                    "status" to status.value,
                    "carStart" to carStart.value,
                    "carEnd" to carEnd.value,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                it.list?.forEach { bean ->
                    bean.positionName =
                        locationList.value?.find { b -> b.value == bean.position }?.key ?: ""
                }
                pageModel.handleListBean(it)
                count.value = it.total
            }
        }
    }

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

    fun onCarScanClickCarEnd() {
        ScanModel.startScan(DevUtils.getTopActivity(), Constant.RequestCode.CAREND)
    }

    fun onAddClick() {
        CarDetailsActivity.start(DevUtils.getTopActivity())
    }

    fun onPrintClick() {
        var num =  printNum.value?.toIntOrNull() ?: 1

        val list =
            (pageModel.rv?.commonAdapter()?.items?.filter { (it as FlatcarBean).checked.get() }
                ?: emptyList())
                    as List<FlatcarBean>
        if (list.isEmpty()) {
            showToast("请选择要打印的车辆")
            return
        }


        BluetoothDialogModel()
            .showCallBack {
                var count = 0
                //打印次数
                while (count < num) {
                    // 循环体
                    list.forEach{
                        PrinterUtils.printCar(it.carId)
                    }
                    count++
                }
            }

    }

    fun onChartClick() {
        CarChartActivity.start(DevUtils.getTopActivity())
    }

    override fun onItemClick(position: Int, type: Int) {
        val bean = pageModel.rv!!.commonAdapter()!!.getItem(position) as FlatcarBean
        when (type) {
            R.id.ll_content -> {
                CarDetailsActivity.start(DevUtils.getTopActivity(), bean.carId)
            }

            R.id.tv_modify -> {
                CarDetailsActivity.start(DevUtils.getTopActivity(), bean.carId, true)
            }

            else -> {}
        }
    }

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

                else -> {}
            }
        }
    }


}