import SwiftUI
import Bedrock
import SwiftData

struct WidgetsView: View {
    @Environment(AppState.self) private var appState

    var body: some View {
        NavigationStack {
            ScrollView {
                VStack(spacing: Design.Spacing.large) {
                    Text("Share using widgets on your phone or watch")
                        .typography(.title2)
                        .bold()
                        .foregroundStyle(Color.Text.primary)

                    if let card = appState.cardStore.selectedCard {
                        WidgetPreviewCardView(card: card)
                    }
                }
                .padding(.horizontal, Design.Spacing.large)
                .padding(.vertical, Design.Spacing.xLarge)
            }
            .background(Color.AppBackground.base)
            .navigationTitle(String.localized("Widgets"))
        }
    }
}

private struct WidgetPreviewCardView: View {
    let card: BusinessCard

    var body: some View {
        VStack(spacing: Design.Spacing.large) {
            PhoneWidgetPreview(card: card)
            WatchWidgetPreview(card: card)
        }
    }
}

private struct PhoneWidgetPreview: View {
    let card: BusinessCard

    var body: some View {
        VStack(alignment: .leading, spacing: Design.Spacing.medium) {
            Text("Phone Widget")
                .typography(.heading)
                .bold()
                .foregroundStyle(Color.Text.primary)

            HStack(spacing: Design.Spacing.medium) {
                QRCodeView(payload: card.vCardPayload)
                    .frame(width: Design.CardSize.widgetPhoneHeight, height: Design.CardSize.widgetPhoneHeight)

                VStack(alignment: .leading, spacing: Design.Spacing.xSmall) {
                    Text(card.fullName)
                        .typography(.heading)
                        .foregroundStyle(Color.Text.primary)
                    Text(card.role)
                        .typography(.subheading)
                        .foregroundStyle(Color.Text.secondary)
                    Text("Tap to share")
                        .typography(.caption)
                        .foregroundStyle(Color.Text.secondary)
                }
            }
        }
        .padding(Design.Spacing.large)
        .background(Color.AppBackground.elevated)
        .clipShape(.rect(cornerRadius: Design.CornerRadius.large))
    }
}

private struct WatchWidgetPreview: View {
    let card: BusinessCard

    var body: some View {
        VStack(alignment: .leading, spacing: Design.Spacing.medium) {
            Text("Watch Widget")
                .typography(.heading)
                .bold()
                .foregroundStyle(Color.Text.primary)

            HStack(spacing: Design.Spacing.medium) {
                QRCodeView(payload: card.vCardPayload)
                    .frame(width: Design.CardSize.widgetWatchSize, height: Design.CardSize.widgetWatchSize)

                VStack(alignment: .leading, spacing: Design.Spacing.xSmall) {
                    Text("Ready to scan")
                        .typography(.subheading)
                        .foregroundStyle(Color.Text.secondary)
                    Text("Open on Apple Watch")
                        .typography(.caption)
                        .foregroundStyle(Color.Text.secondary)
                }
            }
        }
        .padding(Design.Spacing.large)
        .background(Color.AppBackground.elevated)
        .clipShape(.rect(cornerRadius: Design.CornerRadius.large))
    }
}

#Preview {
    WidgetsView()
        .environment(AppState(modelContext: try! ModelContainer(for: BusinessCard.self, Contact.self).mainContext))
}
