package com.lukouguoji.module_base.bean

import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import androidx.databinding.ObservableBoolean
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 GncShouYunBean : BaseObservable() {
    // 有效性
    @Bindable
    var activeId: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.activeId)
        }

    // 有效性
    @Bindable
    var whId: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.whId)
        }

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

    // 运单类型
    @Bindable
    var awbType: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.awbType)
        }

    // 账单号
    @Bindable
    var billsNo: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.billsNo)
        }

    // 业务类型
    @Bindable
    var businessType: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.businessType)
        }

    // 承运人
    @Bindable
    var by0: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.by0)
        }

    // 第一承运人
    @Bindable
    var by1: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.by1)
        }

    // 第二承运人
    @Bindable
    var by2: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.by2)
        }

    // 板车编号
    @Bindable
    var carNo: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.carNo)
        }

    // 货物类型
    @Bindable
    var cargoType: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.cargoType)
        }

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

    // 入库标识 0：未入库，1：入库结束
    @Bindable
    var checkIn: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.checkIn)
        }

    // 收货人代码
    @Bindable
    var cneeCode: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.cneeCode)
        }

    // 收货人身份证号
    @Bindable
    var cneeId: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.cneeId)
        }

    // 收货人电话
    @Bindable
    var cneeTel: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.cneeTel)
        }

    // 收货人
    @Bindable
    var consignee: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.consignee)
        }

    // 出发站
    @Bindable
    var dep: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.dep)
        }

    // 目的港
    @Bindable
    var dest: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.dest)
        }

    // 第一目的港
    @Bindable
    var dest1: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.dest1)
        }

    // 第2目的港
    @Bindable
    var dest2: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.dest2)
        }

    // 危险品库
    @Bindable
    var dgrLocation: String = "" // null
        set(value) {
            field = value
            notifyPropertyChanged(BR.dgrLocation)
        }

    // 危险品描述
    @Bindable
    var dgrDetail: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.dgrDetail)
        }

    // 预计起飞时间
    @Bindable
    var scheduledTackOff: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.scheduledTackOff)
        }

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


    /**
     * 校验起飞时间是否明天
     */
    fun verifyTakeOffNextDay(): Boolean {
        if (fdate.isEmpty() || scheduledTackOff.isEmpty()) {
            return false
        }
        val calendarFDate = DateUtils.getCalendar(fdate, DevFinal.TIME.yyyyMMdd_HYPHEN)
        val calendarTakeOff =
            DateUtils.getCalendar(scheduledTackOff, DevFinal.TIME.yyyyMMddHHmmss_HYPHEN)
        return DateUtils.getDay(calendarTakeOff) > DateUtils.getDay(calendarFDate)
    }

    // 截载时间
    @Bindable
    var fclose: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.fclose)
        }

    // 航班日期
    @Bindable
    var fdate: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.fdate)
        }

    // 航班信息
    @Bindable
    var flight: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.flight)
        }

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

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

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

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

    // 运单号
    @Bindable
    var no: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.no)
        }

    // 原运单号
    @Bindable
    var oldNo: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.oldNo)
        }

    // 原运单号前缀
    @Bindable
    var oldPrefix: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.oldPrefix)
        }

    // 操作日期
    @Bindable
    var opDate: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.opDate)
        }

    // 操作人代码
    @Bindable
    var opId: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.opId)
        }

    // 始发站
    @Bindable
    var origin: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.origin)
        }

    // 包装类型
    @Bindable
    var packageType: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.packageType)
        }

    // 接单时间
    @Bindable
    var paperTime: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.paperTime)
        }

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

    // 预配件数
    var apc: String = ""

    // 运单前缀
    @Bindable
    var prefix: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.prefix)
        }

    // 完整航程: NJ-PK-SHY or NJ-PK
    @Bindable
    var range: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.range)
        }

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

    // 预计到达时间
    @Bindable
    var scheduledArrival: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.scheduledArrival)
        }

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

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

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

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

    // UN编号
    @Bindable
    var unNumber: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.unNumber)
        }

    // 体积明细
    @Bindable
    var volDetail: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.volDetail)
        }

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

    // 运单号
    @Bindable
    var wbNo: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.wbNo)
        }
        get() {
            if (field.isEmpty()) {
                field = "$prefix$no"
            }
            return field
        }

    // 重量
    @Bindable
    var weight: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.weight)
            volume = ((field.toFloatOrNull() ?: 0f) / 220f).limit(2)
        }

    // 平板车重量
    @Bindable
    var carWeight: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.carWeight)
        }

    var location: String = ""

    var username: String = ""

    val checked = ObservableBoolean(false)

    var aircraftCode: String  = ""

    fun getRangeFirst(): String {
        return range.split("-").firstOrNull() ?: ""
    }

    fun getRangeSecond(): String {
        return range.split("-").getOrNull(1) ?: ""
    }

}