//
//  ChargedDeviceLibrarySheetView.swift
//  USB Meter
//
//  Created by Codex on 10/04/2026.
//

import SwiftUI

enum ChargedDeviceLibraryMode {
    case device
    case charger

    var kind: ChargedDeviceKind {
        switch self {
        case .device:
            return .device
        case .charger:
            return .charger
        }
    }

    var title: String {
        kind.pluralTitle
    }

    var singularTitle: String {
        kind.title
    }
}

struct ChargedDeviceLibrarySheetView: View {
    @EnvironmentObject private var appData: AppData
    @Environment(\.dismiss) private var dismiss

    let meterTint: Color
    let mode: ChargedDeviceLibraryMode
    /// true = standalone sheet with own NavigationView; false = pushed into parent nav stack
    let standalone: Bool

    @State private var showingNewEditor = false
    @State private var editingChargedDevice: ChargedDeviceSummary?
    @State private var pendingDeletion: ChargedDeviceSummary?

    init(
        meterTint: Color,
        mode: ChargedDeviceLibraryMode,
        standalone: Bool = true
    ) {
        self.meterTint = meterTint
        self.mode = mode
        self.standalone = standalone
    }

    var body: some View {
        if standalone {
            NavigationView { listContent }
                .navigationViewStyle(StackNavigationViewStyle())
        } else {
            listContent
        }
    }

    private var listContent: some View {
        List {
            if displayedChargedDevices.isEmpty {
                VStack(alignment: .leading, spacing: 10) {
                    HStack(spacing: 8) {
                        Text("No \(mode.title.lowercased()) yet.")
                            .font(.headline)
                        ContextInfoButton(
                            title: mode.title,
                            message: emptyStateDescription
                        )
                    }
                }
                .padding(.vertical, 10)
                .listRowBackground(Color.clear)
            } else {
                ForEach(displayedChargedDevices) { chargedDevice in
                    NavigationLink(destination: ChargedDeviceSettingsView(chargedDeviceID: chargedDevice.id)) {
                        ChargedDeviceLibraryRowView(
                            chargedDevice: chargedDevice,
                            isSelected: false
                        )
                    }
                    .swipeActions(edge: .trailing, allowsFullSwipe: false) {
                        Button(role: .destructive) {
                            pendingDeletion = chargedDevice
                        } label: {
                            Label("Delete", systemImage: "trash")
                        }
                        Button {
                            editingChargedDevice = chargedDevice
                        } label: {
                            Label("Edit", systemImage: "pencil")
                        }
                        .tint(.blue)
                    }
                    .contextMenu {
                        Button {
                            editingChargedDevice = chargedDevice
                        } label: {
                            Label("Edit \(mode.singularTitle)", systemImage: "pencil")
                        }
                        Button(role: .destructive) {
                            pendingDeletion = chargedDevice
                        } label: {
                            Label("Delete \(mode.singularTitle)", systemImage: "trash")
                        }
                    }
                }
            }
        }
        .listStyle(InsetGroupedListStyle())
        .background(
            LinearGradient(
                colors: [meterTint.opacity(0.14), Color.clear],
                startPoint: .topLeading,
                endPoint: .bottomTrailing
            )
            .ignoresSafeArea()
        )
        .navigationTitle(mode.title)
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .cancellationAction) {
                if standalone {
                    Button("Done") { dismiss() }
                }
            }
            ToolbarItem(placement: .confirmationAction) {
                Button("New") { showingNewEditor = true }
            }
        }
        .sheet(isPresented: $showingNewEditor) {
            newEditorSheet
        }
        .sheet(item: $editingChargedDevice) { device in
            editEditorSheet(device)
        }
        .confirmationDialog(
            "Delete \(pendingDeletion?.name ?? mode.singularTitle)?",
            isPresented: Binding(
                get: { pendingDeletion != nil },
                set: { if !$0 { pendingDeletion = nil } }
            ),
            titleVisibility: .visible
        ) {
            Button("Delete", role: .destructive) {
                if let device = pendingDeletion {
                    _ = appData.deleteChargedDevice(id: device.id)
                    pendingDeletion = nil
                }
            }
            Button("Cancel", role: .cancel) { pendingDeletion = nil }
        } message: {
            Text("This will permanently remove the \(mode.singularTitle.lowercased()) and all associated data.")
        }
    }

    @ViewBuilder
    private var newEditorSheet: some View {
        if mode == .charger {
            ChargerEditorSheetView()
                .environmentObject(appData)
        } else {
            ChargedDeviceEditorSheetView()
                .environmentObject(appData)
        }
    }

    @ViewBuilder
    private func editEditorSheet(_ chargedDevice: ChargedDeviceSummary) -> some View {
        if chargedDevice.isCharger {
            ChargerEditorSheetView(chargedDevice: chargedDevice)
                .environmentObject(appData)
        } else {
            ChargedDeviceEditorSheetView(chargedDevice: chargedDevice)
            .environmentObject(appData)
        }
    }

    private var displayedChargedDevices: [ChargedDeviceSummary] {
        switch mode {
        case .device:
            return appData.deviceSummaries
        case .charger:
            return appData.chargerSummaries
        }
    }

    private var emptyStateDescription: String {
        switch mode {
        case .device:
            return "Create one here, then select it explicitly when starting a charging session."
        case .charger:
            return "Create one here, then select it explicitly for wireless charging sessions or standby measurements."
        }
    }
}
