//
//  MeasurementView.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 13/04/2020.
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
//

import SwiftUI

struct MeasurementsView: View {
    
    @EnvironmentObject private var measurements: Measurements
    
    @Binding var visibility: Bool
    
    var body: some View {
        NavigationView {
            VStack {
                List {
                    ForEach (measurements.power.points) { point in
                        // MARK: Crapa la stergere daca lista incape in fereastra:  Fatal error: Index out of range
                        MeasurementPointView(power: point, voltage: self.measurements.voltage.points[point.id], current: self.measurements.current.points[point.id])
                    }.onDelete { (indexSet) in
                        for idx in indexSet {
                            self.measurements.remove(at: idx)
                        }
                    }
                }
            }
            .navigationBarItems(leading: HStack{
                    #if targetEnvironment(macCatalyst)
                    Button(action: {self.visibility.toggle()}) {
                        Text("ⓧ")
                            .foregroundColor(.red)
                    }
                    #else
                    Spacer()
                    #endif
                },
                trailing: HStack{
                    #if targetEnvironment(macCatalyst)
                    EditButton()
                    #endif
                    Button(action: {
                        self.measurements.power.reset()
                        self.measurements.voltage.reset()
                        self.measurements.current.reset()
                    }) {
                        Text("🗑")
                            .foregroundColor(.red)
                    }
                })
                .navigationBarTitle("Measurements", displayMode: .inline)
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}
