import SwiftUI

struct ContentView: View {
    @StateObject private var model = WebViewModel()

    var body: some View {
        ZStack {
            Color.white.ignoresSafeArea()
            WebViewContainer(model: model)
                .ignoresSafeArea(edges: .bottom)

            if model.isLoading {
                VStack {
                    ProgressView()
                        .tint(Color(red: 0.09, green: 0.47, blue: 1.00))
                    Spacer()
                }
                .padding(.top, 14)
                .allowsHitTesting(false)
            }

            if let message = model.errorMessage {
                ErrorView(message: message, retry: model.reload)
                    .transition(.opacity)
            }
        }
        .preferredColorScheme(.light)
    }
}

private struct ErrorView: View {
    let message: String
    let retry: () -> Void

    var body: some View {
        VStack(spacing: 18) {
            Text("!")
                .font(.system(size: 32, weight: .bold))
                .foregroundStyle(.white)
                .frame(width: 66, height: 66)
                .background(Color(red: 0.09, green: 0.47, blue: 1.00))
                .clipShape(Circle())
            Text("页面暂时打不开")
                .font(.title3.bold())
            Text(message)
                .font(.subheadline)
                .foregroundStyle(.secondary)
                .multilineTextAlignment(.center)
                .lineSpacing(5)
            Button("重新加载", action: retry)
                .font(.headline)
                .foregroundStyle(.white)
                .padding(.horizontal, 42)
                .padding(.vertical, 14)
                .background(Color(red: 0.09, green: 0.47, blue: 1.00))
                .clipShape(Capsule())
        }
        .padding(34)
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.white)
    }
}
