//
//  ColorPresetButton.swift
//  SelfieCam
//
//  Created by Matt Bruce on 1/4/26.
//

import SwiftUI
import Bedrock

// MARK: - Color Preset Button

struct ColorPresetButton: View {
    let preset: RingLightColor
    let isSelected: Bool
    let isPremiumUnlocked: Bool
    let action: () -> Void

    /// Whether this premium color is locked (not available)
    private var isLocked: Bool {
        preset.isPremium && !isPremiumUnlocked
    }

    var body: some View {
        Button(action: action) {
            VStack(spacing: Design.Spacing.xxSmall) {
                ZStack {
                    Circle()
                        .fill(preset.color)
                        .frame(width: Design.Size.avatarSmall, height: Design.Size.avatarSmall)
                        .overlay(
                            Circle()
                                .strokeBorder(
                                    isSelected ? Color.Accent.primary : Color.Border.subtle,
                                    lineWidth: isSelected ? Design.LineWidth.thick : Design.LineWidth.thin
                                )
                        )
                        .shadow(
                            color: preset.color.opacity(Design.Opacity.light),
                            radius: isSelected ? Design.Shadow.radiusSmall : 0
                        )

                    // Lock overlay for locked premium colors
                    if isLocked {
                        Circle()
                            .fill(.black.opacity(Design.Opacity.medium))
                            .frame(width: Design.Size.avatarSmall, height: Design.Size.avatarSmall)

                        Image(systemName: "lock.fill")
                            .font(.system(size: Design.BaseFontSize.small))
                            .foregroundStyle(.white)
                    }
                }

                Text(preset.name)
                    .font(.system(size: Design.BaseFontSize.xSmall))
                    .foregroundStyle(.white.opacity(isSelected ? 1.0 : (isLocked ? Design.Opacity.medium : Design.Opacity.accent)))
                    .lineLimit(1)
                    .minimumScaleFactor(Design.MinScaleFactor.tight)

                if preset.isPremium {
                    Image(systemName: isPremiumUnlocked ? "crown.fill" : "crown")
                        .font(.system(size: Design.BaseFontSize.xxSmall))
                        .foregroundStyle(isPremiumUnlocked ? Color.Status.warning : Color.Status.warning.opacity(Design.Opacity.medium))
                }
            }
            .padding(Design.Spacing.xSmall)
            .background(
                RoundedRectangle(cornerRadius: Design.CornerRadius.small)
                    .fill(isSelected ? Color.Accent.primary.opacity(Design.Opacity.subtle) : Color.clear)
            )
        }
        .buttonStyle(.plain)
        .accessibilityLabel(preset.name)
        .accessibilityAddTraits(isSelected ? .isSelected : [])
        .accessibilityHint(isLocked ? String(localized: "Locked. Tap to unlock with Pro.") : (preset.isPremium ? String(localized: "Premium color") : ""))
    }
}
