package com.lukouguoji.mit.viewModel

import android.util.Log
import androidx.lifecycle.*
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.lukouguoji.module_base.common.String.emptyToNull
import com.lukouguoji.module_base.http.user.UserNetwork
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import okhttp3.MultipartBody


class AccidentVisaInfoViewModel : ViewModel() {
    private val logName = "AccidentVisaInfoViewModel"

    var waybillFormNetWork = JSONObject()

    //本地图片名称和服务器名称映射
    val imageKeyValue = hashMapOf<String, String>()

    //服务器返回所有的图片名称
    val imageNamesFromNetWork = arrayListOf<String>()

    /**
     * 进港 舱单详情
     */
    inner class WaybillParam(val wbId: Int, val wbNo: String?)

    private val wbIdData = MutableLiveData<WaybillParam>()
    val waybillLiveData = Transformations.switchMap(wbIdData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = JSONObject()
                if (param.wbId != -9999) {
                    response = UserNetwork.queryVisaById(param.wbId)
                } else if (param.wbNo != null) {
//                    response = UserNetwork.queryFestByWbNoGnjCangDan(param.wbNo)
                } else {
                }
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun queryWaybillById(wbId: Int, wbNo: String?) {
        wbIdData.value = WaybillParam(wbId, wbNo)
    }


    /**
     * 外包装
     */
    var outerPackageTypeList = JSONArray()
    private val outerPackageTypeLiveData = MutableLiveData<String>()
    fun outerPackageType(time: String) {
        outerPackageTypeLiveData.value = time
    }

    val outerPackageTypeObserver = Transformations.switchMap(outerPackageTypeLiveData) { _ ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.outerPackageType()
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    /**
     * 包装破损情况
     */
    var damageTypeList = JSONArray()
    private val damageTypeLiveData = MutableLiveData<String>()
    fun damageType(type: String) {
        damageTypeLiveData.value = type
    }

    val damageTypeObserver = Transformations.switchMap(damageTypeLiveData) { type ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.damageType()
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    /**
     * 内容物情况
     */
    var contentTypeList = JSONArray()
    private val contentTypeLiveData = MutableLiveData<String>()
    fun contentType(time: String) {
        contentTypeLiveData.value = time
    }

    val contentTypeObserver = Transformations.switchMap(contentTypeLiveData) { _ ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.contentType()
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    /**
     * 不正常类型
     */
    var unusualTypeList = JSONArray()
    private val unusualTypeLiveData = MutableLiveData<String>()
    fun unusualType(time: String) {
        unusualTypeLiveData.value = time
    }

    val unusualTypeObserver = Transformations.switchMap(unusualTypeLiveData) { _ ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.unusualType()
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    /**
     * 获取发现时间类型
     */
    var searchDateTypeList = JSONArray()
    private val searchDateTypeLiveData = MutableLiveData<String>()
    fun searchDateType(time: String) {
        searchDateTypeLiveData.value = time
    }

    val searchDateTypeObserver = Transformations.switchMap(searchDateTypeLiveData) { _ ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.searchDateType()
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    /**
     * 进港 新增舱单
     */
    private val saveFestGnjCangDanLiveData = MutableLiveData<JSONObject>()
    val saveFestGnjCangDanObserver = Transformations.switchMap(saveFestGnjCangDanLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.saveVisa(param)
                emit(response)

            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun saveFestGnjCangDan(
        paramObj: JSONObject
    ) {
        saveFestGnjCangDanLiveData.value = paramObj
    }

    /**
     * 进港 修改舱单
     */
    private val updateFestGnjCangDanLiveData = MutableLiveData<JSONObject>()
    val updateFestGnjCangDanObserver = Transformations.switchMap(updateFestGnjCangDanLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.updateVisa(param)
                emit(response)

            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun updateFestGnjCangDan(paramObj: JSONObject) {
        updateFestGnjCangDanLiveData.value = paramObj
    }

    /**
     * 上传图片
     */
    /*private val uploadImgLiveData = MutableLiveData<MultipartBody.Part>()
    val uploadImgObserver = Transformations.switchMap(uploadImgLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.uploadImg(param)
                emit(response)

            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun uploadImg(
        file: MultipartBody.Part
    ) {
        uploadImgLiveData.value = file
    }*/

    val uploadImgObserver = MutableSharedFlow<JSONObject>(replay = 1)
    fun uploadImg(file: MultipartBody.Part) {
        viewModelScope.launch {
            flow {
                val response = UserNetwork.uploadImg(file)
                //返回结果
                emit(response)
            }.flowOn(Dispatchers.Default)
                .catch { e ->
                    Log.e("GnjChuKuListViewModel", e.stackTraceToString())
                }
                .collect { response ->
                    uploadImgObserver.emit(response)
                }
        }
    }


    /**
     * 删除图片
     */
    private val deleteImgLiveData = MutableLiveData<String>()
    val deleteImgObserver = Transformations.switchMap(deleteImgLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.deleteImg(param)
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun deleteImg(name: String) {
        deleteImgLiveData.value = name
    }

    /**
     * 上传图片数组
     */
    private val uploadImgListLiveData = MutableLiveData<List<MultipartBody.Part>>()
    val uploadImgListObserver = Transformations.switchMap(uploadImgListLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.uploadImgList(param)
                emit(response)

            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun uploadImgList(
        files: List<MultipartBody.Part>
    ) {
        uploadImgListLiveData.value = files
    }

    /**
     * 查询航班
     */
    private val queryFlightLiveData = MutableLiveData<JSONObject>()
    val queryFlightObserver = Transformations.switchMap(queryFlightLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.queryFlightMitInPack(param)
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun queryFlight(fdate: String, fno: String) {
        val temObj = JSONObject()
        temObj["countryType"] = null
        temObj["fclose"] = null
        temObj["fdate"] = fdate.emptyToNull()
        temObj["fdep"] = null
        temObj["fdest"] = null
        temObj["fid"] = null
        temObj["fno"] = fno.emptyToNull()
        temObj["mclose"] = null


        queryFlightLiveData.value = temObj
    }
}

