//
//  MeterChartTabView.swift
//  USB Meter
//

import SwiftUI

struct MeterChartTabView: View {
    @EnvironmentObject private var meter: Meter

    let size: CGSize
    let isLandscape: Bool

    private let pageHorizontalPadding: CGFloat = 12
    private let pageVerticalPadding: CGFloat = 12
    private let contentCardPadding: CGFloat = 16

    var body: some View {
        Group {
            if isLandscape {
                landscapeFace {
                    MeasurementChartView()
                        .environmentObject(meter.measurements)
                        .frame(height: max(250, size.height - 44))
                        .padding(contentCardPadding)
                        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
                        .meterCard(tint: meter.color, fillOpacity: 0.12, strokeOpacity: 0.20)
                }
            } else {
                portraitFace {
                    MeasurementChartView()
                        .environmentObject(meter.measurements)
                        .frame(minHeight: size.height / 3.4)
                        .padding(contentCardPadding)
                        .meterCard(tint: meter.color, fillOpacity: 0.12, strokeOpacity: 0.20)
                }
            }
        }
    }

    private func portraitFace<Content: View>(@ViewBuilder content: () -> Content) -> some View {
        ScrollView {
            content()
                .frame(maxWidth: .infinity, alignment: .topLeading)
                .padding(.horizontal, pageHorizontalPadding)
                .padding(.vertical, pageVerticalPadding)
        }
    }

    private func landscapeFace<Content: View>(@ViewBuilder content: () -> Content) -> some View {
        content()
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
            .padding(.horizontal, pageHorizontalPadding)
            .padding(.vertical, pageVerticalPadding)
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
    }
}