package com.lukouguoji.module_base.util

import com.lukouguoji.module_base.bean.DictBean
import com.lukouguoji.module_base.bean.DictListBean
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.ktx.launchCollect
import dev.utils.app.info.KeyValue
import java.util.Collections.emptyList

object DictUtils {

    /**
     * 获取复磅状态列表
     */
    val reWeightStatusList = listOf(
        KeyValue("全部", ""),
        KeyValue("未复磅", "0"),
        KeyValue("已复磅", "1"),
        KeyValue("二次复磅", "2"),
    )

    /**
     * 舱单标识 查询条件增加“全部“、”上舱单“、”未上舱单“查询功能，查询条件默认”全部“
     */
    val mftFlagList = listOf(
        KeyValue("全部", ""),
        KeyValue("上舱单", "0"),
        KeyValue("未上舱单", "1"),
    )

    /**
     * 截载状态
     */
    val cutStatusList = listOf(
        KeyValue("全部", ""),
        KeyValue("未截载", "0"),
        KeyValue("已截载", "1"),
    )

    /**
     * 分配状态
     */
    val distributionStatusList = listOf(
        KeyValue("全部", ""),
        KeyValue("未分配", "0"),
        KeyValue("已分配", "1"),
    )

    /**
     * 交接状态
     */
    val handoverStatusList = listOf(
        KeyValue("全部", ""),
        KeyValue("未交接", "0"),
        KeyValue("已交接", "1"),
    )

    /**
     * 单证类型
     */
    val docTypeList = listOf(
        KeyValue("全部", ""),
        KeyValue("业务袋", "1"),
        KeyValue("特货通知单", "2"),
    )

    /**
     * 货物类型：0“国际货”，1“活体鲜活”，2“贵重物品”，3“其他特货”
     */
    val airCargoTypeList = listOf(
        KeyValue("国际货", "0"),
        KeyValue("活体鲜活", "1"),
        KeyValue("贵重物品", "2"),
        KeyValue("其他特货", "3"),
    )

    /**
     * 转运类型
     */
    val transportTypeList = listOf(
        KeyValue("出港库存至转运区", "0"),
        KeyValue("出港库存至机坪", "1"),
        KeyValue("进港库存至转运区", "2"),
        KeyValue("出港库存至库区", "3"),
    )

    /**
     * 获取所属航司列表（国际）
     */
    fun getIntCarrierList(
        addAll: Boolean = false,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getIntCarrierList()
        }) {
            onSuccess = {
                handleCallBack(it, checkedValue, addAll, callBack)
            }
        }
    }

    /**
     * 获取代理列表
     */
    fun getAgentList(
        addAll: Boolean = true,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getAgentList()
        }) {
            onSuccess = {
                handleCallBack(it, checkedValue, addAll, callBack)
            }
        }
    }

    /**
     * 获取国际进港代理列表
     */
    fun getIntImpAgentList(
        addAll: Boolean = true,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getIntImpAgentList()
        }) {
            onSuccess = {
                handleCallBack(it, checkedValue, addAll, callBack)
            }
        }
    }

    /**
     * 获取仓管列表
     */
    fun getWHSUserList(
        addAll: Boolean = true,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getWHSUserList()
        }) {
            onSuccess = {
                handleCallBack(it, checkedValue, addAll, callBack)
            }
        }
    }

    /**
     * 获取出港代理列表
     */
    fun getGncAgentList(
        addAll: Boolean = true,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getGncAgentList()
        }) {
            onSuccess = {
                handleCallBack(it, checkedValue, addAll, callBack)
            }
        }
    }

    private fun handleCallBack(
        it: DictListBean,
        checkedValue: String?,
        addAll: Boolean,
        callBack: (List<KeyValue>) -> Unit
    ) {
        val list = it.data?.map { b -> b.toKeyValue() } ?: emptyList()
        val arrayList = arrayListOf<KeyValue>()

        // 选中的数据放在第一位
        list.find { b -> b.value == checkedValue }?.let {
            arrayList.add(it)
        }
        // 添加全部
        if (addAll) {
            arrayList.add(KeyValue("全部", ""))
        }
        // 添加剩下的数据
        arrayList.addAll(list.filter { b -> b.value != checkedValue })

        callBack(arrayList)
    }

    /**
     * 特码列表
     */
    fun getSpecialCodeList(
        addAll: Boolean = true,
        parentcode: String = "",
        // 0：国内，1：国际
        flag: Int = 0,
        // I-进港, E-出港
        ieFlag: String = "E",
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getSpecialCodeList(flag, ieFlag, parentcode)
        }) {
            onSuccess = {
                handleCallBack(it, checkedValue, addAll, callBack)
            }
        }
    }


    /**
     * 业务类型列表
     * @param type 获取业务类型---CI:国内进港,CO:国内出港,II:国际进港,IO:国际出港 下拉框
     */
    fun getBusinessTypeList(
        type: String = "II",
        addAll: Boolean = true,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .business(type)
        }) {
            onSuccess = {
                handleCallBack(it, checkedValue, addAll, callBack)
            }
        }
    }

    /**
     * 包装类型---收运
     */
    fun getShouYunPackageTypeList(
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getShouYunPackTypeList()
        }) {
            onSuccess = {
                val list = it.data?.map { b -> b.toKeyValue() } ?: emptyList()
                val arrayList = arrayListOf<KeyValue>()
                // 选中的数据放在第一位
                list.find { b -> b.value == checkedValue }?.let {
                    arrayList.add(it)
                }
                // 添加剩下的数据
                arrayList.addAll(list.filter { b -> b.value != checkedValue })

                callBack(arrayList)
            }
        }
    }

    /**
     * 包装类型
     */
    fun getPackageTypeList(
        addAll: Boolean = true,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getPackTypeList()
        }) {
            onSuccess = {
                handleCallBack(it, checkedValue, addAll, callBack)
            }
        }
    }

    /**
     * 包装类型-国际进港
     */
    fun getGjjPackageTypeList(
        addAll: Boolean = true,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getGjjPackTypeList()
        }) {
            onSuccess = {
                handleCallBack(
                    DictListBean(data = it.data?.map { b ->
                        DictBean(
                            b.customsCode,
                            b.packageName
                        )
                    }), checkedValue, addAll, callBack
                )
            }
        }
    }

    /**
     * 理货状态
     */
    val tallyStatusList = listOf(
        KeyValue("未理货", "0"),
        KeyValue("已理货", "1"),
        KeyValue("全部", ""),
    )

    /**
     * 报文类型
     */
    fun getPacketTypeList(addAll: Boolean = true, callBack: (List<KeyValue>) -> Unit) {
        val list = arrayListOf<KeyValue>()
        if (addAll) {
            list.add(KeyValue("全部", "ALL"))
        }
        // 舱单报
        list.add(KeyValue("FFM", "FFM"))
        // 主单报
        list.add(KeyValue("FWB", "FWB"))
        // 分单报
        list.add(KeyValue("FHL", "FHL"))
        callBack(list)
    }

    /**
     * 货物类型-国际进港
     */
    fun getGjjGoodsTypeList(
        addAll: Boolean = true,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getGjjCargoTypeList()
        }) {
            onSuccess = {
                handleCallBack(DictListBean(data = it.data?.map { b ->
                    DictBean(
                        b.cargoType,
                        b.cargoName
                    )
                }), checkedValue, addAll, callBack)
            }
        }
    }

    /**
     * 货物类型-国内
     */
    fun getGnGoodsTypeList(
        addAll: Boolean = true,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getGoodsTypeList()
        }) {
            onSuccess = {
                handleCallBack(it, checkedValue, addAll, callBack)
            }
        }
    }


    /**
     * 运单类型
     * @param type CI:国内进港,Co:国内出港,II:国际进港,10:国际出港
     */
    fun getWaybillTypeList(
        type: String = "II",
        addAll: Boolean = true,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getWaybillTypeList(type)
        }) {
            onSuccess = {
                handleCallBack(it, checkedValue, addAll, callBack)
            }
        }
    }


    /**
     * 运单类型
     * @param ieFlag I进港、E出港
     * @param type CI:国内进港,Co:国内出港,II:国际进港,10:国际出港
     */
    fun getWaybillTypeList2(
        ieFlag: String = "I",
        type: String = "II",
        addAll: Boolean = true,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getWaybillTypeList2(ieFlag, type)
        }) {
            onSuccess = {
                handleCallBack(it, checkedValue, addAll, callBack)
            }
        }
    }

    /**
     * 获取渠道列表
     */
    fun getChannelList(
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getDictList("GNPASSAGEWAY")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> b.toKeyValue() })
            }
        }
    }

    /**
     * 国际出港通道号列表
     */
    fun getGjcChannelList(
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getDictListByLocation("GJCPASSAGEWAY")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> b.toKeyValue() })
            }
        }
    }


    /**
     * 货物类型
     */
    fun getCargoTypeList(
        addAll: Boolean = true,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getCargoTypeList()
        }) {
            onSuccess = {
                handleCallBack(it, checkedValue, addAll, callBack)
            }
        }
    }

    /**
     * 地区类型
     */
    fun getCountryTypeList(
        addAll: Boolean = true,
        checkedValue: String? = null,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api
                .getCountryTypeList()
        }) {
            onSuccess = {
                handleCallBack(it, checkedValue, addAll, callBack)
            }
        }
    }

    /**
     * 获取角色列表
     */
    fun getRoleList(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getRoleList()
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> b.toKeyValue() })
            }
        }
    }

    // 区域
    fun getAreaList(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getDictList("AREA")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> KeyValue(b.key, b.key) })
            }
        }
    }

    // 转运目的站
    fun getTransDestList(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getDictList("TRANSDEST")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> b.toKeyValue() })
            }
        }
    }

    // 危险品库
    fun getWxKuList(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getDictList("DGRLOCATION")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> b.toKeyValue() })
            }
        }
    }

    // 危险品描述
    fun getWxDescList(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getDictList("DGRDETAIL")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> b.toKeyValue() })
            }
        }
    }

    // 危险品描述  value value
    fun getWxDescListValue(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getDictList("DGRDETAIL")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> KeyValue(b.value, b.value) })
            }
        }
    }


    // 国内进港卸机-货物类型
    fun getDicCargoTypeList(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getDictList("CARGOTYPE")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> b.toKeyValue() })
            }
        }
    }

    // 平板车位置(货斗状态)
    fun getCarLocationList(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getDictList("CARPOSITION")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> b.toKeyValue() })
            }
        }
    }

    // 运输类型
    fun getTransTypeList(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getDictList("TRANSTYPE")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> b.toKeyValue() })
            }
        }
    }

    // 运输类型--根据角色类型
    fun getTransTypeListByRole(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getTransTypeListByRole()
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> b.toKeyValue() })
            }
        }
    }

    // 单证类型
    fun getDocTypeList(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getDictList("DOCTYPE")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> b.toKeyValue() })
            }
        }
    }

    // 报文类型
    fun getTelegramTypeList(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getDictList("SITATTYPE")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> KeyValue(b.key, b.key) })
            }
        }
    }

    // 运输状态
    fun getTransStatusList(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getDictList("TRANSSTATUS")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> KeyValue(b.value, b.key) })
            }
        }
    }

    /**
     * 根据角色获取用户列表
     * 0:计重员、
     * 1:复磅司机、
     * 2:仓管主班、
     * 3:仓管副班、
     * 4:进港理货员
     * 5:进港仓管员
     * 6:接驳司机
     * 7:转运司机
     */
    fun getUserListByRole(
        role: String,
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api.getUserByRoleId(role)
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> b.toKeyValue() })
            }
        }
    }

    fun getAllDriverList(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api.getUserByRoleId("6")
        }) {
            onSuccess = {
                val jbDrivers =
                    (it.data ?: emptyList()).map { b -> KeyValue(b.username, b.username) }
                launchCollect({
                    NetApply.api.getUserByRoleId("7")
                }) {
                    onSuccess = { iit ->
                        val shDrivers =
                            (iit.data ?: emptyList()).map { b -> KeyValue(b.username, b.username) }
                        callBack(jbDrivers + shDrivers)
                    }
                }
            }
        }
    }

    fun getDriverList(
        callBack: (List<KeyValue>) -> Unit
    ) {
        launchCollect({
            NetApply.api.getUserByRoleId("6")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> KeyValue(b.username, b.username) })
            }
        }
    }

    /**
     * 单证交接 单证类型
     */
    fun getDocumentHandoverTypeList(callBack: (List<KeyValue>) -> Unit) {
        launchCollect({
            NetApply.api
                .getDictList("DOCTYPE")
        }) {
            onSuccess = {
                callBack((it.data ?: emptyList()).map { b -> b.toKeyValue() })
            }
        }
    }
}