import APIClient
import ComposableArchitecture
import SwiftUI

struct CoverModeScrubber: View {
    @Bindable var store: StoreOf<CustomCreatePlayer>
    @Binding var hasSentScrubStart: Bool
    
    var body: some View {
        VStack(spacing: 12) {
            GeometryReader { geometry in
                let width = max(1, geometry.size.width)
                ZStack(alignment: .leading) {
                    RoundedRectangle(cornerRadius: 25)
                        .fill(Color.white.opacity(0.2))
                        .frame(height: 4)

                    RoundedRectangle(cornerRadius: 25)
                        .fill(Color.FigmaMCP.Semantic.accentBrand)
                        .frame(width: geometry.size.width * CGFloat(store.elapsedTime / store.clip.duration), height: 4)

                    Circle()
                        .fill(Color.white)
                        .frame(width: 10, height: 10)
                        .shadow(color: .black.opacity(0.25), radius: 1, x: 0, y: 0)
                        .offset(x: geometry.size.width * CGFloat(store.elapsedTime / store.clip.duration) - 5)
                }
                .highPriorityGesture(
                    DragGesture(minimumDistance: 0)
                        .onChanged { value in
                            let x = min(max(0, value.location.x), width)
                            let seconds = store.clip.duration * Double(x / width)
                            if !hasSentScrubStart {
                                hasSentScrubStart = true
                                store.send(.playbackScrubbing(.beginScrubbing(seconds)))
                            }
                            store.send(.playbackScrubbing(.updateScrubbing(seconds)))
                        }
                        .onEnded { value in
                            let x = min(max(0, value.location.x), width)
                            let seconds = store.clip.duration * Double(x / width)
                            store.send(.playbackScrubbing(.endScrubbing(seconds)))
                            hasSentScrubStart = false
                        }
                )
            }
            .frame(height: 10)

            HStack {
                Text(formatTime(store.elapsedTime))
                    .figmaMCPTypography(TypographyV1.FigmaMCP.timecode.inputSans())
                    .foregroundColor(Color.white.opacity(0.5))
                    .shadow(color: .black.opacity(0.35), radius: 2, x: 0, y: 0)

                Spacer()

                Text(store.formatter.string(from: store.clip.duration) ?? "--:--")
                    .figmaMCPTypography(TypographyV1.FigmaMCP.timecode.inputSans())
                    .foregroundColor(Color.white.opacity(0.5))
                    .shadow(color: .black.opacity(0.35), radius: 2, x: 0, y: 0)
            }
        }
    }
    
    private func formatTime(_ seconds: Double) -> String {
        let minutes = Int(seconds) / 60
        let remainingSeconds = Int(seconds) % 60
        return String(format: "%d:%02d", minutes, remainingSeconds)
    }
}

