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

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

import SwiftUI

struct ResourcesView: View {
    @Binding var server: Server

    var body: some View {
        GeometryReader { geometry in
            ScrollView {
                VStack(alignment: .leading, spacing: 6) {
                    TableRowView {
                        Text("CPU Load")
                    } value: {
                        LoadBarCell(
                            percent: (server.info?.load.percent)!,
                            load1: (server.info?.load.minute1)!,
                            load5: (server.info?.load.minute5)!,
                            load15: (server.info?.load.minute15)!
                        )
                    }
                    TableRowView {
                        Text("Memory")
                    } value: {
                        UsageBarCell(
                            free: (server.info?.memory.free)!,
                            used: (server.info?.memory.used)!,
                            total: (server.info?.memory.total)!,
                            percent: (server.info?.memory.percent)!
                        )
                    }
                    TableRowView {
                        Text("Swap")
                    } value: {
                        UsageBarCell(
                            free: (server.info?.swap.free)!,
                            used: (server.info?.swap.used)!,
                            total: (server.info?.swap.total)!,
                            percent: (server.info?.swap.percent)!
                        )
                    }
                    TableRowView {
                        Text("SSD")
                    } value: {
                        UsageBarCell(
                            free: (server.info?.diskSpace.free)!,
                            used: (server.info?.diskSpace.used)!,
                            total: (server.info?.diskSpace.total)!,
                            percent: (server.info?.diskSpace.percent)!
                        )
                    }
                }
                .padding()
                .frame(minHeight: geometry.size.height, alignment: .top)
            }

        }
//        VStack(alignment: .leading, spacing: 16) {
//            if let info = server.info {
//                Text("Server Utilization")
//                    .font(.headline)
//                    .padding(.bottom, 4)
//
//                ResourceRow(label: "CPU Load", value: "\(info.load.percent)", subtext: info.cpuLoadDetail)
//                ResourceRow(label: "Process Count", value: "\(info.processCount)")
////                ResourceRow(label: "Emails in Queue", value: "\(info.emailsInQueue)")
//
//                ResourceBarRow(
//                    label: "Memory",
//                    free: info.memory.free,
//                    used: info.memory.used,
//                    total: info.memory.total,
//                    percent: info.memory.percent
//                )
//
//                ResourceBarRow(
//                    label: "Swap",
//                    free: info.memory.free,
//                    used: info.memory.used,
//                    total: info.memory.total,
//                    percent: info.memory.percent
//                )
//
//                Spacer()
//            } else {
//                Text("No data")
//            }
//        }
        .padding()
        .frame(maxWidth: .infinity, alignment: .leading)
    }
}

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

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

    return PreviewWrapper()
}
