import ComponentLibrary
import SwiftUI

struct EditClipPlayerControls: View {
    let isReadyToPlay: Bool
    let isPlaying: Bool
    let onPlay: () -> Void
    let onPause: () -> Void
    let onTapNext: () -> Void
    let onTapPrevious: () -> Void
    let onSeekForward: () -> Void
    let onSeekBackward: () -> Void
    let canPlayNext: Bool
    let canPlayPrevious: Bool

    @State private var hapticProxy: Int = .zero

    var body: some View {
        HStack(spacing: 8) {
            Button {
                hapticProxy += 1
                onTapPrevious()
            } label: {
                Image(systemName: "backward.fill")
                    .foregroundStyle(Color.SemanticV1.iconPrimary)
                    .frame(width: 24, height: 24)
                    .padding(8)
                    .contentShape(.rect)
            }
            .buttonStyle(ScaleButtonStyle(scaleAmount: 0.98))
            .disabled(!canPlayPrevious)
            .opacity(canPlayPrevious ? 1.0 : 0.5)
            Button {
                hapticProxy += 1
                switch isPlaying {
                case true: onPause()
                case false: onPlay()
                }
            } label: {
                ZStack {
                    Circle()
                        .fill(Color.SemanticV1.textTertiary.opacity(0.40))
                        .frame(width: 42, height: 42)
                    Image(systemName: isPlaying ? "pause.fill" : "play.fill")
                        .frame(width: 24, height: 24)
                        .foregroundStyle(Color.SemanticV1.iconPrimary)
                }
                .frame(width: 42, height: 42)
                .contentShape(.rect)
            }
            .buttonStyle(ScaleButtonStyle(scaleAmount: 0.98))
            .overlay {
                ProgressView()
                    .progressViewStyle(.circular)
                    .opacity(isReadyToPlay ? 0 : 1)
            }
            Button {
                hapticProxy += 1
                onTapNext()
            } label: {
                Image(systemName: "forward.fill")
                    .foregroundStyle(Color.SemanticV1.iconPrimary)
                    .frame(width: 24, height: 24)
                    .padding(8)
                    .contentShape(.rect)
            }
            .buttonStyle(ScaleButtonStyle(scaleAmount: 0.98))
            .disabled(!canPlayNext)
            .opacity(canPlayNext ? 1.0 : 0.5)
        }
        .sensoryFeedbackIfEnabled(.impact(weight: .light), trigger: hapticProxy)
    }
}
