import SwiftUI
import AVFoundation
import AVKit

struct FullscreenVideoPlayer: View {
    let player: AVPlayer
    let track: Track
    @Binding var isPresented: Bool
    @State private var showControls: Bool = true
    @State private var controlsTimer: Timer?
    
    var body: some View {
        ZStack {
            // Background
            Color.black
                .ignoresSafeArea(.all)
            
            // Video Player (without native controls)
            VideoPlayer(player: player)
                .ignoresSafeArea(.all)
                .onTapGesture {
                    // Toggle play/pause when tapping video
                    if player.rate > 0 {
                        player.pause()
                    } else {
                        player.play()
                    }
                    // Also show controls briefly
                    showControls = true
                    resetControlsTimer()
                }
                .disabled(true) // Disable native controls
            
            // Always visible minimize button
            VStack {
                HStack {
                    MediumButton.secondaryIcon("Icon/chevron-down") {
                        isPresented = false
                    }
                    
                    Spacer()
                }
                .padding()
                
                Spacer()
            }
            
            // Auto-hiding play/pause button
            if showControls {
                VStack {
                    Spacer()
                    
                    // Center play/pause button
                    Button(action: {
                        if player.rate > 0 {
                            player.pause()
                        } else {
                            player.play()
                        }
                    }) {
                        Image(systemName: player.rate > 0 ? "pause.fill" : "play.fill")
                            .font(.system(size: 32, weight: .medium))
                            .foregroundColor(.white)
                            .frame(width: 80, height: 80)
                            .background(Color.black.opacity(0.6))
                            .clipShape(Circle())
                    }
                    
                    Spacer()
                }
                .transition(.opacity.animation(.easeInOut(duration: 0.3)))
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .onAppear {
            // Start timer to hide controls after 3 seconds
            resetControlsTimer()
        }
        .onDisappear {
            controlsTimer?.invalidate()
        }
        .statusBarHidden()
        .preferredColorScheme(.dark)
    }
    
    private func toggleControls() {
        withAnimation(.easeInOut(duration: 0.3)) {
            showControls.toggle()
        }
        
        if showControls {
            resetControlsTimer()
        } else {
            controlsTimer?.invalidate()
        }
    }
    
    private func resetControlsTimer() {
        controlsTimer?.invalidate()
        controlsTimer = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false) { _ in
            withAnimation(.easeInOut(duration: 0.3)) {
                showControls = false
            }
        }
    }
}

#Preview {
    // Create a sample player for preview
    let sampleURL = Bundle.main.url(forResource: "sunohook_edited", withExtension: "mov") ?? URL(fileURLWithPath: "")
    let samplePlayer = AVPlayer(url: sampleURL)
    
    let sampleTrack = Track(
        title: "Wobbly Wiggly (Remix)",
        genres: ["Drum n Bass", "Electronic", "Indie"],
        artworkName: "wobbly_wiggly",
        badge: "Video"
    )
    
    FullscreenVideoPlayer(
        player: samplePlayer,
        track: sampleTrack,
        isPresented: .constant(true)
    )
}