package com.lukouguoji.module_base.bean

import android.icu.util.Calendar
import androidx.databinding.ObservableBoolean
import com.lukouguoji.module_base.interfaces.ICheck
import com.lukouguoji.module_base.ktx.noNull
import dev.utils.DevFinal
import dev.utils.common.DateUtils

class FlightBean : ICheck {

    // 实际到达时间
    var actualArrival: String = ""

    // 实际起飞时间
    var actualTakeOff: String = ""


    // 实际起飞时间-时分
    val actualTakeOffHM: String
        get() {
            return DateUtils.parseString(
                actualTakeOff,
                DevFinal.TIME.yyyyMMddHHmmss_HYPHEN,
                "HH:mm"
            ).noNull(actualTakeOff)
        }


    // 机型
    var aircraftCode: String = ""

    // 地区类型名称
    var countryName: String = ""

    // 航班类型- 0:国内 1:国际
    var countryType: String = ""

    // 延误原因
    var delayFreeText: String = ""

    // 司机
    var driver: String = ""

    // 预计到达时间 HH:mm 格式
    var estimatedArrival: String = ""

    // 预计起飞时间
    var estimatedTakeOff: String = ""

    // 航班关闭时间
    var fclose: String = ""
    var fdate: String = ""

    // 始发港
    var fdep: String = ""

    // 目的港
    var fdest: String = ""

    // 主键
    var fid: String = ""

    var id: String = ""

    // 单证类型：1业务袋、2特货通知单（字典表）
    var fileType: String = ""

    val fileTypeName: String
        get() {
            return when (fileType) {
                "1" -> "业务袋"
                "2" -> "特货通知单"
                "3" -> "业务袋、特货通知单"
                else -> ""
            }
        }

    // 单证类型：1业务袋、2特货通知单（字典表）
    var cmFlag: String = ""

    val cmFlagName: String
        get() {
            return when (cmFlag) {
                "0" -> "有货邮"
                "1" -> "无货邮"
                else -> ""
            }
        }

    // 航班状态(0:正常;1:登机;2:登机结束;3:起飞;4:到达;5:取消;6:备降;7:删除发布;8:延误;9:未知)
    var flightStatus: String = ""

    val flightStatusName: String
        get() {
            return when (flightStatus) {
                "0" -> "正常"
                "1" -> "登机"
                "2" -> "登机结束"
                "3" -> "起飞"
                "4" -> "到达"
                "5" -> "取消"
                "6" -> "备降"
                "7" -> "删除发布"
                "8" -> "延误"
                "9" -> "未知"
                else -> ""
            }
        }

    // 航班号
    var fno: String = ""

    // 航班进出港标志 I:进港 E:出港
    var ieFlag: String = ""

    // 经停港（可多个用’’分隔）
    var jtz: String = ""

    // 航班截载时间
    var mclose: String = ""

    // 组织代码
    var organizationCode: String = ""

    // 飞行状态
    var portCode: String = ""

    // 运单前缀
    var prefix: String = ""

    // 航程
    var range: String = ""

    // 注册号
    var registration: String = ""

    // 计划到达时间
    var scheduledArrival: String = ""

    // 计划起飞时间
    var scheduledTackOff: String = ""

    // 计划起飞时间-时分
    val scheduledTackOffHM: String
        get() {
            return DateUtils.parseString(
                scheduledTackOff,
                DevFinal.TIME.yyyyMMddHHmmss_HYPHEN,
                "HH:mm"
            ).noNull(scheduledTackOff)
        }

    // 预计起飞时间-时分
    val estimatedTakeOffHM: String
        get() {
            return DateUtils.parseString(
                estimatedTakeOff,
                DevFinal.TIME.yyyyMMddHHmmss_HYPHEN,
                "HH:mm"
            ).noNull(estimatedTakeOff)
        }

    // 计划降落时间-时分
    val scheduledArrivalHM: String
        get() {
            return DateUtils.parseString(
                scheduledArrival,
                DevFinal.TIME.yyyyMMddHHmmss_HYPHEN,
                "HH:mm"
            ).noNull(scheduledArrival)
        }

    // 预计降落时间-时分
    val estimatedArrivalHM: String
        get() {
            return DateUtils.parseString(
                estimatedArrival,
                DevFinal.TIME.yyyyMMddHHmmss_HYPHEN,
                "HH:mm"
            ).noNull(estimatedArrival)
        }

    // 实际降落时间-时分
    val actualArrivalHM: String
        get() {
            return DateUtils.parseString(
                actualArrival,
                DevFinal.TIME.yyyyMMddHHmmss_HYPHEN,
                "HH:mm"
            ).noNull(actualArrival)
        }

    // 航班服务种类(0:客机;1:货机;2:卡车)
    var serviceType: String = ""

    // 机位
    var standId: String = ""

    // 进出港状态
    var status: String = ""

    // 预计到达时间
    var arrivalDate: String = ""

    // 航空公司
    var nameCN: String = ""

    // 交接人
    var handoverPerson: String = ""

    // 交接时间
    var handoverTime: String = ""

    //货物类型：0“国际货”，1“活体鲜活”，2“贵重物品”，3“其他特货”
    var cargoType: String = ""

    val checked = ObservableBoolean(false)

    override fun getCheckObservable() = checked

    fun beArrival(): Boolean {
        return "1" == status
    }

    fun getHangCheng(): String {
        if (fdep == null || fdep == "") {
            return ""
        }
        return "${fdep ?: ""}-${fdest ?: ""}"
    }

    fun getCountryTypeName(): String {
        return when (countryType) {
            "0" -> "国内"
            "1" -> "国际"
            else -> ""
        }
    }

    fun getTimeDesc(): String {
        val datetime = if (beArrival()) scheduledArrival else scheduledTackOff
        if (datetime.isEmpty()) {
            return ""
        }
        return DateUtils.formatDate(DateUtils.parseDate(datetime), "HH:mm")
    }

    fun showAddOne(): Boolean {
        val datetime = if (beArrival()) scheduledArrival else scheduledTackOff
        if (datetime.isEmpty() || fdate.isEmpty()) {
            return false
        }
        val calendar = DateUtils.getCalendar(DateUtils.parseDate(datetime))
        val current = DateUtils.getCalendar(DateUtils.parseDate(fdate, "yyyy-MM-dd"))
        if (calendar.get(Calendar.YEAR) > current.get(Calendar.YEAR)) {
            return true
        }
        return calendar.get(Calendar.DAY_OF_YEAR) > current.get(Calendar.DAY_OF_YEAR)
    }

    // 预计起飞 vs 计划起飞 是否跨日
    fun isEstimatedTakeOffNextDay(): Boolean {
        if (scheduledTackOff.isEmpty() || estimatedTakeOff.isEmpty()) return false
        return try {
            val calBase = DateUtils.getCalendar(DateUtils.parseDate(scheduledTackOff))
            val calTarget = DateUtils.getCalendar(DateUtils.parseDate(estimatedTakeOff))
            if (calTarget.get(Calendar.YEAR) > calBase.get(Calendar.YEAR)) true
            else calTarget.get(Calendar.DAY_OF_YEAR) > calBase.get(Calendar.DAY_OF_YEAR)
        } catch (e: Exception) { false }
    }

    // 实际起飞 vs 计划起飞 是否跨日
    fun isActualTakeOffNextDay(): Boolean {
        if (scheduledTackOff.isEmpty() || actualTakeOff.isEmpty()) return false
        return try {
            val calBase = DateUtils.getCalendar(DateUtils.parseDate(scheduledTackOff))
            val calTarget = DateUtils.getCalendar(DateUtils.parseDate(actualTakeOff))
            if (calTarget.get(Calendar.YEAR) > calBase.get(Calendar.YEAR)) true
            else calTarget.get(Calendar.DAY_OF_YEAR) > calBase.get(Calendar.DAY_OF_YEAR)
        } catch (e: Exception) { false }
    }

    // 预计降落 vs 计划降落 是否跨日
    fun isEstimatedArrivalNextDay(): Boolean {
        if (scheduledArrival.isEmpty() || estimatedArrival.isEmpty()) return false
        return try {
            val calBase = DateUtils.getCalendar(DateUtils.parseDate(scheduledArrival))
            val calTarget = DateUtils.getCalendar(DateUtils.parseDate(estimatedArrival))
            if (calTarget.get(Calendar.YEAR) > calBase.get(Calendar.YEAR)) true
            else calTarget.get(Calendar.DAY_OF_YEAR) > calBase.get(Calendar.DAY_OF_YEAR)
        } catch (e: Exception) { false }
    }

    // 实际降落 vs 计划降落 是否跨日
    fun isActualArrivalNextDay(): Boolean {
        if (scheduledArrival.isEmpty() || actualArrival.isEmpty()) return false
        return try {
            val calBase = DateUtils.getCalendar(DateUtils.parseDate(scheduledArrival))
            val calTarget = DateUtils.getCalendar(DateUtils.parseDate(actualArrival))
            if (calTarget.get(Calendar.YEAR) > calBase.get(Calendar.YEAR)) true
            else calTarget.get(Calendar.DAY_OF_YEAR) > calBase.get(Calendar.DAY_OF_YEAR)
        } catch (e: Exception) { false }
    }

    fun getCargoTypeName(): String {
        return when (cargoType) {
            "0" -> "国际货"
            "1" -> "活体鲜活"
            "2" -> "贵重物品"
            "3" -> "其他特货"
            else -> ""
        }
    }

}