package com.lukouguoji.module_base.http.user

import android.util.Xml
import com.alibaba.fastjson.JSONObject
import com.lukouguoji.module_base.http.user.`interface`.CommonService
import com.lukouguoji.module_base.http.user.`interface`.WeightDataService
import okhttp3.MultipartBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import javax.xml.XMLConstants
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

object UserNetwork {

    var commonService = ServiceCreator.create<CommonService>()
    private val weightdataService = WeightDataServiceCreator.create<WeightDataService>()
    private suspend fun <T> Call<T>.await(): T {
        return suspendCoroutine { continuation ->
            enqueue(object : Callback<T> {
                override fun onResponse(call: Call<T>, response: Response<T>) {
                    val body = response.body()
                    if (body != null) continuation.resume(body)
                    else {
                        continuation.resumeWithException(RuntimeException("response body is null"))
                    }
                }

                override fun onFailure(call: Call<T>, t: Throwable) {
                    continuation.resumeWithException(t)
                }
            })
        }
    }

    suspend fun login(username: String, password: String, roleTerm: String, userRole: String) =
        commonService.login(username, password, roleTerm, userRole).await()

    suspend fun updatePwd(param: JSONObject) = commonService.updatePwd(param).await()

    suspend fun search(param: JSONObject) = commonService.search(param).await()

    suspend fun queryWbById(wbId: Int) = commonService.queryWbById(wbId).await()

    suspend fun queryWbDetailById(wbId: Int) = commonService.queryWbById(wbId).await()

    suspend fun queryWbByNo(wbNo: String) = commonService.queryWbByNo(wbNo).await()

    suspend fun agentCodeGnc() = commonService.agentCodeGnc().await()

    suspend fun agentCodeGnj() = commonService.agentCodeGnj().await()

    suspend fun startGnj() = commonService.startGnj().await()

    suspend fun business(type: String) = commonService.business(type).await()

    suspend fun awb(type: String) = commonService.awb(type).await()

    suspend fun specialCode() = commonService.specialCode().await()

    suspend fun danger() = commonService.danger().await()

    suspend fun queryActiveCar(id: String) = commonService.queryActiveCar(id).await()

    suspend fun completed(data: JSONObject) = commonService.completed(data).await()

    suspend fun updateFlatcarGncStartReceive(data: JSONObject) =
        commonService.updateFlatcarGncStartReceive(data).await()

    suspend fun divided(data: JSONObject) = commonService.divided(data).await()

    suspend fun searchFuBang(data: JSONObject) = commonService.searchFuBang(data).await()
    suspend fun gncChuku(data: JSONObject) = commonService.gncChuKu(data).await()
    suspend fun gncload(data: JSONObject) = commonService.gncLoad(data).await()

    suspend fun queryWeightingByNo(carNo: String) = commonService.queryWeightingByNo(carNo).await()

    suspend fun queryWeightingById(useId: String) = commonService.queryWeightingById(useId).await()

    suspend fun cargoSelect() = commonService.cargoSelect().await()

    suspend fun cargoSelectGjc() = commonService.cargoSelectGjc().await()

    suspend fun completedFuBang(data: JSONObject) = commonService.completedFuBang(data).await()

    suspend fun searchGncTransfer(data: JSONObject) = commonService.searchGncTransfer(data).await()

    suspend fun queryFlatById(useId: Int) = commonService.queryFlatById(useId).await()

    suspend fun queryFlatByNo(carId: String) = commonService.queryFlatByNo(carId).await()

    suspend fun completedTransfer(data: JSONObject) = commonService.completedTransfer(data).await()

    suspend fun searchGncWareHouse(data: JSONObject) =
        commonService.searchGncWareHouse(data).await()

    suspend fun queryInfoByIdGncWareHouse(id: Int) =
        commonService.queryInfoByIdGncWareHouse(id).await()

    suspend fun totalGncWareHouse(data: JSONObject) = commonService.totalGncWareHouse(data).await()

    suspend fun searchGncQueryList(data: JSONObject) =
        commonService.searchGncQueryList(data).await()

    suspend fun searchByIdGncQuery(id: Int) = commonService.searchByIdGncQuery(id).await()

    suspend fun searchByWbNoGncQuery(wbNo: String) =
        commonService.searchByWbNoGncQuery(wbNo).await()

    suspend fun searchGnjCangDanList(data: JSONObject) =
        commonService.searchGnjCangDanList(data).await()

    suspend fun queryFestByIdGnjCangDan(id: Int) = commonService.queryFestByIdGnjCangDan(id).await()

    suspend fun queryFestByWbNoGnjCangDan(wbNo: String) =
        commonService.queryFestByWbNoGnjCangDan(wbNo).await()

    suspend fun domFlightGnjCangDan(data: JSONObject) =
        commonService.domFlightGnjCangDan(data).await()

    suspend fun saveFestGnjCangDan(data: JSONObject) =
        commonService.saveFestGnjCangDan(data).await()

    suspend fun updateFestGnjCangDan(data: JSONObject) =
        commonService.updateFestGnjCangDan(data).await()

    suspend fun deleteFestGnjCangDan(id: Int) = commonService.deleteFestGnjCangDan(id).await()

    suspend fun provideGnjCangDan(data: JSONObject) = commonService.provideGnjCangDan(data).await()

    suspend fun searchGnjChuKu(data: JSONObject) = commonService.searchGnjChuKu(data).await()

    suspend fun queryWbNoGnjChuKu(wbNo: String) = commonService.queryWbNoGnjChuKu(wbNo).await()

    suspend fun completedGnjChuKu(data: JSONObject) = commonService.completedGnjChuKu(data).await()

    suspend fun queryPickByIdGnjChuKu(pkId: String) =
        commonService.queryPickByIdGnjChuKu(pkId).await()

    suspend fun searchGnjWareHouse(data: JSONObject) =
        commonService.searchGnjWareHouse(data).await()

    suspend fun queryInfoByIdGnjWareHouse(id: String) =
        commonService.queryInfoByIdGnjWareHouse(id).await()

    suspend fun totalGnjWareHouse(data: JSONObject) = commonService.totalGnjWareHouse(data).await()

    suspend fun searchGnjQuery(data: JSONObject) = commonService.searchGnjQuery(data).await()

    suspend fun searchByIdGnjQuery(id: Int) = commonService.searchByIdGnjQuery(id).await()

    suspend fun searchByWbNoGnjQuery(wbNo: String) =
        commonService.searchByWbNoGnjQuery(wbNo).await()

    suspend fun awbTypeByWbNoGnjQuery() = commonService.awbTypeByWbNoGnjQuery().await()

    suspend fun searchGnjRuKu(data: JSONObject) = commonService.searchGnjRuKu(data).await()

    suspend fun loadCarGnjRuKu(data: JSONObject) = commonService.loadCarGnjRuKu(data).await()

    suspend fun unloadCarGnjRuKu(data: JSONObject) = commonService.unloadCarGnjRuKu(data).await()

    suspend fun searchGnjYiKu(data: JSONObject) = commonService.searchGnjYiKu(data).await()

    suspend fun searchMovedGnjYiKu(data: JSONObject) =
        commonService.searchMovedGnjYiKu(data).await()

    suspend fun moveGnjYiKu(data: JSONObject) = commonService.moveGnjYiKu(data).await()

    suspend fun awbTypeGnjYiKu() = commonService.awbTypeGnjYiKu().await()

    suspend fun flightType() = commonService.flightType().await()

    suspend fun countryType() = commonService.countryType().await()

    suspend fun searchHangBan(data: JSONObject) = commonService.searchHangBan(data).await()

    suspend fun queryFlightByIdHangBan(id: Int) = commonService.queryFlightByIdHangBan(id).await()

    suspend fun searchGjcReceive(data: JSONObject) = commonService.searchGjcReceive(data).await()

    suspend fun queryWbByNoGjcReceive(wbNo: String) =
        commonService.queryWbByNoGjcReceive(wbNo).await()

    suspend fun queryWbByIdGjcReceive(id: Int) = commonService.queryWbByIdGjcReceive(id).await()

    suspend fun completedGjcReceive(data: JSONObject) =
        commonService.completedGjcReceive(data).await()

    suspend fun queryUldGjcReceive(id: String) = commonService.queryUldGjcReceive(id).await()

    suspend fun updateUldGjcReceive(data: JSONObject) =
        commonService.updateUldGjcReceive(data).await()

    suspend fun queryFlatcarGjcReceive(id: String) =
        commonService.queryFlatcarGjcReceive(id).await()

    suspend fun updateFlatcarGjcReceive(data: JSONObject) =
        commonService.updateFlatcarGjcReceive(data).await()

    suspend fun completedGjcFuBang(data: JSONObject) =
        commonService.completedGjcFuBang(data).await()

    suspend fun queryInfoByIdGjcWareHouse(id: Int) =
        commonService.queryInfoByIdGjcWareHouse(id).await()

    suspend fun searchGjcWareHouse(data: JSONObject) =
        commonService.searchGjcWareHouse(data).await()

    suspend fun totalGjcWareHouse(data: JSONObject) = commonService.totalGjcWareHouse(data).await()

    suspend fun agentCodeGjc() = commonService.agentCodeGjc().await()

    suspend fun searchGjcQuery(data: JSONObject) = commonService.searchGjcQuery(data).await()

    suspend fun totalGjcQuery(data: JSONObject) = commonService.totalGjcQuery(data).await()

    suspend fun searchByIdGjcQuery(id: Int) = commonService.searchByIdGjcQuery(id).await()

    suspend fun searchByWbNoGjcQuery(id: Int) = commonService.searchByWbNoGjcQuery(id).await()

    suspend fun searchGjcYiKu(data: JSONObject) = commonService.searchGjcYiKu(data).await()

    suspend fun searchMovedGjcYiKu(data: JSONObject) =
        commonService.searchMovedGjcYiKu(data).await()

    suspend fun moveGjcYiKu(data: JSONObject) = commonService.moveGjcYiKu(data).await()

    suspend fun searchGjjCangDan(data: JSONObject) = commonService.searchGjjCangDan(data).await()

    suspend fun intFlightGjjCangDan(data: JSONObject) =
        commonService.intFlightGjjCangDan(data).await()

    suspend fun queryFestByIdGjjCangDan(id: Int) = commonService.queryFestByIdGjjCangDan(id).await()

    suspend fun queryFestByWbNoGjjCangDan(wbNo: String) =
        commonService.queryFestByWbNoGjjCangDan(wbNo).await()

    suspend fun provideGjjCangDan(data: JSONObject) = commonService.provideGjjCangDan(data).await()

    suspend fun deleteFestGjjCangDan(id: Int) = commonService.deleteFestGjjCangDan(id).await()

    suspend fun saveFestGjjCangDan(data: JSONObject) =
        commonService.saveFestGjjCangDan(data).await()

    suspend fun updateFestGjjCangDan(data: JSONObject) =
        commonService.updateFestGjjCangDan(data).await()

    suspend fun searchGjjCangKu(data: JSONObject) = commonService.searchGjjCangKu(data).await()

    suspend fun totalGjjCangKu(data: JSONObject) = commonService.totalGjjCangKu(data).await()

    suspend fun agentCodeGjj() = commonService.agentCodeGjj().await()

    suspend fun queryInfoByIdGjjCangKu(id: Int) = commonService.queryInfoByIdGjjCangKu(id).await()

    suspend fun searchGjjChuKu(data: JSONObject) = commonService.searchGjjChuKu(data).await()

    suspend fun queryWbNoGjjChuKu(wbNo: String) = commonService.queryWbNoGjjChuKu(wbNo).await()

    suspend fun queryPickByIdGjjChuKu(pkId: String) =
        commonService.queryPickByIdGjjChuKu(pkId).await()

    suspend fun completedGjjChuKu(data: JSONObject) = commonService.completedGjjChuKu(data).await()

    suspend fun searchGjjQuery(data: JSONObject) = commonService.searchGjjQuery(data).await()

    suspend fun searchByIdGjjQuery(id: Int) = commonService.searchByIdGjjQuery(id).await()

    suspend fun searchByWbNoGjjQuery(wbNo: String) =
        commonService.searchByWbNoGjjQuery(wbNo).await()

    suspend fun searchMitInPack(data: JSONObject) = commonService.searchMitInPack(data).await()

    suspend fun deleteMitInPack(data: JSONObject) = commonService.deleteMitInPack(data).await()

    suspend fun completedMitInPack(data: JSONObject) =
        commonService.completedMitInPack(data).await()

    suspend fun queryFlightMitInPack(data: JSONObject) =
        commonService.queryFlightMitInPack(data).await()

    suspend fun deleteVisa(id: Int) = commonService.deleteVisa(id).await()

    suspend fun queryVisaById(id: Int) = commonService.queryVisaById(id).await()

    suspend fun saveVisa(data: JSONObject) = commonService.saveVisa(data).await()

    suspend fun searchVisa(data: JSONObject) = commonService.searchVisa(data).await()

    suspend fun updateVisa(data: JSONObject) = commonService.updateVisa(data).await()

    suspend fun outerPackageType() = commonService.outerPackageType().await()

    suspend fun damageType() = commonService.damageType().await()

    suspend fun contentType() = commonService.contentType().await()

    suspend fun unusualType() = commonService.unusualType().await()

    suspend fun searchDateType() = commonService.searchDateType().await()

    suspend fun uploadImg(file: MultipartBody.Part) = commonService.uploadImg(file).await()

    suspend fun uploadImgList(files: List<MultipartBody.Part>) =
        commonService.uploadImgList(files).await()

    suspend fun deleteImg(name: String) = commonService.deleteImg(name).await()

    suspend fun searchOutLoading(data: JSONObject) = commonService.searchOutLoading(data).await()

    suspend fun queryWbOutLoading(data: JSONObject) = commonService.queryWbOutLoading(data).await()

    suspend fun pullOutLoading(data: JSONObject) = commonService.pullOutLoading(data).await()

    suspend fun searchCargoStatus(data: JSONObject) = commonService.searchCargoStatus(data).await()
    fun getWeightData(WeightBridgeName: String): Call<String> =
        weightdataService.getWeightData(WeightBridgeName)

}