//
//  GeneralTab.swift
//  iKeyMon
//
//  Created by tracer on 30.03.25.
//

import SwiftUI

struct GeneralView: View {
    @Binding var server: Server
    
    var body: some View {
        GeometryReader { geometry in
            ScrollView {
                VStack(alignment: .leading, spacing: 6) {
                    TableRowView {
                        Text("Hostname")
                    } value: {
                        InfoCell(value: [server.hostname])
                    }
                    
                    TableRowView {
                        Text("IP addresses")
                    } value: {
                        InfoCell(value: server.info?.ipAddresses ?? [], monospaced: true)
                    }
                    
                    TableRowView {
                        Text("Server time")
                    } value: {
                        InfoCell(value: [server.info?.formattedServerTime ?? ""], monospaced: true)
                    }

                    TableRowView {
                        Text("Uptime")
                    } value: {
                        InfoCell(value: [server.info?.uptime ?? ""])
                    }

                    TableRowView {
                        Text("KeyHelp version")
                    } value: {
                        InfoCell(value: [server.info?.formattedVersion ?? ""], monospaced: true)
                    }

                    TableRowView {
                        Text("Operating system")
                    } value: {
                        InfoCell(
                            value: {
                                guard let os = server.info?.operatingSystem else { return [] }
                                var rows: [String] = []

                                let distro = [os.distribution, os.version]
                                    .filter { !$0.isEmpty }
                                    .joined(separator: " ")
                                    .trimmingCharacters(in: .whitespacesAndNewlines)
                                var description = os.label.trimmingCharacters(in: .whitespacesAndNewlines)
                                if description.isEmpty {
                                    description = distro
                                } else if !distro.isEmpty && description.range(of: distro, options: [.caseInsensitive]) == nil {
                                    description += " • \(distro)"
                                }
                                if !os.architecture.isEmpty &&
                                    description.range(of: os.architecture, options: [.caseInsensitive]) == nil {
                                    description += " (\(os.architecture))"
                                }
                                if !description.isEmpty {
                                    rows.append(description)
                                }

                                if let updates = os.updates {
                                    var updateDescription = "Updates: \(updates.updateCount)"
                                    if updates.securityUpdateCount > 0 {
                                        updateDescription += " • \(updates.securityUpdateCount) security"
                                    }
                                    rows.append(updateDescription)
                                    if updates.rebootRequired {
                                        rows.append("Reboot required")
                                    }
                                }

                                if os.endOfLife {
                                    rows.append("End-of-life release")
                                }

                                return rows
                            }(),
                            monospaced: true
                        )
                    }

                    TableRowView {
                        Text("Sytem PHP version")
                    } value: {
                        InfoCell(value: [server.info?.phpVersion ?? ""], monospaced: true)
                    }

                    TableRowView(showDivider: false) {
                        Text("Additional PHP interpreters")
                    } value: {
                        InfoCell(
                            value: {
                                let interpreters = server.info?.additionalPHPInterpreters ?? []
                                if interpreters.isEmpty {
                                    return ["None"]
                                }
                                let versions = interpreters
                                    .map { $0.fullVersion }
                                    .filter { !$0.isEmpty }
                                if versions.isEmpty {
                                    return ["None"]
                                }
                                return [versions.joined(separator: " • ")]
                            }(),
                            monospaced: true
                        )
                    }
                }
                .padding()
                .frame(minHeight: geometry.size.height, alignment: .top)
            }
            .padding()
            .scrollDisabled(true)
        }
    }
}

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

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

    return PreviewWrapper()
}
