//
//  MeterLiveTabView.swift
//  USB Meter
//

import SwiftUI

struct MeterLiveTabView: View {
    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 {
                    MeterLiveContentView(compactLayout: true, availableSize: size)
                        .padding(contentCardPadding)
                        .frame(maxWidth: .infinity, alignment: .topLeading)
                        .meterCard(tint: meter.color, fillOpacity: 0.12, strokeOpacity: 0.20)
                }
            } else {
                portraitFace {
                    MeterLiveContentView(compactLayout: prefersCompactPortraitLayout, availableSize: size)
                        .padding(contentCardPadding)
                        .meterCard(tint: meter.color, fillOpacity: 0.12, strokeOpacity: 0.20)
                }
            }
        }
    }

    @EnvironmentObject private var meter: Meter

    private var prefersCompactPortraitLayout: Bool {
        size.height < 760 || size.width < 380
    }

    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 {
        ScrollView {
            content()
                .frame(maxWidth: .infinity, alignment: .topLeading)
                .padding(.horizontal, pageHorizontalPadding)
                .padding(.vertical, pageVerticalPadding)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
    }
}
