import Foundation
import SwiftUI

public struct CTABanner: View {
    let title: String
    let subtitle: String
    let icon: Image
    let background: Image
    let action: () -> Void
    let dismissAction: () -> Void

    public init(
        title: String,
        subtitle: String,
        @ViewBuilder icon: () -> Image,
        @ViewBuilder background: () -> Image,
        action: @escaping () -> Void,
        dismissAction: @escaping () -> Void
    ) {
        self.title = title
        self.subtitle = subtitle
        self.icon = icon()
        self.background = background()
        self.action = action
        self.dismissAction = dismissAction
    }

    public var body: some View {
        ZStack(alignment: .topTrailing) {
            Button {
                action()
            } label: {
                HStack(spacing: 12) {
                    Circle()
                        .fill(Color.SemanticV1.backgroundPrimary.opacity(0.25))
                        .frame(width: 44, height: 44)
                        .overlay {
                            icon
                                .foregroundStyle(Color.SemanticV1.textPrimary)
                                .frame(width: 24, height: 24)
                        }

                    VStack(alignment: .leading, spacing: 4) {
                        Text(title)
                            .typographyV1(.headline5)
                            .foregroundStyle(Color.SemanticV1.textPrimary)

                        Text(subtitle)
                            .typographyV1(.caption2)
                            .foregroundStyle(Color.SemanticV1.textPrimary)
                    }

                    Spacer()
                }
                .padding(.horizontal, 16)
                .padding(.vertical, 22)
                .background {
                    background
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                }
                .contentShape(.rect)
                .mask {
                    RoundedRectangle(cornerRadius: 8)
                }
            }
            .buttonStyle(ScaleButtonStyle(scaleAmount: 0.98))

            Button(action: dismissAction) {
                Image.Icon.close
                    .foregroundStyle(Color.SemanticV1.iconPrimary)
            }
            .frame(width: 24, height: 24)
            .padding(8)
            .contentShape(.rect)
            .buttonStyle(BorderlessButtonStyle())
        }
        .environment(\.colorScheme, .dark)
    }
}
