import APIClient
import ComponentLibrary
import ComposableArchitecture
import FeatureSocial
import Localization
import SwiftUI

@Reducer
public struct ContactsAuthorization {
    @Reducer(state: .equatable)
    public enum Destination {
        case contactsAuthorizationAlert(ContactsAuthorizationAlert)
    }

    @ObservableState
    public struct State: Equatable {
        public var me: Me
        @Presents public var destination: Destination.State?

        public init(me: Me) {
            self.me = me
        }
    }

    public enum Action {
        case onAppear
        case showContactsAuthorizationAlert
        case destination(PresentationAction<Destination.Action>)
        case delegate(Delegate)

        public enum Delegate {
            case onAppear
            case authorized(Me)
            case denied(Me)
        }
    }

    public var body: some ReducerOf<Self> {
        Reduce { state, action in
            switch action {
            case .onAppear:
                return .merge(
                    .send(.delegate(.onAppear)),
                    .run { send in
                        try await Task.sleep(for: .seconds(0.5))
                        await send(.showContactsAuthorizationAlert)
                    }
                )

            case .showContactsAuthorizationAlert:
                state.destination = .contactsAuthorizationAlert(ContactsAuthorizationAlert.State())
                return .none

            case .destination(.presented(.contactsAuthorizationAlert(.delegate(.authorized)))):
                return .send(.delegate(.authorized(state.me)))

            case .destination(.presented(.contactsAuthorizationAlert(.delegate(.denied)))):
                return .send(.delegate(.denied(state.me)))

            case .destination:
                return .none

            case .delegate:
                // Catch-all
                return .none
            }
        }
        .ifLet(\.$destination, action: \.destination)

        Analytics()
    }
}

public struct ContactsAuthorizationScreen: View {
    @Bindable var store: StoreOf<ContactsAuthorization>

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

    public var body: some View {
        VStack {
            Spacer()

            Text(L10n.FeatureSocial.contactSyncAuthorizationHeadline)
                .typographyV1(.headline1.editorialNewLight())
                .foregroundStyle(Color.SemanticV1.textPrimary)
                .lineLimit(1)

            Spacer()
            Spacer()

            Text(L10n.FeatureSocial.contactSyncAuthorizationFooter)
                .typographyV1(.headline3.neueMontrealMedium())
                .foregroundStyle(Color.SemanticV1.textSecondary)
                .multilineTextAlignment(.center)
                .padding(.horizontal, 44)

            Spacer()
        }
        .contactsAuthorizationAlert(
            $store.scope(
                state: \.destination?.contactsAuthorizationAlert,
                action: \.destination.contactsAuthorizationAlert
            )
        )
        .navigationBarBackButtonHidden(true)
        .onAppear {
            store.send(.onAppear)
        }
    }
}
