import ComposableArchitecture
import SwiftUI

public struct PlayPauseButton: View {
    var isPlaying: Bool
    var action: () -> Void

    public init(isPlaying: Bool, action: @escaping () -> Void) {
        self.isPlaying = isPlaying
        self.action = action
    }

    public var body: some View {
        Button(action: action) {
            ZStack {
                Image(systemName: "play.fill")
                    .foregroundColor(.SemanticV1.iconOnDark)
                    .opacity(isPlaying ? .zero : .one)
                    .animation(.bouncy(extraBounce: 0.3), value: isPlaying)

                Image.Icon.pause
                    .foregroundColor(.SemanticV1.iconOnDark)
                    .opacity(isPlaying ? .one : .zero)
                    .animation(.bouncy(extraBounce: 0.3), value: isPlaying)
            }
            .frame(width: 40, height: 40)
        }
    }
}

public struct TimeElapsedView: View {
    let currentTime: Double
    let trimStart: Double
    let trimEnd: Double
    let totalDuration: Double

    public init(currentTime: Double, trimStart: Double, trimEnd: Double, totalDuration: Double) {
        self.currentTime = currentTime
        self.trimStart = trimStart
        self.trimEnd = trimEnd
        self.totalDuration = totalDuration
    }

    public var formattedTime: String {
        let current = max(0, min(currentTime - trimStart * totalDuration, (trimEnd - trimStart) * totalDuration))
        let total = (trimEnd - trimStart) * totalDuration
        return String(format: "%02d:%02d/%02d:%02d",
                      Int(current) / 60, Int(current) % 60,
                      Int(total) / 60, Int(total) % 60)
    }

    public var body: some View {
        Text(formattedTime)
            .foregroundColor(.white)
            .typographyV1(.monospace.lineHeight(24.0))
            .frame(height: 50)
            .padding(.trailing, 12)
    }
}
