//
//  ChargeRecordSheetView.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 09/03/2020.
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
//

import SwiftUI

struct ChargeRecordSheetView: View {
    @Binding var visibility: Bool
    @EnvironmentObject private var appData: AppData

    @State private var chargedDeviceLibraryVisibility = false
    @State private var chargerLibraryVisibility = false
    @State private var deviceLibraryMACAddress = ""
    @State private var chargerLibraryMACAddress = ""
    @State private var chargedDeviceLibraryTint: Color = .orange
    @State private var chargerLibraryTint: Color = .pink

    var body: some View {
        NavigationView {
            MeterChargeRecordContentView(
                onSelectDevice: { mac, tint in
                    deviceLibraryMACAddress = mac
                    chargedDeviceLibraryTint = tint
                    chargedDeviceLibraryVisibility = true
                },
                onSelectCharger: { mac, tint in
                    chargerLibraryMACAddress = mac
                    chargerLibraryTint = tint
                    chargerLibraryVisibility = true
                }
            )
            .navigationTitle("Charge Record")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button("Done") {
                        visibility = false
                    }
                }
            }
            .background(
                Group {
                    NavigationLink(isActive: $chargedDeviceLibraryVisibility) {
                        ChargedDeviceLibrarySheetView(
                            meterMACAddress: deviceLibraryMACAddress,
                            meterTint: chargedDeviceLibraryTint,
                            mode: .device,
                            standalone: false
                        )
                        .environmentObject(appData)
                    } label: { EmptyView() }

                    NavigationLink(isActive: $chargerLibraryVisibility) {
                        ChargedDeviceLibrarySheetView(
                            meterMACAddress: chargerLibraryMACAddress,
                            meterTint: chargerLibraryTint,
                            mode: .charger,
                            standalone: false
                        )
                        .environmentObject(appData)
                    } label: { EmptyView() }
                }
            )
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

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

    let sessionID: UUID
    let initialTargetPercent: Double?

    @State private var targetPercent: Double

    init(sessionID: UUID, initialTargetPercent: Double?) {
        self.sessionID = sessionID
        self.initialTargetPercent = initialTargetPercent
        _targetPercent = State(initialValue: initialTargetPercent ?? 80)
    }

    var body: some View {
        NavigationView {
            Form {
                Section(
                    header: ContextInfoHeader(
                        title: "Target Level",
                        message: "A local notification will be generated on synced devices when the estimated battery level reaches this target."
                    )
                ) {
                    VStack(alignment: .leading, spacing: 12) {
                        Text("\(targetPercent.format(decimalDigits: 0))%")
                            .font(.title3.weight(.bold))
                        Slider(value: $targetPercent, in: 20...100, step: 1)
                    }
                }
            }
            .navigationTitle("Battery Target")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button("Cancel") {
                        dismiss()
                    }
                }

                ToolbarItem(placement: .confirmationAction) {
                    Button("Save") {
                        if appData.setTargetBatteryPercent(targetPercent, for: sessionID) {
                            dismiss()
                        }
                    }
                }
            }
        }
    }
}

#Preview {
    ChargeRecordSheetView(visibility: .constant(true))
}
