//
//  ServicesView.swift
//  iKeyMon
//
//  Created by tracer on 31.03.25.
//

import SwiftUI


struct ServicesView: View {
    @Binding var server: Server

    var body: some View {
        VStack(alignment: .leading) {
            if let ports = server.info?.ports {
                Table(ports) {
                    
                    TableColumn("Service") { port in
                        Text(port.service)
                    }

                    TableColumn("Status") { port in
                        Text(port.status)
                            .foregroundColor(
                                port.status.lowercased() == "online" ? .green : .red
                            )
                    }
                    
                    TableColumn("Port") { port in
                        Text("\(port.port) \(port.proto.uppercased())")
                    }

                    TableColumn("Protocol") { port in
                        Text("\(port.proto.uppercased())")
                            .monospacedDigit()
                    }

                }
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .padding(0)
            } else {
                Text("No service information available")
                    .foregroundColor(.secondary)
                    .padding()
            }
        }
        .navigationTitle("Service/port monitoring")
    }
}

#Preview {
    struct PreviewWrapper: View {
        @State var previewServer = Server(hostname: "example.com", info: .placeholder)

        var body: some View {
            ServicesView(server: $previewServer)
        }
    }

    return PreviewWrapper()
}
