import SwiftUI
import Network

struct ConnectionSheet: View {
    var remoteClient: RemoteClient
    @Binding var isPresented: Bool

    var body: some View {
        VStack(spacing: 16) {
            Text("Connect to Host")
                .font(.headline)

            if let message = remoteClient.connectionState.userMessage {
                HStack(spacing: 8) {
                    if !remoteClient.connectionState.isConnected &&
                        remoteClient.connectionState != .disconnected {
                        ProgressView().controlSize(.small)
                    }
                    Text(message)
                        .font(.subheadline)
                        .foregroundStyle(.secondary)
                }
            }

            if case .connectionLost(let reason) = remoteClient.connectionState {
                VStack(spacing: 8) {
                    Text(reason).font(.caption).foregroundStyle(.red)
                    Button("Retry") { remoteClient.startDiscovery() }
                }
            }

            if remoteClient.discoveredHosts.isEmpty && remoteClient.connectionState == .discovering {
                VStack(spacing: 8) {
                    ProgressView()
                    Text("Looking for hosts on your network...")
                        .font(.caption).foregroundStyle(.secondary)
                }
                .frame(height: 100)
            } else {
                List(remoteClient.discoveredHosts, id: \.name) { host in
                    HStack {
                        Image(systemName: "desktopcomputer").foregroundStyle(.secondary)
                        Text(host.name)
                        Spacer()
                        Button("Connect") { remoteClient.connect(to: host) }
                            .buttonStyle(.borderedProminent).controlSize(.small)
                    }
                    .padding(.vertical, 4)
                }
                .frame(minHeight: 100, maxHeight: 200)
            }

            Button("Cancel") {
                remoteClient.stopDiscovery()
                isPresented = false
            }
            .keyboardShortcut(.cancelAction)
        }
        .padding(20)
        .frame(width: 380)
        .onChange(of: remoteClient.connectionState) { _, newState in
            if case .connected = newState { isPresented = false }
        }
    }
}
