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

import SwiftUI

struct MeasurementSeriesSheetView: View {
    
    @EnvironmentObject private var measurements: Measurements
    
    @Binding var visibility: Bool
    
    var body: some View {
        let seriesPoints = measurements.power.samplePoints

        NavigationView {
            ScrollView {
                VStack(alignment: .leading, spacing: 14) {
                    VStack(alignment: .leading, spacing: 8) {
                        HStack {
                            Text("Measurement Series")
                                .font(.system(.title3, design: .rounded).weight(.bold))
                            ContextInfoButton(
                                title: "Measurement Series",
                                message: "Buffered measurement series captured from the meter for analysis, charts, and correlations."
                            )
                            Spacer(minLength: 0)
                        }
                    }
                    .padding(18)
                    .meterCard(tint: .blue, fillOpacity: 0.18, strokeOpacity: 0.24)

                    if seriesPoints.isEmpty {
                        Text("No measurement samples have been captured yet.")
                            .font(.footnote)
                            .foregroundColor(.secondary)
                            .padding(18)
                            .meterCard(tint: .secondary, fillOpacity: 0.14, strokeOpacity: 0.20)
                    } else {
                        LazyVStack(spacing: 12) {
                            ForEach(seriesPoints) { point in
                                MeasurementSeriesSampleView(
                                    power: point,
                                    voltage: measurements.voltage.points[point.id],
                                    current: measurements.current.points[point.id],
                                    energy: energyPoint(for: point.timestamp)
                                )
                            }
                        }
                    }
                }
                .padding()
            }
            .background(
                LinearGradient(
                    colors: [.blue.opacity(0.14), Color.clear],
                    startPoint: .topLeading,
                    endPoint: .bottomTrailing
                )
                .ignoresSafeArea()
            )
            .navigationBarItems(
                leading: Button("Done") { visibility.toggle() },
                trailing: Button("Reset Series") {
                    measurements.resetSeries()
                }
                .foregroundColor(.red)
            )
            .navigationBarTitle("Measurement Series", displayMode: .inline)
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }

    private func energyPoint(for timestamp: Date) -> Measurements.Measurement.Point? {
        measurements.energy.samplePoints.last { $0.timestamp == timestamp }
    }
}
