import SwiftUI

struct PlaylistRow: View {
    let track: Track
    let index: Int
    let isCurrentTrack: Bool
    let isPlaying: Bool
    let onSelect: (Int) -> Void
    let onDelete: (Int) -> Void
    
    var body: some View {
        HStack {
            if isCurrentTrack {
                Image(systemName: isPlaying ? "speaker.wave.2.fill" : "music.note")
                    .foregroundStyle(isPlaying ? .green : .blue)
                    .frame(width: 24)
            } else {
                Image(systemName: "music.note")
                    .foregroundStyle(.secondary)
                    .frame(width: 24)
            }
            
            VStack(alignment: .leading, spacing: 2) {
                Text(track.name)
                    .font(.subheadline)
                    .foregroundStyle(isCurrentTrack ? .blue : .primary)
                    .lineLimit(1)
                
                Text(formatDuration(track.duration))
                    .font(.caption)
                    .foregroundStyle(.secondary)
            }
            
            Spacer()
            
            Button(action: { onDelete(index) }) {
                Image(systemName: "trash")
                    .foregroundStyle(.red.opacity(0.7))
                    .font(.caption)
            }
        }
        .padding(.vertical, 8)
        .padding(.horizontal, 10)
        .background(
            isCurrentTrack ?
            Color.blue.opacity(0.1) :
            Color.clear
        )
        .cornerRadius(8)
        .contentShape(Rectangle())
        .onTapGesture {
            onSelect(index)
        }
    }
    
    func formatDuration(_ duration: TimeInterval) -> String {
        let minutes = Int(duration) / 60
        let seconds = Int(duration) % 60
        return String(format: "%d:%02d", minutes, seconds)
    }
}
