//
//  ListRowViewModifier.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 27/03/2024.
//

import SwiftUI

struct ListRowViewModifier: ViewModifier {
    let isActive: Bool
    let color: Color
    var hideColorVariation: Bool = false
    var backgroundColor: Color? = nil
    let alignment: Alignment
    
    func colorVariation() -> Color {
        hideColorVariation ? (backgroundColor ?? Color(uiColor: .secondarySystemGroupedBackground)) : color.variation()
    }

    func body(content: Content) -> some View {
        if isActive {
            content
                .listRowBackground(
                    colorVariation()
                        .overlay(alignment: alignment, content: {
                            color.frame(width: 8)
                        })
                )
        } else {
            content
        }
    }
}

extension View {
    func listRowView(isActive: Bool = false, color: Color, hideColorVariation: Bool = false, backgroundColor: Color? = nil, alignment: Alignment = .leading) -> some View {
        modifier(ListRowViewModifier(isActive: isActive, color: color, hideColorVariation: hideColorVariation, backgroundColor: backgroundColor, alignment: alignment))
    }
}
