package com.lukouguoji.module_base.bean

import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import com.lukouguoji.module_base.BR
import com.lukouguoji.module_base.ktx.limit
import com.lukouguoji.module_base.ktx.noNull
import dev.utils.DevFinal
import dev.utils.common.DateUtils

class GncFuBangBean : BaseObservable() {
    // 有效性
    @Bindable
    var activited: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.activited)
        }

    // 分配状态 0：未分配，1：已分配
    @Bindable
    var allocationStatus: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.allocationStatus)
        }

    @Bindable
    var boardType: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.boardType)
        }

    @Bindable
    var carId: String = "" // 166
        set(value) {
            field = value
            notifyPropertyChanged(BR.carId)
        }

    @Bindable
    var carWeight: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.carWeight)
            computeWeight()
        }

    @Bindable
    var cargoType: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.cargoType)
        }

    @Bindable
    var cargoWeight: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.cargoWeight)
        }

    @Bindable
    var ccargoWeight: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.ccargoWeight)
        }

//    复磅总重
    var ctotalWeight: String = ""

    @Bindable
    var carrier: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.carrier)
        }

    @Bindable
    var checkFlag: String = "" //
        set(value) {
            field = value
            notifyPropertyChanged(BR.checkFlag)
        }

    @Bindable
    var mftFlag: String = "" //
        set(value) {
            field = value
            notifyPropertyChanged(BR.mftFlag)
        }

    val checkFlagName: String
        get() {
            return when (checkFlag) {
                "0" -> "未复磅"
                "1" -> "已复磅"
                "2" -> "二次复磅"
                else -> ""
            }
        }
    @Bindable
    var mftFno: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.mftFno)
        }

    @Bindable
    var mftFdate: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.mftFdate)
        }

    @Bindable
    var mftFid: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.mftFid)
        }

    val compareMftFlight: Boolean
        get () {
            return fno == mftFno && fdate == mftFdate
        }

    val mftFlight: String
        get() {
            if (mftFdate != "" && mftFno != "" && activited == "1") {
                val (year, mon, day) = mftFdate.split("-")
                return "${year}${mon}${day}/${mftFno}"
            }
            return ""
        }

    @Bindable
    var copDate: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.copDate)
        }

    @Bindable
    var copId: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.copId)
        }

    @Bindable
    var driver: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.driver)
        }

    @Bindable
    var estimatedTakeOff: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.estimatedTakeOff)
        }


    @Bindable
    var scheduledTackOff: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.scheduledTackOff)
        }

    val scheduledTackOffHM: String
        get() {
            return DateUtils.parseString(
                scheduledTackOff,
                DevFinal.TIME.yyyyMMddHHmmss_HYPHEN,
                "HH:mm"
            ).noNull(scheduledTackOff)
        }

    @Bindable
    var fclose: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.fclose)
        }

    @Bindable
    var fdate: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.fdate)
        }

//    货物目的站
    var dest: String = ""

    @Bindable
    var fdep: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.fdep)
        }

    @Bindable
    var fdest: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.fdest)
        }

    @Bindable
    var flight: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.flight)
        }

    @Bindable
    var fno: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.fno)
        }

    val scheduleFlight: String
        get() {
            val (year, mon, day) = fdate.split("-")
            return "${year}${mon}${day}/${fno}"
        }


    @Bindable
    var ldRemark: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.ldRemark)
        }

    @Bindable
    var location: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.location)
        }

    @Bindable
    var mailWeight: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.mailWeight)
        }

    @Bindable
    var maxWeight: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.maxWeight)
        }

    @Bindable
    var netWeight: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.netWeight)
        }

    @Bindable
    var opDate: String = "" // 2024-06-13 10:51:43
        set(value) {
            field = value
            notifyPropertyChanged(BR.opDate)
        }

    @Bindable
    var opId: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.opId)
        }

    @Bindable
    var pc: String = "" // 6000
        set(value) {
            field = value
            notifyPropertyChanged(BR.pc)
        }

    @Bindable
    var priority: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.priority)
        }

    @Bindable
    var remark: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.remark)
        }

    @Bindable
    var standId: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.standId)
        }

    @Bindable
    var storageArea: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.storageArea)
        }

    @Bindable
    var totalWeight: String = "" // 1770
        set(value) {
            field = value
            notifyPropertyChanged(BR.totalWeight)
        }

    @Bindable
    var transDate: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.transDate)
        }

    @Bindable
    var transId: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.transId)
        }

    @Bindable
    var transStatus: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.transStatus)
        }

    @Bindable
    var transType: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.transType)
        }

    @Bindable
    var uld: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.uld)
        }

    @Bindable
    var uldWeight: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.uldWeight)
        }

    @Bindable
    var useId: String = "" // 764653
        set(value) {
            field = value
            notifyPropertyChanged(BR.useId)
        }

    @Bindable
    var volume: String = "" // 0.36
        set(value) {
            field = value
            notifyPropertyChanged(BR.volume)
        }

    /**
     * 偏差
     */
    @Bindable
    var deviation: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.deviation)
        }
        get() {
            if (field.isEmpty()) {
                computeWeight()
            }
            return field
        }

    /**
     * 当前净重
     */
    @Bindable
    var currentWeight: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.currentWeight)
        }
        get() {
            if (field.isEmpty()) {
                computeWeight()
            }
            return field
        }

    /**
     * 地磅重量
     */
    @Bindable
    var weighbridgeWeight: String = "0"
        set(value) {
            field = value
            notifyPropertyChanged(BR.weighbridgeWeight)
            computeWeight()
        }

    var username: String = ""

    /**
     * 计算重量
     */
    fun computeWeight() {
        // 地磅称重
        val ww = weighbridgeWeight.toFloatOrNull() ?: 0f
        // 平板车自重
        val carW = carWeight.toFloatOrNull() ?: 0f
        // 净重
        val netW = netWeight.toFloatOrNull() ?: 0f

        if (netW == 0f) {
            currentWeight = ""
            deviation = ""
            return
        }

        currentWeight = (ww - carW).toString()

        deviation = (((ww - carW) / netW - 1) * 100f).limit(1) + "%"

        ctotalWeight = weighbridgeWeight
    }
}