import APIClient
import ComponentLibrary
import ComposableArchitecture
import FeatureOnboardingModels
import Foundation
import Localization
import SwiftUI

public struct BirthdayEntryScreen: View {
    @Bindable var store: StoreOf<BirthdayEntry>

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

    public var body: some View {
        DataEntryScreen(
            title: L10n.FeatureOnboarding.birthdayTitle,
            subtitle: L10n.FeatureOnboarding.birthdaySubtitle,
            buttonTitle: L10n.FeatureOnboarding.next,
            placeholder: L10n.FeatureOnboarding.birthdayPlaceholder,
            autofillContentType: .birthdate,
            isWorking: store.isWorking,
            value: $store.formattedValue,
            focused: $store.isFocused.sending(\.setFocus),
            action: { store.send(.updateBirthdayTapped) },
            cancel: { store.send(.delegate(.backTapped)) },
            showSkip: true,
            showBackButton: store.showBackButton,
            showClearButton: store.showClearButton,
            isDisabled: !store.isValidBirthday
        )
        .alert($store.scope(state: \.alert, action: \.alert))
        .keyboardType(.numberPad)
        .textContentType(.birthdate)
        .birthdayBackspaceHandling(formattedValue: $store.formattedValue) { value in
            store.send(.formatDate(value))
        }
        .onAppear { store.send(.onAppear) }
    }
}

#if DEBUG
    #Preview {
        NavigationView {
            BirthdayEntryScreen(store: Store(initialState: BirthdayEntry.State(isWorking: false, me: Me(models: [], roles: [:], flags: [:], user: User.mock()), initialValue: "", showBackButton: true, showClearButton: true), reducer: { BirthdayEntry() }))
        }
    }
#endif
