import ComponentLibrary
import SwiftUI

struct WaveformView: View {
    @State private var height: CGFloat = 0

    let samples: [TimeInterval: CGFloat]

    var body: some View {
        // divide the view equally, generate waveform from center point
        HStack(spacing: 4) {
            Color.clear
                .overlay(alignment: .trailing) {
                    waveform
                }
            Color.clear
        }
        .onGeometryChange(for: CGSize.self) { proxy in proxy.size } action: {
            height = $0.height
        }
    }

    private var waveform: some View {
        HStack(spacing: 4) {
            ForEach(samples.keys.sorted(by: <), id: \.self) { key in
                let value = samples[key]!
                Rectangle()
                    .fill(Color.SemanticV1.iconLink)
                    .frame(width: 2, height: value * height)
            }
        }
    }
}
