package com.zaojiao.app.data.repo.impl

import com.zaojiao.app.core.common.remote.di.ApplicationScope
import com.zaojiao.app.core.common.state.DataState
import com.zaojiao.app.data.local.user.LocalUserData
import com.zaojiao.app.data.model.UserModel
import com.zaojiao.app.data.remote.RemoteUserData
import com.zaojiao.app.data.repo.UserRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import javax.inject.Inject


class UserRepositoryImpl @Inject constructor(
    private val localUserData: LocalUserData,
    private val remoteUserData: RemoteUserData,
    @ApplicationScope private val coroutineScope: CoroutineScope,
) : UserRepository {
    override val user: Flow<UserModel?> get() = localUserData.user

    private val _fansCount = MutableStateFlow(0)
    override val fansCount: Flow<Int> get() = _fansCount

    private val _followerCount = MutableStateFlow(0)
    override val followerCount: Flow<Int> get() = _followerCount

    private val _phoneNumber = MutableStateFlow<String?>(null)
    override val phoneNumber: Flow<String?> get() = _phoneNumber

    private val _wechatState = MutableStateFlow(false)
    override val wechatState: Flow<Boolean> get() = _wechatState

    override suspend fun getUser() {
        remoteUserData.getUser().apply {
            when (this) {
                is DataState.Failure -> {}
                is DataState.Success -> {
                    data?.apply {
                        localUserData.updateUser(this.userModel)
                        _fansCount.emit(this.fansCount)
                        _followerCount.emit(this.followCount)
                    }
                }
            }
        }
    }

    override suspend fun updateUser(userModel: UserModel) {
        TODO("Not yet implemented")
    }


    override suspend fun deleteUser() {

    }
}