//
//  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

    var body: some View {
        NavigationView {
            MeterChargeRecordContentView()
            .navigationTitle("Charge Record")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button("Done") {
                        visibility = false
                    }
                }
            }
        }
        .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))
}
