import SwiftUI

enum LayoutMode {
    case fixedRow
    case containerAware

    var label: String {
        switch self {
        case .fixedRow: "Fixed row"
        case .containerAware: "Container-aware"
        }
    }
}

@main
struct LayoutEvidenceDemo: App {
    private let mode: LayoutMode = CommandLine.arguments.contains("--fixed") ? .containerAware : .fixedRow

    var body: some Scene {
        WindowGroup {
            LayoutScreen(mode: mode)
                .dynamicTypeSize(.accessibility3)
                .preferredColorScheme(.light)
        }
    }
}

struct LayoutScreen: View {
    let mode: LayoutMode
    @State private var continued = false

    private var orderTitle: some View {
        Text("Review your delivery preferences")
            .font(.title3.weight(.semibold))
            .fixedSize(horizontal: false, vertical: true)
    }

    private var continueButton: some View {
        Button {
            continued = true
        } label: {
            Text(continued ? "Done" : "Continue")
                .font(.headline)
                .frame(maxWidth: .infinity, alignment: .leading)
                .padding(.horizontal, 18)
                .padding(.vertical, 16)
                .foregroundStyle(.white)
                .background(.blue, in: RoundedRectangle(cornerRadius: 14))
        }
        .accessibilityIdentifier("continueButton")
    }

    var body: some View {
        NavigationStack {
            ScrollView {
                VStack(alignment: .leading, spacing: 24) {
                    Text(mode.label)
                        .font(.subheadline.weight(.semibold))
                        .foregroundStyle(.secondary)
                    Text("Orders")
                        .font(.largeTitle.bold())
                    Group {
                        switch mode {
                        case .fixedRow:
                            GeometryReader { geometry in
                                HStack(alignment: .center, spacing: 16) {
                                    orderTitle.frame(width: 250, alignment: .leading)
                                    continueButton.frame(width: 230)
                                    Spacer(minLength: 0)
                                }
                                .padding(20)
                                .frame(width: 640, height: 320, alignment: .leading)
                                .background(.white)
                                .frame(width: geometry.size.width, alignment: .leading)
                                .clipped()
                            }
                            .frame(height: 320)
                        case .containerAware:
                            ViewThatFits(in: .horizontal) {
                                HStack(spacing: 16) {
                                    orderTitle.fixedSize()
                                    continueButton.fixedSize()
                                }
                                VStack(alignment: .leading, spacing: 24) {
                                    orderTitle
                                    continueButton
                                }
                            }
                            .padding(20)
                            .frame(maxWidth: .infinity, alignment: .leading)
                            .background(.white)
                        }
                    }
                    .clipShape(RoundedRectangle(cornerRadius: 20))
                    Text("Accessibility text size")
                        .font(.caption)
                        .foregroundStyle(.secondary)
                }
                .padding(20)
            }
            .background(Color(uiColor: .systemGroupedBackground))
            .navigationTitle("Layout test")
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}
