import ComponentLibrary
import Foundation
import SwiftUI
import Utilities

public struct MenuRow<Icon: View>: View {
    let title: String
    let flagWarning: Bool
    let action: () -> Void
    let icon: Icon

    public init(
        title: String,
        flagWarning: Bool = false,
        _ action: @escaping () -> Void,
        @ViewBuilder icon: () -> Icon
    ) {
        self.icon = icon()
        self.title = title
        self.action = action
        self.flagWarning = flagWarning
    }

    public var body: some View {
        Button {
            action()
        } label: {
            VStack {
                icon
                    .frame(width: 24, height: 24)
                    .foregroundStyle(
                        flagWarning
                            ? Color.SemanticV2.accentError
                            : Color.SemanticV1.iconPrimary
                    )
                Text(title)
                    .font(TypographyV1.bodySmall.font)
                    .foregroundStyle(
                        flagWarning
                            ? Color.SemanticV2.accentError
                            : Color.SemanticV1.textPrimary
                    )
            }
            .frame(maxWidth: .infinity)
            .frame(height: 64)
            .contentShape(.rect)
        }
        .background(Color.SemanticV1.backgroundSecondary)
        .simultaneousGesture(TapGesture().onEnded {})
    }
}

public extension MenuRow where Icon == Image {
    init(
        title: String,
        flagWarning: Bool = false,
        _ action: @escaping () -> Void,
        icon: @escaping () -> Image
    ) {
        self.title = title
        self.flagWarning = flagWarning
        self.action = action
        self.icon = icon()
            .renderingMode(.template)
    }
}
