import SwiftUI

struct SearchBarView: View {
    @Binding var searchText: String
    let trackCount: Int
    let onSearch: (String) -> Void
    let onSaveSearch: (String) -> Void
    let isShazamListening: Bool
    let onShazam: () -> Void

    var body: some View {
        HStack(spacing: 12) {
            HStack(spacing: 8) {
                Image(systemName: "magnifyingglass")
                    .foregroundStyle(.secondary)
                TextField("Search by title, artist, album, genre...", text: $searchText)
                    .textFieldStyle(.plain)
                    .onChange(of: searchText) { _, newValue in
                        onSearch(newValue)
                    }
                if !searchText.isEmpty {
                    Button {
                        searchText = ""
                        onSearch("")
                    } label: {
                        Image(systemName: "xmark.circle.fill")
                            .foregroundStyle(.secondary)
                    }
                    .buttonStyle(.plain)

                    Button {
                        onSaveSearch(searchText)
                    } label: {
                        Image(systemName: "plus.circle")
                            .foregroundStyle(.secondary)
                    }
                    .buttonStyle(.plain)
                    .help("Save as smart playlist")
                }
            }
            .padding(8)
            .background(.quaternary)
            .clipShape(RoundedRectangle(cornerRadius: 8))

            Text("\(trackCount) tracks")
                .font(.caption)
                .foregroundStyle(.secondary)

            Button(action: onShazam) {
                Image(systemName: "shazam.logo")
                    .font(.system(size: 16))
                    .foregroundStyle(isShazamListening ? .blue : .secondary)
                    .symbolEffect(.pulse, isActive: isShazamListening)
            }
            .buttonStyle(.plain)
            .help(isShazamListening ? "Stop listening" : "Identify song with Shazam")
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 8)
    }
}
