import Foundation
import Localization
import SwiftUI

public struct Tooltip: Equatable, Sendable {
    let title: String
    let subtitle: String
    let buttonTitle: String
    let badgeLabel: String?
    let configuration: Configuration

    public static func == (lhs: Tooltip, rhs: Tooltip) -> Bool {
        return lhs.title == rhs.title &&
            lhs.subtitle == rhs.subtitle &&
            lhs.buttonTitle == rhs.buttonTitle &&
            lhs.configuration == rhs.configuration
    }

    public init(
        title: String,
        subtitle: String,
        buttonTitle: String,
        badgeLabel: String? = nil,
        configuration: Configuration
    ) {
        self.title = title
        self.subtitle = subtitle
        self.buttonTitle = buttonTitle
        self.badgeLabel = badgeLabel
        self.configuration = configuration
    }
}

public extension Tooltip {
    enum ButtonCopy {
        case gotIt

        var displayText: String {
            switch self {
            case .gotIt: return L10n.FeaturePlayer.gotIt
            }
        }
    }

    static func preset(_ preset: Preset) -> Tooltip {
        return preset.textCopy
    }
}

struct TooltipView: View {
    let tooltip: Tooltip
    let maxWidth: CGFloat
    let accentColor: Color
    let onButtonTap: () -> Void

    private var systemColorScheme: ColorScheme {
        UITraitCollection.current.userInterfaceStyle == .dark ? .dark : .light
    }

    var body: some View {
        VStack(alignment: .leading, spacing: tooltip.configuration.spacing) {
            if let badgeLabel = tooltip.badgeLabel {
                BadgeLabel(badgeLabel)
            }

            Text(tooltip.title)
                .typographyV1(.navigationTitle)
                .foregroundColor(.SemanticV1.textPrimary)
                .multilineTextAlignment(.leading)
                .fixedSize(horizontal: false, vertical: true)

            Text(tooltip.subtitle)
                .typographyV1(.body2)
                .foregroundColor(.SemanticV1.textSecondary)
                .multilineTextAlignment(.leading)
                .fixedSize(horizontal: false, vertical: true)

            switch tooltip.configuration.buttonStyle {
            case .textLink:
                textLinkButton

            case .auraButton:
                auraButton
            }
        }
        .padding(16)
        .padding(.top, tooltip.configuration.anchor.topOffset)
        .padding(.trailing, 12)
        .background(
            TooltipBubbleShape(
                anchor: tooltip.configuration.anchor,
                style: tooltip.configuration.bubbleStyle
            )
            .fill(Color.SemanticV1.backgroundSecondary)
            .shadow(color: .black.opacity(0.3), radius: 8, x: 2, y: 4)
        )
        .frame(maxWidth: maxWidth)
        .fixedSize(horizontal: true, vertical: false)
        .environment(\.colorScheme, systemColorScheme)
    }

    @ViewBuilder
    private var textLinkButton: some View {
        Button {
            withAnimation(.easeInOut(duration: 0.2)) {
                onButtonTap()
            }
        } label: {
            Text(tooltip.buttonTitle)
                .typographyV1(.button1)
                .foregroundColor(accentColor)
                .padding(.top, 4)
                .contentShape(.rect)
        }
    }

    @ViewBuilder
    private var auraButton: some View {
        Button(tooltip.buttonTitle) {
            withAnimation(.easeInOut(duration: 0.2)) {
                onButtonTap()
            }
        }.buttonStyle(AuraButtonStyle())
    }
}
