package com.craigvg.lichun_android.viewmodel import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.craigvg.lichun_android.domain.models.* import com.craigvg.lichun_android.managers.AnalyticsManager import com.craigvg.lichun_android.managers.SoundManager import com.craigvg.lichun_android.network.WebSocketManager import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.* import javax.inject.Inject /** * ViewModel for character-specific state * Ported from iOS PlayerViewModel.swift */ @HiltViewModel class PlayerViewModel @Inject constructor( private val webSocketManager: WebSocketManager, private val soundManager: SoundManager, private val analyticsManager: AnalyticsManager ) : ViewModel() { // Current player character val person = webSocketManager.person // Player data val player = webSocketManager.player // Swipe character for dating val swipeCharacter = webSocketManager.swipeCharacter // Extracurriculars val extracurriculars = webSocketManager.extracurriculars // Date ideas val dateIdeas = webSocketManager.dateIdeas // Derived properties val fullName: StateFlow = person.map { it.fullName } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = "" ) val age: StateFlow = person.map { it.ageYears } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = 0 ) val healthPercentage: StateFlow = person.map { (it.health / 100.0).toFloat().coerceIn(0f, 1f) } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = 1f ) val happinessPercentage: StateFlow = person.map { (it.happiness / 100f).coerceIn(0f, 1f) } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = 0.5f ) val intelligencePercentage: StateFlow = person.map { (it.intelligence / 100f).coerceIn(0f, 1f) } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = 0.5f ) val prestigePercentage: StateFlow = person.map { (it.prestige / 100f).coerceIn(0f, 1f) } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = 0f ) // Relationships from player data val relationships: StateFlow> = player.map { it.r } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = emptyList() ) // Categorized relationships val romanticRelationships: StateFlow> = relationships.map { people -> people.filter { it.relationship.lowercase().contains("romantic") || it.relationship.lowercase().contains("dating") || it.relationship.lowercase().contains("married") } }.stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = emptyList() ) val familyRelationships: StateFlow> = relationships.map { people -> people.filter { it.relationship.lowercase().contains("family") || it.relationship.lowercase().contains("parent") || it.relationship.lowercase().contains("sibling") || it.relationship.lowercase().contains("child") } }.stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = emptyList() ) val friendRelationships: StateFlow> = relationships.map { people -> people.filter { it.relationship.lowercase().contains("friend") } }.stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = emptyList() ) // Habits val habits: StateFlow> = person.map { it.habits } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = emptyList() ) // Active conversations val activeConversations: StateFlow> = player.map { it.activeConversations } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = emptyList() ) // Activities val currentActivities: StateFlow> = person.map { it.activities } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = emptyList() ) // Activity records val activityRecords: StateFlow> = person.map { it.activityRecords } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = emptyList() ) // Focus options val focusOptions: StateFlow> = player.map { it.focuses } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = emptyList() ) // Occupations val occupations: StateFlow> = player.map { it.occupations } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = emptyList() ) // Store items val storeItems: StateFlow> = player.map { it.storeItems } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = emptyList() ) // In-app purchases val inAppPurchases: StateFlow> = player.map { it.inAppPurchases } .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = emptyList() ) // Methods fun isEnrolledIn(activityId: String): Boolean { return person.value.activities.any { it.id == activityId } } fun getActivityRecord(activityId: String): ActivityRecord? { return person.value.activityRecords.find { it.id == activityId } } fun quitHabit(habitName: String) { webSocketManager.quitHabit(habitName) } fun stopQuitHabit(habitName: String) { webSocketManager.stopQuitHabit(habitName) } fun setFocus(activityId: String, focusId: Int) { webSocketManager.setFocus(activityId, focusId) } fun retrievePerson(personId: String) { webSocketManager.retrievePerson(personId) } fun findPersonById(personId: String): Person? { return player.value.r.find { it.id == personId } } fun sendChatMessage(characterId: String, message: String) { webSocketManager.sendChatMessage(characterId, message) } fun callCharacter(characterId: String) { webSocketManager.callCharacter(characterId) } fun giftCharacter(characterId: String) { webSocketManager.giftCharacter(characterId) } fun updateAffinity(personId: String, change: Int) { webSocketManager.updateAffinity(personId, change) } fun swipeRight(personId: String) { webSocketManager.swipeRight(personId) soundManager.playSwipeRight() analyticsManager.logSwipeAction(personId, "right") } fun swipeLeft(personId: String) { webSocketManager.swipeLeft(personId) soundManager.playSwipeLeft() analyticsManager.logSwipeAction(personId, "left") } fun fetchSwipeCharacter() { webSocketManager.fetchSwipeCharacter() } fun enrollInActivity(activityId: String) { webSocketManager.enrollInActivity(activityId) } fun dropActivity(activityId: String) { webSocketManager.dropActivity(activityId) } fun applyForJob(jobId: String) { webSocketManager.applyForJob(jobId) } fun fetchExtracurriculars() { webSocketManager.fetchExtracurriculars() } fun fetchDateIdeas(characterId: String) { webSocketManager.fetchDateIdeas(characterId) } fun goOnDate(dateIdeaId: String, characterId: String) { webSocketManager.goOnDate(dateIdeaId, characterId) } }