package com.sixin.module_p.ui.gnc.shouyun.dialog

import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.bean.ShouYunSyncBean
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.interfaces.ILoading
import com.lukouguoji.module_base.ktx.launchCollect
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.limit
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.ktx.refresh
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lxj.xpopup.XPopup
import com.sixin.module_p.R
import com.sixin.module_p.databinding.DialogPdaGncShouYunSyncBinding
import dev.DevUtils

class GncPdaShouYunSyncDialogModel(
    val waybill: String,
    val list: List<ShouYunSyncBean>,
    val callBack: (GncPdaShouYunSyncDialogModel) -> Unit
) : BaseDialogModel<DialogPdaGncShouYunSyncBinding>(DIALOG_TYPE_FULL) {

    val viewHolder = GncPdaShouYunSyncViewHolder::class.java
    val itemLayoutId = R.layout.item_pda_gnc_shou_yun_sync
    val number = MutableLiveData("0")
    val weight = MutableLiveData("0")

    override fun onBuild(builder: XPopup.Builder) {
        builder.autoDismiss(false)
            .dismissOnBackPressed(false)
            .dismissOnTouchOutside(false)
            .enableDrag(false)
    }

    override fun layoutId(): Int {
        return R.layout.dialog_pda_gnc_shou_yun_sync
    }

    override fun onDialogCreated(context: Context) {
        binding.model = this
        binding.root.findViewById<TextView>(R.id.title_name).text = "同步操作"
        binding.root.findViewById<View>(R.id.tool_back).setOnClickListener { dismiss() }
        binding.root.findViewById<View>(R.id.tool_tv_back).visibility = View.GONE

        var totalPc = 0;
        var totalWeight = 0.0 ;
        for (obj in list){
            obj.pcLiveData.value = obj.pc
            obj.weightLiveData.value = obj.weight
            totalPc += obj.pc.toInt()
            totalWeight += obj.weight.toDouble()
        }
        number.value = totalPc.toString()
        weight.value = totalWeight.toString()

        // 监听每个 item 的 pc 变化
        list.forEach { item ->
            item.pcLiveData.observeForever { newValue ->
                changePcTotal()
            }
            item.weightLiveData.observeForever { newValue ->
                item.weight = newValue.toString()
                item.volume = ((newValue.toFloatOrNull() ?: 0f) / 220f).limit(2)
                changeWeightTotal()
            }
        }
        binding.rv.refresh(list)
    }

    fun confirmClick() {
        println("confirmClick:"+list.get(0).pc)
        for (obj in list){
            obj.pc = obj.pcLiveData.value.toString()
            obj.weight = obj.weightLiveData.value.toString()
        }

        callBack.invoke(this)
        dismiss()
    }

    fun changePcTotal(){
        println("changePcTotal...")
        var totalPc = 0;
        for (obj in list){
            if (obj.pcLiveData.value != ""){
                totalPc += obj.pcLiveData.value!!.toInt()
            }
        }
        number.value = totalPc.toString()
    }


    fun changeWeightTotal(){
        println("changeWeightTotal...")
        var totalWeight = 0.0 ;
        for (obj in list){
            if (obj.weight != ""){
                totalWeight += obj.weight.toDouble()
            }
        }
        weight.value = totalWeight.toString()
    }
}