package com.adealink.weparty.profile.relation.viewmodel

import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LiveData
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.Rlt
import com.adealink.frame.coroutine.dispatcher.Dispatcher
import com.adealink.frame.mvvm.livedata.ExtMutableLiveData
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.frame.util.AppUtil
import com.adealink.weparty.App
import com.adealink.weparty.R
import com.adealink.weparty.commonui.widget.CommonDialog
import com.adealink.weparty.module.profile.viewmodel.IBlackViewModel
import com.adealink.weparty.profile.datasource.remote.BlackHttpService
import com.adealink.weparty.profile.relation.data.BlackReq
import com.adealink.weparty.profile.relation.data.IsBlackReq
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import kotlin.coroutines.resume
import com.adealink.weparty.R as APP_R

class BlackViewModel : BaseViewModel(), IBlackViewModel {

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

    private val blackStatusMap = mutableMapOf<String, Boolean>()

    override val isBlackLD = ExtMutableLiveData<Map<String, Boolean>>()

    override fun isBlack(uid: String): LiveData<Rlt<Boolean>> {
        val liveData = OnceMutableLiveData<Rlt<Boolean>>()
        viewModelScope.launch {
            val rlt = blackHttpService.isBlack(IsBlackReq(listOf(uid)))
            when (rlt) {
                is Rlt.Failed -> {
                    liveData.send(rlt)
                }

                is Rlt.Success -> {
                    val isBlack = rlt.data.data?.list?.find { it.uid == uid }?.black ?: false
                    blackStatusMap[uid] = isBlack
                    this@BlackViewModel.isBlackLD.send(blackStatusMap)
                    liveData.send(Rlt.Success(isBlack))
                }
            }
        }
        return liveData
    }


    override fun isBlack(uids: List<String>): LiveData<Rlt<Map<String, Boolean>>> {
        val liveData = OnceMutableLiveData<Rlt<Map<String, Boolean>>>()
        viewModelScope.launch {
            val rlt = blackHttpService.isBlack(IsBlackReq(uids.distinct()))
            when (rlt) {
                is Rlt.Failed -> {
                    liveData.send(rlt)
                }

                is Rlt.Success -> {
                    rlt.data.data?.list?.forEach {
                        blackStatusMap[it.uid] = it.black
                    }
                    this@BlackViewModel.isBlackLD.send(blackStatusMap)
                    liveData.send(Rlt.Success(blackStatusMap))
                }
            }
        }
        return liveData
    }

    override fun black(uid: String): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val activity = AppUtil.currentActivity as? FragmentActivity ?: return@launch
            val black = withContext(Dispatcher.UI) {
                checkBlackDialog(activity)
            }
            if (!black) {
                return@launch
            }
            val rlt = blackHttpService.black(BlackReq(uid, true))
            when (rlt) {
                is Rlt.Failed -> {
                    //ntd.
                }

                is Rlt.Success -> {
                    onBlackStatusChanged(uid, true)
                }
            }
            liveData.send(rlt)
        }
        return liveData
    }

    private suspend fun checkBlackDialog(activity: FragmentActivity): Boolean {
        return suspendCancellableCoroutine { coroutine ->
            CommonDialog.Builder()
                .message(getCompatString(APP_R.string.common_black_tips))
                .setShowDefaultCancel(true)
                .onPositive {
                    if (coroutine.isActive) {
                        coroutine.resume(true)
                    }
                }
                .onNegative {
                    if (coroutine.isActive) {
                        coroutine.resume(false)
                    }
                }
                .onDismiss {
                    if (coroutine.isActive) {
                        coroutine.resume(false)
                    }
                }
                .build()
                .show(activity.supportFragmentManager)
        }
    }

    override fun unBlack(uid: String): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val rlt = blackHttpService.black(BlackReq(uid, false))
            when (rlt) {
                is Rlt.Failed -> {
                    //ntd.
                }

                is Rlt.Success -> {
                    onBlackStatusChanged(uid, false)
                }
            }
            liveData.send(rlt)
        }
        return liveData
    }


    private fun onBlackStatusChanged(uid: String, black: Boolean) {
        blackStatusMap[uid] = black
        isBlackLD.send(blackStatusMap)
    }

}