//
//  View+AppListStyle.swift
//  Tusker
//
//  Created by Shadowfacts on 2/6/23.
//  Copyright © 2023 Shadowfacts. All rights reserved.
//

import SwiftUI
import Combine

extension View {
    @ViewBuilder
    func appGroupedListBackground(container: UIAppearanceContainer.Type, applyBackground: Bool = true) -> some View {
        if #available(iOS 16.0, *) {
            if applyBackground {
                self
                    .scrollContentBackground(.hidden)
                    .background(Color.appGroupedBackground.edgesIgnoringSafeArea(.all))
            } else {
                self
                    .scrollContentBackground(.hidden)
            }
        } else {
            self
                .onAppear {
                    UITableView.appearance(whenContainedInInstancesOf: [container]).backgroundColor = .appGroupedBackground
                }
        }
    }
    
    func appGroupedListRowBackground() -> some View {
        self.modifier(AppGroupedListRowBackground())
    }
}

private struct AppGroupedListRowBackground: ViewModifier {
    @Environment(\.colorScheme) private var colorScheme
    
    func body(content: Content) -> some View {
        if colorScheme == .dark, !Preferences.shared.pureBlackDarkMode {
            content
                .listRowBackground(Color.appGroupedCellBackground)
        } else {
            content
        }
    }
}
