import SwiftUI
import UIKit
import Bedrock

/// A form view for editing a structured postal address
struct AddressEditorView: View {
    @Binding var address: PostalAddress
    
    var body: some View {
        VStack(alignment: .leading, spacing: Design.Spacing.medium) {
            AddressTextField(
                label: String(localized: "Street"),
                placeholder: String(localized: "123 Main St"),
                text: $address.street,
                textContentType: .streetAddressLine1
            )
            
            AddressTextField(
                label: String(localized: "Street 2"),
                placeholder: String(localized: "Apt, Suite, Unit (optional)"),
                text: $address.street2,
                textContentType: .streetAddressLine2
            )
            
            AddressTextField(
                label: String(localized: "City"),
                placeholder: String(localized: "City"),
                text: $address.city,
                textContentType: .addressCity
            )
            
            HStack(spacing: Design.Spacing.medium) {
                AddressTextField(
                    label: String(localized: "State"),
                    placeholder: String(localized: "State"),
                    text: $address.state,
                    textContentType: .addressState
                )
                
                AddressTextField(
                    label: String(localized: "ZIP Code"),
                    placeholder: String(localized: "12345"),
                    text: $address.postalCode,
                    textContentType: .postalCode,
                    keyboardType: .numbersAndPunctuation
                )
            }
            
            AddressTextField(
                label: String(localized: "Country"),
                placeholder: String(localized: "Country (optional)"),
                text: $address.country,
                textContentType: .countryName
            )
        }
    }
}

// MARK: - Address Text Field

private struct AddressTextField: View {
    let label: String
    let placeholder: String
    @Binding var text: String
    var textContentType: UITextContentType?
    var keyboardType: UIKeyboardType = .default
    
    var body: some View {
        VStack(alignment: .leading, spacing: Design.Spacing.xSmall) {
            Text(label)
                .font(.caption)
                .foregroundStyle(Color.Text.secondary)
            
            TextField(placeholder, text: $text)
                .textContentType(textContentType)
                .keyboardType(keyboardType)
                .textInputAutocapitalization(.words)
            
            Divider()
        }
    }
}

// MARK: - Preview

#Preview {
    @Previewable @State var address = PostalAddress(
        street: "6565 Headquarters Dr",
        city: "Plano",
        state: "TX",
        postalCode: "75024"
    )
    
    Form {
        Section("Address") {
            AddressEditorView(address: $address)
        }
        
        Section("Preview") {
            Text(address.formattedString)
                .font(.subheadline)
        }
    }
}
