package com.example.musicapp.ViewModel

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.musicapp.Data.AlbumDetails
import com.example.musicapp.Data.Repository.DeezerRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class AlbumDetailsViewModel : ViewModel() {
    private val deezerRepository = DeezerRepository()

    private val _albumDetails = MutableStateFlow<AlbumDetails?>(null)
    val albumDetails: StateFlow<AlbumDetails?>
        get() = _albumDetails

    fun fetchAlbumDetails(albumId: Int) {
        viewModelScope.launch {
            try {
                val albumDetails = deezerRepository.getAlbumDetails(albumId)
                _albumDetails.value = albumDetails
            } catch (e: Exception) {
                Log.e("AlbumDetailsViewModel", "Failed to fetch album details: " + e.message)
            }
        }
    }
},package com.example.musicapp.ViewModel

import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.SimpleExoPlayer

class PlayerViewModel : ViewModel() {
    val currentPlayer = mutableStateOf<SimpleExoPlayer?>(null)

    fun playSong(player: SimpleExoPlayer) {
        currentPlayer.value?.let {
            if (it != player) {
                it.pause()
            }
        }
        if (player.playbackState == ExoPlayer.STATE_ENDED) {
            player.seekToDefaultPosition()
        }
        player.play()
        currentPlayer.value = player
    }

    fun pauseSong() {
        currentPlayer.value?.pause()
    }

    override fun onCleared() {
        super.onCleared()
        currentPlayer.value?.release()
    }
},package com.example.musicapp.Data

data class Song(
    val id: Int,
    val title: String,
    val duration: Int, // in seconds
    val cover_medium: String,
    val preview: String // 30 seconds preview URL
),package com.example.musicapp.Data

data class Album(
    val id: Int,
    val title: String,
    val link: String,
    val cover: String,
    val cover_small: String,
    val cover_medium: String,
    val cover_big: String,
    val cover_xl: String,
    val release_date: String,
    val tracklist: String,
    val type: String
),package com.example.musicapp.Data

data class AlbumDetails(
    val id: Int,
    val title: String,
    val cover_medium: String,
    val songs: List<Song>
)-> these are my other classes separated with "," so you can understand better. But in this class after your suggestions ->package com.example.musicapp.Interface

import TopBar
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.musicapp.Data.Song
import com.example.musicapp.ViewModel.AlbumDetailsViewModel

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavController
import com.google.android.exoplayer2.MediaItem
import androidx.compose.runtime.remember
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import coil.compose.rememberAsyncImagePainter
import com.google.android.exoplayer2.SimpleExoPlayer
import com.example.musicapp.ViewModel.PlayerViewModel


@Composable
fun AlbumDetailScreen(albumId: Int, navController: NavController) {
    val albumDetailsViewModel: AlbumDetailsViewModel = viewModel()
    val playerViewModel: PlayerViewModel = viewModel()
    albumDetailsViewModel.fetchAlbumDetails(albumId)
    val albumDetails by albumDetailsViewModel.albumDetails.collectAsState()

    albumDetails?.let { details ->
        Scaffold(
            topBar = {
                TopBar(title = details.title)
            },
            content = { padding ->
                Column(
                    modifier = Modifier.padding(padding)
                ) {
                    LazyColumn {
                        items(details.songs) { song ->
                            AlbumDetailItem(song, navController, playerViewModel)
                        }
                    }
                }
            }
        )
    }
}
/*
@Composable
fun AlbumDetailItem(song: Song, navController: NavController, playerViewModel: PlayerViewModel) {
    val context = LocalContext.current
    val player = remember(context) {
        SimpleExoPlayer.Builder(context).build().apply {
            setMediaItem(MediaItem.fromUri(song.preview))
            prepare()
        }
    }

    DisposableEffect(song.preview) {
        onDispose {
            player.stop()
            player.release()
        }
    }

    Row(
        modifier = Modifier
            .padding(horizontal = 16.dp, vertical = 8.dp)
            .background(
                color = Color.Black.copy(alpha = 0.5f),
                shape = RoundedCornerShape(8.dp)
            )
            .border(
                width = 1.dp,
                color = Color.LightGray,
                shape = RoundedCornerShape(8.dp)
            )
            .clickable(onClick = {
                if (player.isPlaying) {
                    playerViewModel.pauseSong()
                } else {
                    playerViewModel.playSong(player)
                }
            })
    ) {
        Column(
            modifier = Modifier
                .weight(1f)
                .padding(start = 16.dp)
                .align(Alignment.CenterVertically)
        ) {
            Text(
                text = song.title ?: "Unknown title",
            style = MaterialTheme.typography.subtitle1,
            color = Color.White
            )
            Text(
                text = "${song.duration}" ?: "Unknown duration",
            style = MaterialTheme.typography.caption,
            color = Color.White
            )
        }
    }
}

 */
@Composable
fun AlbumDetailItem(song: Song, navController: NavController, playerViewModel: PlayerViewModel) {
    Row(
        modifier = Modifier
            .padding(horizontal = 16.dp, vertical = 8.dp)
            .background(
                color = Color.Black.copy(alpha = 0.5f),
                shape = RoundedCornerShape(8.dp)
            )
            .border(
                width = 1.dp,
                color = Color.LightGray,
                shape = RoundedCornerShape(8.dp)
            )
            .clickable(onClick = {
                navController.navigate("songDetail/${song.id}")
            })
    ) {
        val painter = rememberAsyncImagePainter(model = song.album.cover_medium)
        Image(
            painter = painter,
            contentDescription = song.title,
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(100.dp)
                .align(Alignment.CenterVertically)
        )

        Column(
            modifier = Modifier
                .weight(1f)
                .padding(start = 16.dp)
                .align(Alignment.CenterVertically)
        ) {
            Text(
                text = song.title ?: "Unknown title",
                style = MaterialTheme.typography.subtitle1,
                color = Color.White
            )
            Text(
                text = "${song.duration}" ?: "Unknown duration",
                style = MaterialTheme.typography.caption,
                color = Color.White
            )
        }

        IconButton(
            onClick = {
                if (playerViewModel.currentSong?.id == song.id) {
                    if (playerViewModel.isPlaying) {
                        playerViewModel.pauseSong()
                    } else {
                        playerViewModel.resumeSong()
                    }
                } else {
                    playerViewModel.playSong(song, navController)
                }
            },
            modifier = Modifier.align(Alignment.CenterVertically)
        ) {
            Icon(
                painter = painterResource(id = if (playerViewModel.currentSong?.id == song.id && playerViewModel.isPlaying) R.drawable.ic_pause else R.drawable.ic_play),
                contentDescription = if (playerViewModel.currentSong?.id == song.id && playerViewModel.isPlaying) "Pause" else "Play"
            )
        }
    }
}-> i have these errors now ->Unresolved reference: album:142,Unresolved reference: currentSong:172,Unresolved reference: isPlaying:173,Unresolved reference: resumeSong:176,Type mismatch: inferred type is Song but SimpleExoPlayer was expected:179,Too many arguments for public final fun playSong(player: SimpleExoPlayer): Unit defined in com.example.musicapp.ViewModel.PlayerViewModel:179,Unresolved reference: currentSong:185,Unresolved reference: isPlaying:185,Unresolved reference: drawable:185,Unresolved reference: drawable:185,Unresolved reference: currentSong:186,Unresolved reference: isPlaying:186