package com.remax.visualnovel.repository.api

import com.remax.visualnovel.api.service.LoginService
import com.remax.visualnovel.entity.request.CompleteUserInfoInput
import com.remax.visualnovel.entity.request.PlatformAccountVerifyDTO
import com.remax.visualnovel.entity.response.base.Response
import com.remax.visualnovel.event.model.OnLoginEvent
import com.remax.visualnovel.manager.login.LoginManager
import com.remax.visualnovel.repository.api.base.BaseRepository
import com.pengxr.modular.eventbus.generated.events.EventDefineOfUserEvents
import javax.inject.Inject

/**
 * Created by HJW on 2022/10/28
 *
 * 用户相关
 */
open class LoginRepository @Inject constructor(
    private val loginService: LoginService
) : BaseRepository() {

    suspend fun checkUserNickname(nickname: String?,exUserId:String?) = executeHttp {
        val request = CompleteUserInfoInput(nickname = nickname, exUserId = exUserId)
        loginService.checkUserNickname(request)
    }

    suspend fun platformThirdVerify(thirdToken: String, thirdType: String) = executeHttp {
        loginService.platformThirdVerify(PlatformAccountVerifyDTO(thirdToken, thirdType))
    }.transformResult({
        if (it?.isLogin == true) {
            LoginManager.putToken(it.token)
        }
    })

    /**
     * 退出登录
     */
    suspend fun logout(): Response<Any> = executeHttp {
        loginService.logout()
    }.transformResult({
        LoginManager.logout()
    })

    suspend fun register(request: CompleteUserInfoInput) = executeHttp {
        loginService.register(request)
    }.transformResult({
        EventDefineOfUserEvents.onUserInfoChanged().post(null)
    })

}
