import SwiftUI

public struct CircularProgressView: View {
    private let progress: Double

    public init(progress: Double) {
        self.progress = progress
    }

    public var body: some View {
        Circle()
            .trim(from: 0, to: progress)
            .stroke(style: StrokeStyle(lineWidth: 2, lineCap: .round))
            .rotationEffect(.degrees(-90))
            .animation(.easeIn, value: progress)
    }
}

public struct InfiniteCircularProgressView: View {
    private let strokeColor: Color
    private let lineWidth: CGFloat
    @State private var isAnimating = false

    public init(strokeColor: Color = .blue, lineWidth: CGFloat = 4) {
        self.strokeColor = strokeColor
        self.lineWidth = lineWidth
    }

    public var body: some View {
        Circle()
            .trim(from: 0, to: 0.4)
            .stroke(strokeColor, lineWidth: lineWidth)
            .rotationEffect(.degrees(isAnimating ? 360 : 0))
            .onAppear {
                withAnimation(.linear(duration: 2).repeatForever(autoreverses: false)) {
                    isAnimating = true
                }
            }
    }
}

public struct InfiniteProgressBarView: View {
    @State private var isAnimating = false

    public init() {}

    public var body: some View {
        AngularGradient(
            colors: [
                Color.PaletteV1.red1,
                Color.PaletteV1.pink1,
                Color.yellow.opacity(0.8),
                Color.yellow.opacity(0.8),
                Color.PaletteV1.pink1,
                Color.PaletteV1.red1,
                Color.yellow.opacity(0.8),
                Color.yellow.opacity(0.8),
                Color.PaletteV1.red1,
            ],
            center: isAnimating ? .topLeading : .topTrailing,
            angle: .degrees(isAnimating ? 45 : -45)
        )
        .onAppear {
            withAnimation(.linear(duration: 2).repeatForever(autoreverses: true)) {
                isAnimating.toggle()
            }
        }
    }
}

#Preview {
    CircularProgressView(progress: 0.3)
        .frame(width: 60, height: 60)
}
