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

import SwiftUI

struct MeasurementSeriesSampleView: View {
    
    var power: Measurements.Measurement.Point
    var voltage: Measurements.Measurement.Point
    var current: Measurements.Measurement.Point

    @State var showDetail: Bool = false
    
    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            HStack(spacing: 12) {
                Image(systemName: "waveform.path.ecg.rectangle.fill")
                    .foregroundColor(.blue)
                    .frame(width: 36, height: 36)
                    .background(Circle().fill(Color.blue.opacity(0.16)))
                VStack(alignment: .leading, spacing: 3) {
                    Text(voltage.timestamp.format(as: "yyyy-MM-dd HH:mm:ss"))
                        .font(.subheadline.weight(.semibold))
                    Text("Captured sample")
                        .font(.caption)
                        .foregroundColor(.secondary)
                }
                Spacer()
                Button(action: {
                    showDetail.toggle()
                }) {
                    Image(systemName: "chevron.right.circle.fill")
                        .imageScale(.large)
                        .foregroundColor(.secondary)
                        .rotationEffect(.degrees(showDetail ? 90 : 0))
                        .animation(.easeInOut(duration: 0.25), value: showDetail)
                }
                .buttonStyle(.plain)
            }

            if showDetail {
                VStack(spacing: 10) {
                    detailRow(title: "Power", value: "\(power.value.format(fractionDigits: 4)) W")
                    detailRow(title: "Voltage", value: "\(voltage.value.format(fractionDigits: 4)) V")
                    detailRow(title: "Current", value: "\(current.value.format(fractionDigits: 4)) A")
                }
            }
        }
        .padding(16)
        .meterCard(tint: .blue, fillOpacity: 0.14, strokeOpacity: 0.20, cornerRadius: 18)
    }

    private func detailRow(title: String, value: String) -> some View {
        HStack {
            Text(title)
                .foregroundColor(.secondary)
            Spacer()
            Text(value)
                .fontWeight(.semibold)
                .monospacedDigit()
        }
        .font(.footnote)
    }
}
