//
//  DataGroupsSheetView.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 10/03/2020.
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
//

import SwiftUI

struct DataGroupsSheetView: View {
    
    @Binding var visibility: Bool
    @EnvironmentObject private var usbMeter: Meter
    
    var body: some View {
        NavigationView {
            GeometryReader { box in
                let columnTitles = [usbMeter.supportsDataGroupCommands ? "Group" : "Memory"]
                    + (usbMeter.showsDataGroupEnergy ? ["Wh"] : [])
                    + (usbMeter.supportsDataGroupCommands ? ["Clear"] : [])
                let columnWidth = (box.size.width - 60) / CGFloat(columnTitles.count)

                ScrollView {
                    VStack(alignment: .leading, spacing: 14) {
                        VStack(alignment: .leading, spacing: 8) {
                            HStack {
                                Text(usbMeter.dataGroupsTitle)
                                    .font(.system(.title3, design: .rounded).weight(.bold))
                                if let hint = usbMeter.dataGroupsHint {
                                    ContextInfoButton(
                                        title: usbMeter.dataGroupsTitle,
                                        message: hint
                                    )
                                }
                                Spacer(minLength: 0)
                            }
                        }
                        .padding(18)
                        .meterCard(tint: .teal, fillOpacity: 0.18, strokeOpacity: 0.24)

                        HStack(spacing: 8) {
                            ForEach(columnTitles, id: \.self ) { text in
                                headerCell(text: text, width: columnWidth)
                            }
                        }

                        VStack(spacing: 10) {
                            ForEach(usbMeter.availableDataGroupIDs, id: \.self) { groupId in
                                DataGroupRowView(
                                    id: groupId,
                                    width: columnWidth,
                                    opacity: groupId.isMultiple(of: 2) ? 0.1 : 0.2,
                                    showsCommands: usbMeter.supportsDataGroupCommands,
                                    showsEnergy: usbMeter.showsDataGroupEnergy,
                                    highlightsSelection: usbMeter.highlightsActiveDataGroup
                                )
                            }
                        }
                    }
                    .padding()
                }
                .background(
                    LinearGradient(
                        colors: [.teal.opacity(0.14), Color.clear],
                        startPoint: .topLeading,
                        endPoint: .bottomTrailing
                    )
                    .ignoresSafeArea()
                )
            }
            .navigationTitle(usbMeter.dataGroupsTitle)
            .navigationBarTitleDisplayMode(.inline)
            .navigationBarItems(trailing: Button("Done") { visibility.toggle() })
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
    
    fileprivate func headerCell(text: String, width: CGFloat) -> some View {
        Text(text)
            .font(.footnote.weight(.bold))
            .frame(width: width)
            .frame(minHeight: 38)
            .foregroundColor(.secondary)
            .meterCard(tint: .secondary, fillOpacity: 0.14, strokeOpacity: 0.20, cornerRadius: 14)
    }
}
