//
//  SidebarListView.swift
//  USB Meter
//

import SwiftUI

struct SidebarListView<USBMetersSection: View, HelpSection: View, DebugSection: View>: View {
    let backgroundTint: Color
    let usbMetersSection: USBMetersSection
    let helpSection: HelpSection
    let debugSection: DebugSection

    init(
        backgroundTint: Color,
        @ViewBuilder usbMetersSection: () -> USBMetersSection,
        @ViewBuilder helpSection: () -> HelpSection,
        @ViewBuilder debugSection: () -> DebugSection
    ) {
        self.backgroundTint = backgroundTint
        self.usbMetersSection = usbMetersSection()
        self.helpSection = helpSection()
        self.debugSection = debugSection()
    }

    var body: some View {
        if #available(iOS 16.0, *) {
            listBody.scrollContentBackground(.hidden)
        } else {
            listBody
        }
    }

    private var listBody: some View {
        List {
            usbMetersSection
            helpSection
            debugSection
        }
        .listStyle(SidebarListStyle())
        .background(
            LinearGradient(
                colors: [
                    backgroundTint.opacity(0.18),
                    Color.clear
                ],
                startPoint: .topLeading,
                endPoint: .bottomTrailing
            )
            .ignoresSafeArea()
        )
    }
}
