package com.adealink.weparty.profile

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.profile.IProfileService
import com.adealink.weparty.module.profile.data.UserConfigType
import com.adealink.weparty.module.profile.data.UserInfo
import com.adealink.weparty.module.profile.viewmodel.IFollowViewModel
import com.adealink.weparty.module.profile.viewmodel.IProfileViewModel
import com.adealink.weparty.profile.datasource.remote.ProfileHttpService
import com.adealink.weparty.profile.manager.profileManager
import com.adealink.weparty.profile.relation.viewmodel.FollowViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory

@RegisterService(IProfileService::class)
class ProfileServiceImpl : IProfileService {

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

    override fun getMyUid(): String {
        return profileManager.getMyUid()
    }

    override fun getMyUserInfo(): UserInfo? {
        return profileManager.getMyUserInfo()
    }

    override fun setMyUserInfo(userInfo: UserInfo) {
        profileManager.setMyUserInfo(userInfo, null)
    }

    override fun updateMyUserCountryInfo(country: String, region: String, countryFlag: String) {
        profileManager.updateMyUserCountryInfo(country, region, countryFlag)
    }

    override fun getCacheUserInfo(uid: String): UserInfo? {
        return profileManager.getCacheUserInfo(uid)
    }

    override fun updateCacheUserInfo(userInfo: UserInfo) {
        profileManager.updateCacheUserInfo(userInfo)
    }

    override suspend fun getUserInfoByUid(
        uid: String,
        cache: Boolean,
        attrSet: Set<UserConfigType>?,
    ): Rlt<UserInfo> {
        return profileManager.getUserInfoByUid(uid, cache, attrSet)
    }

    override suspend fun getUsersInfoByUid(
        uidSet: Set<String>,
        cache: Boolean,
        attrSet: Set<UserConfigType>?,
    ): Rlt<Map<String, UserInfo>> {
        return profileManager.getUsersInfoByUid(uidSet, cache, attrSet)
    }

    override fun getProfileViewModel(owner: ViewModelStoreOwner): IProfileViewModel {
        return ViewModelProvider(owner, ProfileViewModelFactory())[ProfileViewModel::class.java]
    }

    override fun getFollowViewModel(owner: ViewModelStoreOwner): IFollowViewModel? {
        return ViewModelProvider(owner, ProfileViewModelFactory())[FollowViewModel::class.java]
    }

    override fun logout() {
        profileManager.clear()
    }


    override fun getService(): IProfileService {
        return this
    }

}