//
//  DataGroupsView.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 10/03/2020.
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
//
//  MARK: GeometryReader: https://stackoverflow.com/questions/57577462/get-width-of-a-view-using-in-swiftui/57591483 Pe iPhone dimensiunea shetului pare mai mare decat cea afisata fara GeometryReader

import SwiftUI

struct DataGroupsView: View {
    
    @Binding var visibility: Bool
    @EnvironmentObject private var usbMeter: Meter
    
    var body: some View {
        GeometryReader { box in
            VStack (spacing: 1) {
                HStack {
                    Text("Data Groups")
                        .bold()
                    Spacer()
                    Button(action: {self.visibility.toggle()}) {
                        Text("ⓧ")
                            .foregroundColor(.red)
                    }
                }
                .font(.title)
                
                Spacer()
                
                HStack (spacing: 1) {
                    ForEach (["Group", "Ah", "Wh", "Clear"], id: \.self ) { text in
                        self.THView( text: text, width: (box.size.width-25)/4 )
                    }
                }
                .frame(height: ( box.size.height - 100 ) / 11)
                ForEach((0...9), id: \.self) { groupId in
                    DataGroupRowView( id: UInt8(groupId), width: ((box.size.width-25)/4 ), opacity : groupId.isMultiple(of: 2) ? 0.1 : 0.2 )
                }
                .frame(height: ( box.size.height - 100 ) / 11)
            }
            .padding()
        }
    }
    
    fileprivate func THView(text: String, width: CGFloat) -> some View {
        return ZStack {
            Rectangle()
                .opacity(0.4)
            Text(text)
                .bold()
        }
        .frame(width: width)
    }
    
}
