//
//  ProgressBar.swift
//  lichunWebsocket
//
//  Progress bar component for showing completion percentage
//

import SwiftUI

struct ProgressBar: View {
    let value: Double // 0.0 to 1.0
    var height: CGFloat = 8
    var backgroundColor: Color = Color.white.opacity(0.2)
    var foregroundColor: Color = AppColors.primary
    var cornerRadius: CGFloat = 4
    var showLabel: Bool = false
    var label: String? = nil

    var body: some View {
        VStack(alignment: .leading, spacing: AppSpacing.xs) {
            if showLabel || label != nil {
                HStack {
                    if let labelText = label {
                        Text(labelText)
                            .font(.appCaption)
                            .foregroundColor(AppColors.primaryText)
                    }
                    Spacer()
                    if showLabel {
                        Text("\(Int(value * 100))%")
                            .font(.appCaptionBold)
                            .foregroundColor(AppColors.primaryText)
                    }
                }
            }

            GeometryReader { geometry in
                ZStack(alignment: .leading) {
                    Rectangle()
                        .fill(backgroundColor)
                        .frame(height: height)
                        .cornerRadius(cornerRadius)

                    Rectangle()
                        .fill(foregroundColor)
                        .frame(width: geometry.size.width * CGFloat(min(max(value, 0), 1)), height: height)
                        .cornerRadius(cornerRadius)
                }
            }
            .frame(height: height)
        }
    }
}

// MARK: - Preview
#if DEBUG
struct ProgressBar_Previews: PreviewProvider {
    static var previews: some View {
        VStack(spacing: AppSpacing.lg) {
            ProgressBar(value: 0.75)
            ProgressBar(value: 0.5, showLabel: true)
            ProgressBar(value: 0.3, foregroundColor: AppColors.health, showLabel: true, label: "Health")
            ProgressBar(value: 0.8, foregroundColor: AppColors.happiness, showLabel: true, label: "Happiness")
        }
        .padding()
        .background(Color.black)
    }
}
#endif
