//
// OpenRocky — Voice-first AI Agent
// https://github.com/openrocky
//
// Developed by everettjf with the assistance of Claude Code and Codex.
// Copyright (c) 2026 everettjf. All rights reserved.
//

import SwiftUI

/// Compact "now playing" pill that floats just below the nav bar while media is loaded.
/// Tap to open the full player sheet.
struct OpenRockyMediaPlayerBar: View {
    @ObservedObject private var service = OpenRockyMediaPlayerService.shared
    @State private var showsSheet = false

    var body: some View {
        Group {
            if service.currentIndex != nil {
                content
                    .transition(.move(edge: .top).combined(with: .opacity))
            }
        }
        .animation(.spring(duration: 0.3), value: service.currentIndex != nil)
        .sheet(isPresented: $showsSheet) {
            OpenRockyMediaPlayerSheet()
                .presentationDetents([.large])
                .presentationDragIndicator(.visible)
        }
    }

    private var content: some View {
        Button {
            showsSheet = true
        } label: {
            HStack(spacing: 10) {
                Image(systemName: iconForCurrent)
                    .font(.system(size: 14, weight: .bold))
                    .foregroundStyle(.white)
                    .frame(width: 28, height: 28)
                    .background(
                        LinearGradient(
                            colors: [Color.blue, Color.purple],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        ),
                        in: RoundedRectangle(cornerRadius: 8, style: .continuous)
                    )

                Text(currentFilename)
                    .font(.system(size: 13, weight: .medium, design: .rounded))
                    .foregroundStyle(.primary)
                    .lineLimit(1)

                Spacer(minLength: 4)

                Button {
                    try? service.previous()
                } label: {
                    Image(systemName: "backward.fill")
                        .font(.system(size: 13, weight: .medium))
                        .foregroundStyle(.secondary)
                        .frame(width: 28, height: 28)
                }
                .buttonStyle(.plain)

                Button {
                    if service.isPlaying { service.pause() } else { service.resume() }
                } label: {
                    Image(systemName: service.isPlaying ? "pause.fill" : "play.fill")
                        .font(.system(size: 14, weight: .bold))
                        .foregroundStyle(.primary)
                        .frame(width: 28, height: 28)
                }
                .buttonStyle(.plain)

                Button {
                    try? service.next()
                } label: {
                    Image(systemName: "forward.fill")
                        .font(.system(size: 13, weight: .medium))
                        .foregroundStyle(.secondary)
                        .frame(width: 28, height: 28)
                }
                .buttonStyle(.plain)
            }
            .padding(.horizontal, 10)
            .padding(.vertical, 6)
            .background(
                RoundedRectangle(cornerRadius: 14, style: .continuous)
                    .fill(.ultraThinMaterial)
                    .shadow(color: .black.opacity(0.18), radius: 12, y: 4)
            )
        }
        .buttonStyle(.plain)
        .padding(.horizontal, 14)
    }

    private var currentFilename: String {
        guard let idx = service.currentIndex, service.playlist.indices.contains(idx) else {
            return "No media"
        }
        return service.playlist[idx].filename
    }

    private var iconForCurrent: String {
        guard let idx = service.currentIndex, service.playlist.indices.contains(idx) else {
            return "music.note"
        }
        return service.playlist[idx].kind == .video ? "play.tv" : "music.note"
    }
}
