package com.lukouguoji.gnc.page.shouyun.details.dialog

import android.app.Dialog
import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import com.lukouguoji.gnc.R
import com.lukouguoji.gnc.databinding.DialogGncShouYunSyncBinding
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.bean.GncQueryDetailsBean
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 dev.DevUtils

class GncShouYunSyncDialogModel(
    val waybill:String,
    val list: List<ShouYunSyncBean>,
    val callBack: (GncShouYunSyncDialogModel) -> Unit
) : BaseDialogModel<DialogGncShouYunSyncBinding>(DIALOG_TYPE_CENTER) {

    override fun onBuild(builder: XPopup.Builder) {
        builder.dismissOnBackPressed(false)
            .dismissOnTouchOutside(false)
            .autoDismiss(false)
    }
    val viewHolder = GncShouYunSyncViewHolder::class.java
    val itemLayoutId = R.layout.item_gnc_shou_yun_sync
    val number = MutableLiveData("0")
    val weight = MutableLiveData("0")
    val newList =  MutableLiveData<List<ShouYunSyncBean>>()

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

    override fun onDialogCreated(context: Context) {
        binding.model = this
        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() {
        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()
    }
}