import ComponentLibrary
import SwiftUI

public struct BlendedCreateAuraButton<LeadingView: View>: View {
    private let title: String
    private let isLoading: Bool
    private let auraStyle: ButtonAuraBackground.AuraStyle
    private let leadingView: LeadingView
    private let action: () -> Void

    @Environment(\.pillButtonSizeV1) private var size
    @Environment(\.isEnabled) private var isEnabled

    public init(
        title: String = "",
        isLoading: Bool = false,
        auraStyle: ButtonAuraBackground.AuraStyle,
        @ViewBuilder leadingView: () -> LeadingView,
        action: @escaping () -> Void
    ) {
        self.title = title
        self.isLoading = isLoading
        self.auraStyle = auraStyle
        self.leadingView = leadingView()
        self.action = action
    }

    public var body: some View {
        Button {
            action()
        } label: {
            HStack {
                leadingView // need a solution here to allow .resizable and .aspectRatio(contentMode: .fit)
                    .frame(width: size.iconWidth, height: size.iconWidth)

                Text(title)
                    .typographyV1(.heading3)
                    .foregroundStyle(Color.SemanticV2.foregroundPrimaryOnDark)
                    .accessibilityLabel(title)
            }
        }
        .disabled(isLoading)
        .buttonStyle(
            PillButtonStyleV1(
                isLoading: isLoading,
                isEnabled: isEnabled,
                isIconOnly: false,
                size: size,
                colorCombination: auraButtonColorCombination
            )
        )
        .background {
            #if targetEnvironment(simulator)
                Color.orange
            #else
                BlendedCreateAuraView()
            #endif
        }
        .shadow(color: .black.opacity(0.2), radius: 3, x: 2, y: 2)
        .shadow(color: .black.opacity(0.1), radius: 8, x: 6, y: 6)
        .clipShape(Capsule())
    }
}

public extension BlendedCreateAuraButton where LeadingView == EmptyView {
    /// Create a primary button without a leading view.
    /// - Parameters:
    ///   - title: Centered title label
    ///   - isLoading: True to display a loading indicator instead of the label.
    ///   - action: Action to be triggered on tap
    init(
        title: String,
        isLoading: Bool = false,
        auraStyle: ButtonAuraBackground.AuraStyle,
        action: @escaping () -> Void
    ) {
        self.init(
            title: title,
            isLoading: isLoading,
            auraStyle: auraStyle,
            leadingView: { EmptyView() },
            action: action
        )
    }
}

let auraButtonColorCombination = PillButtonStyleV1.ColorCombination(
    enabled: PillButtonStyleV1.ColorSet(
        foreground: .SemanticV1.textPrimary,
        background: Color.clear,
        loading: .SemanticV1.iconInvert,
        border: .clear
    ),
    pressed: PillButtonStyleV1.ColorSet(
        foreground: .SemanticV1.textPrimary.opacity(0.5),
        background: .SemanticV1.backgroundPrimary.opacity(0.3),
        loading: .SemanticV1.iconInvert,
        border: .clear
    ),
    disabled: PillButtonStyleV1.ColorSet(
        foreground: .SemanticV1.textPrimary.opacity(0.5),
        background: .SemanticV1.backgroundPrimary.opacity(0.5),
        loading: .SemanticV1.iconInvert,
        border: .clear
    )
)
