import ComponentLibrary
import ComposableArchitecture
import SwiftUI

@Reducer
public struct BrandedAlertSingleButton {
    @ObservableState
    public struct State: Equatable {
        var style: SingleButtonAlertStyle = .custom(.empty)
    }

    public enum Action: BindableAction {
        case delegate(Delegate)
        case binding(BindingAction<State>)

        public enum Delegate {
            case tappedButton
            case exitButton
        }
    }

    public init() {}

    public var body: some ReducerOf<Self> {
        BindingReducer()
        Reduce<State, Action> { _, action in
            switch action {
            case .delegate,
                 .binding:
                /* Catch-all */
                return .none
            }
        }
    }
}

public struct BrandedAlertSingleButtonView: View {
    @Bindable var store: StoreOf<BrandedAlertSingleButton>

    public init(store: StoreOf<BrandedAlertSingleButton>) {
        self.store = store
    }

    public var body: some View {
        VStack(spacing: .zero) {
            alertCopyStack
            ctaButton
        }
        .frame(width: 313.0)
        .background {
            Color.SemanticV1.backgroundPrimary
        }
        .environment(\.colorScheme, .light)
        .clipShape(.rect(cornerRadius: 24.0))
        .shadow(radius: 10.0)
    }
}

private extension BrandedAlertSingleButtonView {
    @ViewBuilder
    var alertCopyStack: some View {
        VStack(spacing: .zero) {
            if copyData.showIcon {
                Image.Icon.alert
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 24.0, height: 24.0)
                    .foregroundStyle(Color.SemanticV1.iconBrand)
                    .padding(.top, 20.0)
            }

            Text(copyData.title)
                .typographyV1(.headline4)
                .multilineTextAlignment(.center)
                .lineLimit(3)
                .fixedSize(horizontal: false, vertical: true)
                .foregroundStyle(Color.SemanticV1.textPrimary)
                .padding(.top, copyData.showClose ? (copyData.showIcon ? 10 : 20) : 0)
                .padding(.bottom, 10.0)

            Text(copyData.description)
                .typographyV1(.body3Book.lineHeight(5.0))
                .foregroundStyle(Color.SemanticV1.iconBrand)
                .multilineTextAlignment(.center)
        }
        .overlay(alignment: .topTrailing) {
            if copyData.showClose {
                Button {
                    store.send(.delegate(.exitButton))
                } label: {
                    Image.Icon.close
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 20.0, height: 20.0)
                        .foregroundStyle(Color.SemanticV1.iconBrand)
                }
            }
        }
        .padding(.vertical, 24.0)
        .padding(.horizontal, 16.0)
    }

    @ViewBuilder
    var ctaButton: some View {
        Button {
            store.send(.delegate(.tappedButton))
        } label: {
            ZStack {
                Color.SemanticV1.backgroundPrimary
                Text(copyData.buttonLabel)
                    .typographyV1(.button1)
                    .foregroundStyle(Color.SemanticV1.textPrimary)
            }
        }
        .contentShape(.rect)
        .frame(height: 56.0)
        .background {
            Color.clear
        }
        .padding(.top, 1.0)
        .background {
            Color.black.opacity(0.15)
        }
    }

    var copyData: SingleButtonAlertStyle.TextCopy {
        store.style.textCopy
    }
}
