//
//  ChargerEditorSheetView.swift
//  USB Meter
//

import SwiftUI

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

    let chargedDevice: ChargedDeviceSummary?
    /// When false the view omits its own NavigationView (used as a push destination).
    let standalone: Bool

    @State private var name: String
    @State private var chargerType: ChargerType
    @State private var notes: String

    init(
        chargedDevice: ChargedDeviceSummary? = nil,
        standalone: Bool = true
    ) {
        self.chargedDevice = chargedDevice
        self.standalone = standalone
        _name = State(initialValue: chargedDevice?.name ?? "")
        _chargerType = State(initialValue: chargedDevice?.chargerType ?? .genericQi)
        _notes = State(initialValue: chargedDevice?.notes ?? "")
    }

    var body: some View {
        ChargedDeviceEditorScaffoldView(
            title: editorTitle,
            saveButtonTitle: saveButtonTitle,
            canSave: canSave,
            standalone: standalone,
            save: save
        ) {
            Section(header: Text("Identity")) {
                TextField("Charger name", text: $name)

                if let chargedDevice {
                    Text(chargedDevice.qrIdentifier)
                        .font(.caption.monospaced())
                        .foregroundColor(.secondary)
                        .textSelection(.enabled)
                }
            }

            Section(
                header: ContextInfoHeader(
                    title: "Charger Type",
                    message: "MagSafe and Watch chargers use magnetic alignment, enabling accurate efficiency calibration. Standby current and efficiency are learned automatically from sessions."
                )
            ) {
                Picker("Type", selection: $chargerType) {
                    ForEach(ChargerType.allCases) { type in
                        Label(type.title, systemImage: type.symbolName)
                            .tag(type)
                    }
                }
                .pickerStyle(.menu)
            }

            Section(header: Text("Notes")) {
                TextField("Optional notes", text: $notes)
            }
        }
    }

    private var editorTitle: String {
        chargedDevice == nil ? "New Charger" : "Edit Charger"
    }

    private var saveButtonTitle: String {
        chargedDevice == nil ? "Save" : "Update"
    }

    private var canSave: Bool {
        !name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
    }

    private func save() {
        let trimmedNotes = notes.trimmingCharacters(in: .whitespacesAndNewlines)
        let notesValue: String? = trimmedNotes.isEmpty ? nil : trimmedNotes

        let didSave: Bool
        if let chargedDevice {
            didSave = appData.updateCharger(
                id: chargedDevice.id,
                name: name,
                chargerType: chargerType,
                notes: notesValue
            )
        } else {
            didSave = appData.createCharger(
                name: name,
                chargerType: chargerType,
                notes: notesValue
            )
        }

        if didSave {
            dismiss()
        }
    }
}
