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

@Reducer
public struct ContactSync {
    @ObservableState
    public struct State: Equatable {
        var me: Me
        @ObservationStateIgnored @ObservedBox var fromYourContactsState: FromYourContacts.State

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

    public enum Action {
        case onAppear
        case fromYourContactsAction(FromYourContacts.Action)
        case delegate(Delegate)

        public enum Delegate {
            case onAppear
            case doneButtonTapped(Me)
        }
    }

    public init() {}

    public var body: some ReducerOf<Self> {
        Scope(state: \.fromYourContactsState, action: \.fromYourContactsAction) {
            FromYourContacts()
        }
        Reduce { _, action in
            switch action {
            case .onAppear:
                return .send(.delegate(.onAppear))

            case .fromYourContactsAction:
                return .none

            case .delegate:
                return .none
            }
        }
        Analytics()
    }
}

public struct ContactSyncScreen: View {
    public var store: StoreOf<ContactSync>

    public var body: some View {
        FromYourContactsScreen(store: store.scope(state: \.fromYourContactsState, action: \.fromYourContactsAction))
            .safeAreaInset(edge: .bottom) {
                doneButton
            }
            .navigationBarBackButtonHidden(true)
            .onAppear { store.send(.onAppear) }
    }

    private var doneButton: some View {
        PrimaryButtonV1(
            title: L10n.FeatureSocial.done,
            colorCombination: .dark,
            preferredSize: .large,
            action: { store.send(.delegate(.doneButtonTapped(store.state.me))) }
        )
        .padding(12)
        .background(Color.SemanticV1.backgroundPrimary)
    }
}
