import SwiftUI
import VNCUI

struct RootView: View {
    @Environment(AppStateController.self) private var appState

    var body: some View {
        ZStack {
            switch appState.state {
            case .launching:
                LaunchView()
                    .transition(.opacity)
            case .list:
                ConnectionListView()
                    .transition(.opacity)
            case .error(let error):
                ErrorView(error: error)
                    .transition(.opacity)
            }
        }
        .animation(.easeInOut(duration: 0.2), value: appState.state)
    }
}

private struct LaunchView: View {
    var body: some View {
        VStack(spacing: 16) {
            Image(systemName: "display")
                .font(.system(size: 48, weight: .semibold))
                .foregroundStyle(.tint)
            ProgressView()
        }
    }
}

private struct ErrorView: View {
    let error: AppError
    var body: some View {
        ContentUnavailableView(
            "Something went wrong",
            systemImage: "exclamationmark.triangle",
            description: Text(String(describing: error))
        )
    }
}
