import SwiftUI

// MARK: - Toolbar View

/// The main toolbar containing all playback controls.
struct ToolbarView: View {
    @ObservedObject var playbackState: PlaybackState
    let partNames: [String]

    // Callbacks — MainWindow drives the audio engine directly
    var onPlay: () -> Void
    var onStop: () -> Void
    var onRecord: () -> Void
    var onStopRecording: () -> Void

    var body: some View {
        HStack(spacing: 12) {
            // 1. Part dropdown
            Picker("Part", selection: $playbackState.currentPart) {
                Text("All").tag("All")
                ForEach(partNames, id: \.self) { name in
                    Text(name).tag(name)
                }
            }
            .frame(width: 120)

            Divider().frame(height: 24)

            // 2. Play All vs Play Current Part toggle
            Picker("", selection: $playbackState.partMode) {
                Text("Play All").tag(PlaybackState.PartMode.all)
                Text("Play Current").tag(PlaybackState.PartMode.current)
            }
            .pickerStyle(.segmented)
            .frame(width: 180)

            Divider().frame(height: 24)

            // 3. Tones vs Recorded toggle
            Picker("", selection: $playbackState.audioSource) {
                Text("Tones").tag(PlaybackState.AudioSource.tones)
                Text("Recorded").tag(PlaybackState.AudioSource.recorded)
            }
            .pickerStyle(.segmented)
            .frame(width: 160)

            Divider().frame(height: 24)

            // 4. BPM slider
            HStack(spacing: 4) {
                Text("\(playbackState.tempo)")
                    .font(.system(size: 11, weight: .medium, design: .monospaced))
                    .frame(width: 30, alignment: .trailing)
                Slider(
                    value: Binding(
                        get: { Double(playbackState.tempo) },
                        set: { playbackState.tempo = Int($0) }
                    ),
                    in: Double(AppConstants.minTempo)...Double(AppConstants.maxTempo),
                    step: 1
                )
                .frame(width: 100)
                Text("bpm")
                    .font(.system(size: 11))
                    .foregroundColor(.secondary)
            }

            Divider().frame(height: 24)

            // 5. Loop checkbox
            Toggle("Loop", isOn: $playbackState.isLooping)
                .toggleStyle(.checkbox)

            Divider().frame(height: 24)

            // 6. Play button
            Button(action: {
                if playbackState.isPlaying {
                    onStop()
                } else {
                    onPlay()
                }
            }) {
                Image(systemName: playbackState.isPlaying ? "stop.fill" : "play.fill")
                    .font(.system(size: 16))
            }
            .keyboardShortcut(.space, modifiers: [])
            .help(playbackState.isPlaying ? "Stop" : "Play")

            // 7. Record button
            Button(action: {
                if playbackState.isRecording {
                    onStopRecording()
                } else {
                    onRecord()
                }
            }) {
                HStack(spacing: 4) {
                    Image(systemName: playbackState.isRecording ? "stop.circle.fill" : "record.circle")
                        .font(.system(size: 16))
                        .foregroundColor(playbackState.isRecording ? .red : .primary)
                    if playbackState.isRecording {
                        // Pulsing red dot to indicate recording
                        Circle()
                            .fill(Color.red)
                            .frame(width: 8, height: 8)
                            .opacity(playbackState.isRecording ? 1.0 : 0.0)
                        Text("REC")
                            .font(.system(size: 10, weight: .bold))
                            .foregroundColor(.red)
                    }
                }
            }
            .help(playbackState.isRecording ? "Stop Recording" : "Record")
            .disabled(playbackState.currentPart == "All")

            Spacer()
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 8)
        .background(Color(nsColor: .controlBackgroundColor))
    }
}
