package com.adealink.weparty.playmate

import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import com.adealink.frame.base.Rlt
import com.adealink.frame.spi.RegisterService
import com.adealink.weparty.App
import com.adealink.weparty.module.playmate.IPlaymateService
import com.adealink.weparty.module.playmate.viewmodel.IPlaymateDetailViewModel
import com.adealink.weparty.module.playmate.viewmodel.IPlaymateViewModel
import com.adealink.weparty.playmate.data.PlaymateUserPushQuotaReq
import com.adealink.weparty.playmate.datasource.remote.PlaymateHttpService
import com.adealink.weparty.playmate.detail.viewmodel.PlaymateDetailViewModel
import com.adealink.weparty.playmate.stat.UserPushSendMessageEvent
import com.adealink.weparty.playmate.stat.WelcomeReplyEvent
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModel

@RegisterService(IPlaymateService::class)
class PlaymateServiceImpl : IPlaymateService {

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

    override fun getService(): IPlaymateService {
        return this
    }

    override fun getPlaymateViewModel(owner: ViewModelStoreOwner): IPlaymateViewModel? {
        return ViewModelProvider(owner)[PlaymateViewModel::class.java]
    }

    override fun getPlaymateDetailViewModel(owner: ViewModelStoreOwner): IPlaymateDetailViewModel? {
        return ViewModelProvider(owner)[PlaymateDetailViewModel::class.java]
    }

    override fun reportWelcomeReply(uid: String) {
        WelcomeReplyEvent().apply {
            this.uid to uid
        }.send()
    }

    override fun reportUserPushChat(uid: String) {
        UserPushSendMessageEvent().apply {
            this.uid to uid
        }.send()
    }

    override suspend fun isUserPushChatAvailable(uid: String): Rlt<Boolean> {
        return when (val rlt =
            playmateHttpService.getUserPushChatQuota(PlaymateUserPushQuotaReq(uid))) {
            is Rlt.Failed -> {
                rlt
            }

            is Rlt.Success -> {
                Rlt.Success(rlt.data.data?.isQuotaAvailable() ?: false)
            }
        }
    }

    override fun onLogout() {

    }

}