package com.aiosman.riderpro.ui.profile

import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.lifecycle.viewmodel.compose.viewModel
import com.aiosman.riderpro.LocalNavController
import com.aiosman.riderpro.exp.viewModelFactory
import com.aiosman.riderpro.ui.index.tabs.profile.MyProfileViewModel
import com.aiosman.riderpro.ui.index.tabs.profile.ProfileV3
import com.aiosman.riderpro.ui.navigateToChat

@Composable
fun AccountProfileV2(id: String){
    val model: AccountProfileViewModel = viewModel(factory = viewModelFactory {
        AccountProfileViewModel()
    }, key = "viewModel_${id}")
    val navController = LocalNavController.current
    LaunchedEffect(Unit) {
        model.loadProfile(id)
        MyProfileViewModel.loadProfile()
    }
    var isSelf = false
    if (id == MyProfileViewModel.profile?.id.toString()) {
        isSelf = true
    }
    ProfileV3(
        sharedFlow = model.momentsFlow,
        profile = model.profile,
        isSelf = isSelf,
        onChatClick = {
            model.profile?.let {
                navController.navigateToChat(it.id.toString())
            }
        },
        onFollowClick = {
            model.profile?.let {
                if (it.isFollowing) {
                    model.unFollowUser(id)
                } else {
                    model.followUser(id)
                }
            }
        }
    )
}