package com.radiola.ui.charts import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.radiola.domain.model.ChartEntry import com.radiola.domain.model.ChartPeriod import com.radiola.domain.model.TrackStats import com.radiola.domain.repository.ChartsRepository import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch import javax.inject.Inject @HiltViewModel class ChartsViewModel @Inject constructor( private val chartsRepository: ChartsRepository ) : ViewModel() { private val _period = MutableStateFlow(ChartPeriod.WEEK) val period: StateFlow = _period.asStateFlow() private val _charts = MutableStateFlow>(emptyList()) val charts: StateFlow> = _charts.asStateFlow() /** Доступные жанры для фильтра (с бэкенда). */ private val _genres = MutableStateFlow>(emptyList()) val genres: StateFlow> = _genres.asStateFlow() /** Выбранный жанр (null — «Все»). */ private val _selectedGenre = MutableStateFlow(null) val selectedGenre: StateFlow = _selectedGenre.asStateFlow() private val _isLoadingCharts = MutableStateFlow(false) val isLoadingCharts: StateFlow = _isLoadingCharts.asStateFlow() private val _selectedTrackStats = MutableStateFlow(null) val selectedTrackStats: StateFlow = _selectedTrackStats.asStateFlow() private val _isLoadingStats = MutableStateFlow(false) val isLoadingStats: StateFlow = _isLoadingStats.asStateFlow() init { loadCharts() loadGenres() } fun selectPeriod(newPeriod: ChartPeriod) { if (_period.value == newPeriod) return _period.value = newPeriod loadCharts() } fun selectGenre(genre: String?) { if (_selectedGenre.value == genre) return _selectedGenre.value = genre loadCharts() } fun selectTrack(trackId: String) { viewModelScope.launch { _isLoadingStats.value = true _selectedTrackStats.value = null try { val stats = chartsRepository.getTrackStats(trackId) _selectedTrackStats.value = stats } catch (e: Exception) { Log.e("ChartsViewModel", "Ошибка загрузки статистики трека $trackId", e) } finally { _isLoadingStats.value = false } } } fun clearSelection() { _selectedTrackStats.value = null } fun toggleLike(trackId: String) { val stats = _selectedTrackStats.value ?: return val newLiked = !stats.isLiked // Оптимистично обновляем UI _selectedTrackStats.value = stats.copy( isLiked = newLiked, totalLikes = if (newLiked) stats.totalLikes + 1 else stats.totalLikes - 1 ) viewModelScope.launch { try { chartsRepository.setLiked(trackId, newLiked) } catch (e: Exception) { Log.e("ChartsViewModel", "Ошибка переключения лайка трека $trackId", e) // Откатываем при ошибке _selectedTrackStats.value = stats } } } private fun loadCharts() { viewModelScope.launch { _isLoadingCharts.value = true try { _charts.value = chartsRepository.getCharts(_period.value, _selectedGenre.value) } catch (e: Exception) { Log.e("ChartsViewModel", "Ошибка загрузки чартов", e) _charts.value = emptyList() } finally { _isLoadingCharts.value = false } } } private fun loadGenres() { viewModelScope.launch { _genres.value = chartsRepository.getGenres() } } }