import ComponentLibrary
import ComposableArchitecture
import SwiftUI

@Reducer
public struct BrandedAlertFeaturedArtist {
    @ObservableState
    public struct State: Equatable {
        var style: FeaturedArtistAlertStyle = .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 BrandedAlertFeaturedArtistView: View {
    @Bindable var store: StoreOf<BrandedAlertFeaturedArtist>

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

    public var body: some View {
        VStack(spacing: .zero) {
            Spacer()
            ctaButton
        }
        .frame(width: 313.0, height: 590.0)
        .overlay(alignment: .topTrailing) {
            Button {
                store.send(.delegate(.exitButton))
            } label: {
                ZStack {
                    Circle()
                        .fill(Color.SemanticV1.backgroundBlockOverlay.opacity(0.25))
                        .frame(width: 40, height: 40)
                    Image.Icon.close
                        .foregroundStyle(Color.SemanticV1.iconOnDark)
                }
                .padding(.top, 12)
                .padding(.trailing, 12)
            }
        }
        .background {
            ZStack {
                Color.SemanticV1.backgroundPrimary
                copyData.backgroundImage
            }
            .environment(\.colorScheme, .dark)
        }
        .environment(\.colorScheme, .light)
        .clipShape(.rect(cornerRadius: 24.0))
        .shadow(radius: 10.0)
    }
}

private extension BrandedAlertFeaturedArtistView {
    @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: FeaturedArtistAlertStyle.TextCopy {
        store.style.textCopy
    }
}
