package com.adealink.weparty.location.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.adealink.frame.base.Rlt
import com.adealink.frame.data.json.toJsonErrorNull
import com.adealink.frame.locale.country.getNewCountryCode
import com.adealink.frame.locale.language.languageManager
import com.adealink.frame.log.Log
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.frame.network.data.NetworkResultNullError
import com.adealink.frame.util.getNetworkOperator
import com.adealink.frame.util.getSimOperator
import com.adealink.weparty.App
import com.adealink.weparty.location.constant.LocationInfoToJsonError
import com.adealink.weparty.location.constant.TAG_LOCATION_REPORT
import com.adealink.weparty.location.data.LocationInfo
import com.adealink.weparty.location.data.ReportLocationReq
import com.adealink.weparty.location.data.UserCountryInfo
import com.adealink.weparty.location.datasource.remote.LocationHttpService
import kotlinx.coroutines.launch

class LocationViewModel : BaseViewModel(), ILocationViewModel {

    private val locationHttpService by lazy {
        App.instance.networkService.getHttpService(LocationHttpService::class.java)
    }

    override fun reportLocation(): LiveData<Rlt<String>> {
        val liveData: LiveData<Rlt<String>> = MutableLiveData()
        viewModelScope.launch {
            Log.d(TAG_LOCATION_REPORT, "reportLocation")
            val latitude: Long? = null
            val longitude: Long? = null
            var simMcc = ""
            var simMnc = ""
            var netMcc = ""
            var netMnc = ""
            val simMccMnc: String = getSimOperator()
            if (simMccMnc.length >= 3) {
                simMcc = simMccMnc.substring(0, 3)
            }
            if (simMccMnc.length >= 4) {
                simMnc = simMccMnc.substring(3)
            }
            val netMccMnc: String = getNetworkOperator()
            if (netMccMnc.length >= 3) {
                netMcc = netMccMnc.substring(0, 3)
            }
            if (netMccMnc.length >= 5) {
                netMnc = netMccMnc.substring(3, 5)
            }
            val locationInfo = LocationInfo(latitude, longitude,
                getNewCountryCode(),
                languageManager?.getLanguageCode() ?: "",
                simMcc, simMnc, netMcc, netMnc, "")
            val locationInfoJson = toJsonErrorNull(locationInfo)
            if (locationInfoJson.isNullOrEmpty()) {
                liveData.send(Rlt.Failed(LocationInfoToJsonError()))
                return@launch
            }

            when (val result =
                locationHttpService.reportLocation(ReportLocationReq(locationInfoJson))) {
                is Rlt.Success -> {
                    liveData.send(Rlt.Success(locationInfoJson))
                    getMyCountryInfo()
                }
                is Rlt.Failed -> {
                    liveData.send(result)
                }
            }
        }
        return liveData
    }

    override fun getMyCountryInfo() {
        viewModelScope.launch {
            // TODO:
//            val myUid = ProfileModule.getMyUid()
//            when(val result = locationHttpService.getUserCountryInfos(listOf(myUid))) {
//                is Rlt.Success -> {
//                    val res = result.data.data
//                    if (res != null) {
//                        res.userCountryMap[myUid]?.let {
//                            LocationLocalService.countryCode = it.country
//                            ProfileModule.updateMyUserCountryInfo(it.country, it.region, it.countryFlag)
//                        }
//                    }
//                }
//                is Rlt.Failed -> {
//                }
//            }
        }

    }


    override fun getUserCountryInfos(ids: Set<Long>): LiveData<Rlt<Map<Long, UserCountryInfo>>> {
        val liveData: LiveData<Rlt<Map<Long, UserCountryInfo>>> = MutableLiveData()
        viewModelScope.launch {
            when(val result = locationHttpService.getUserCountryInfos(ids.toList())) {
                is Rlt.Success -> {
                    val res = result.data.data
                    if (res != null) {
                        liveData.send(Rlt.Success(res.userCountryMap))
                    } else {
                        liveData.send(Rlt.Failed(NetworkResultNullError()))
                    }
                }
                is Rlt.Failed -> {
                    liveData.send(result)
                }
            }
        }
        return liveData
    }

}