import ComponentLibrary
import Foundation
import SwiftUI
import Utilities

public struct MenuRowFullWidth<LeadingView: View>: View {
    let leadingView: LeadingView?
    let title: String
    let subtitle: String?
    let isLoading: Bool
    let flagWarning: Bool
    let action: () -> Void
    let trailingView: AnyView?
    let isToggleOn: Bool
    let toggleAction: (() -> Void)?
    let toggleTint: Color
    let rowType: MenuRowFullWidthType

    public init(
        title: String,
        subtitle: String? = nil,
        isLoading: Bool = false,
        flagWarning: Bool = false,
        rowType: MenuRowFullWidthType = .plain,
        isToggleOn: Bool = false,
        toggleAction: (() -> Void)? = nil,
        toggleTint: Color = Color.SemanticV2.accentPink,
        _ action: @escaping () -> Void,
        @ViewBuilder leadingView: () -> LeadingView?,
        @ViewBuilder trailingView: () -> AnyView? = { nil }
    ) {
        self.title = title
        self.subtitle = subtitle
        self.isLoading = isLoading
        self.action = action
        self.flagWarning = flagWarning
        self.trailingView = trailingView()
        self.rowType = rowType
        self.isToggleOn = isToggleOn
        self.toggleAction = toggleAction
        self.toggleTint = toggleTint
        self.leadingView = leadingView()
    }

    public var body: some View {
        Button {
            action()
        } label: {
            HStack(spacing: 0) {
                HStack(spacing: 10) {
                    if let _ = leadingView {
                        renderedLeadingView
                            .frame(width: 24, height: 24)
                    }

                    Text(title)
                        .foregroundStyle(
                            flagWarning
                                ? Color.SemanticV2.accentError
                                : Color.SemanticV2.foregroundPrimary
                        )
                        .typographyV1(.caption)
                }

                Spacer()

                if let trailingView {
                    trailingView
                        .foregroundStyle(
                            flagWarning
                                ? Color.SemanticV2.accentError
                                : Color.SemanticV2.foregroundPrimary
                        )
                }

                if rowType == .toggle {
                    Toggle(
                        isOn: .init(
                            get: { isToggleOn },
                            set: { _ in
                                toggleAction?()
                            }
                        ),
                        label: {
                            EmptyView()
                        }
                    )
                    .frame(height: 42)
                    .tint(toggleTint)
                }
            }
            .padding(.leading, 15)
            .padding(.vertical, rowType == .plain ? 15 : 8)
            .padding(.trailing, 10)
            .contentShape(.rect)
        }
        .allowsHitTesting(isLoading ? false : true)
        .buttonStyle(.plain)
        .background(Color.SemanticV1.backgroundSecondary)
        .simultaneousGesture(TapGesture().onEnded {})
    }
}

public enum MenuRowFullWidthType {
    case plain
    case toggle
}

public extension MenuRowFullWidth {
    var renderedLeadingView: some View {
        leadingView
    }
}
