//
//  SoundControlView.swift
//  TheNoiseClock
//
//  Created by Matt Bruce on 9/7/25.
//

import SwiftUI
import Bedrock
import AudioPlaybackKit

/// Component for audio playback controls
struct SoundControlView: View {
    
    // MARK: - Properties
    let isPlaying: Bool
    let selectedSound: Sound?
    let onPlay: (Sound) -> Void
    let onStop: () -> Void
    
    // MARK: - Body
    var body: some View {
        VStack(spacing: Design.Spacing.medium) {
            // Sound info header
            if let sound = selectedSound {
                HStack {
                    VStack(alignment: .leading, spacing: 4) {
                        Text(sound.name)
                            .font(.headline.weight(.semibold))
                            .foregroundColor(AppTextColors.primary)
                        
                        Text(sound.description)
                            .font(.caption)
                            .foregroundColor(AppTextColors.secondary)
                            .lineLimit(2)
                    }
                    
                    Spacer()
                    
                    // Category badge
                    Text(sound.category.capitalized)
                        .font(.caption2.weight(.medium))
                        .foregroundColor(.white)
                        .padding(.horizontal, 8)
                        .padding(.vertical, 4)
                        .background(AppAccent.primary, in: Capsule())
                }
                .contentPadding(horizontal: Design.Spacing.medium, vertical: Design.Spacing.small)
                .background(AppSurface.overlay, in: RoundedRectangle(cornerRadius: Design.CornerRadius.large))
            }
            
            // Main control button
            Button(action: {
                if isPlaying {
                    onStop()
                } else if let sound = selectedSound {
                    onPlay(sound)
                }
            }) {
                HStack(spacing: Design.Spacing.small) {
                    Image(systemName: isPlaying ? "stop.fill" : "play.fill")
                        .font(.title2.weight(.semibold))
                        .foregroundColor(.white)
                    
                    Text(isPlaying ? "Stop Sound" : "Play Sound")
                        .font(.headline.weight(.semibold))
                        .foregroundColor(.white)
                }
                .contentPadding(horizontal: Design.Spacing.large, vertical: Design.Spacing.medium)
                .background(
                    RoundedRectangle(cornerRadius: Design.CornerRadius.appLarge)
                        .fill(isPlaying ? Color.red : AppAccent.primary)
                        .shadow(color: (isPlaying ? Color.red : AppAccent.primary).opacity(0.3), radius: 8, x: 0, y: 4)
                )
            }
            .disabled(selectedSound == nil)
            .scaleEffect(selectedSound == nil ? 0.95 : 1.0)
            .opacity(selectedSound == nil ? 0.6 : 1.0)
            .animation(.easeInOut(duration: 0.2), value: isPlaying)
            .animation(.easeInOut(duration: 0.2), value: selectedSound)
        }
        .frame(maxWidth: 400) // Reasonable max width for iPad
        .padding(Design.Spacing.medium)
        .background(AppSurface.overlay, in: RoundedRectangle(cornerRadius: Design.CornerRadius.appLarge))
        .overlay(
            RoundedRectangle(cornerRadius: Design.CornerRadius.appLarge)
                .stroke(AppBorder.subtle, lineWidth: Design.LineWidth.thin)
        )
    }
}

// MARK: - Preview
#Preview("Not Playing") {
    SoundControlView(
        isPlaying: false,
        selectedSound: Sound(
            name: "White Noise",
            fileName: "white-noise.mp3",
            category: "ambient",
            description: "Classic white noise for focus and relaxation",
            bundleName: "Ambient"
        ),
        onPlay: { _ in },
        onStop: {}
    )
    .padding()
    .background(Color.black)
}

#Preview("Playing") {
    SoundControlView(
        isPlaying: true,
        selectedSound: Sound(
            name: "Heavy Rain",
            fileName: "heavy-rain-white-noise.mp3",
            category: "nature",
            description: "Soothing rain sounds for deep relaxation and sleep",
            bundleName: "Nature"
        ),
        onPlay: { _ in },
        onStop: {}
    )
    .padding()
    .background(Color.black)
}
