import Localization
import SwiftUI

// Simple card containing a title & body text, copy button which copies body text to clipboard
public struct ClipDetailsSimpleCardView: View {
    let cardTitle: String
    let bodyText: String?
    let textColor: Color
    let backgroundColor: Color
    let buttonText: String?
    let buttonColor: Color?
    let shouldShowButton: Bool
    let onCopy: () -> Void
    let onButtonTap: () -> Void?

    @State var isShowingExpandedBodyText: Bool = false
    @State var cardSize: CGSize = CGSize(width: 0, height: 0)

    private var gradientOverlayHeight: Double {
        let finalSize = (cardSize.height / 2)
        return finalSize
    }

    public init(
        cardTitle: String,
        bodyText: String?,
        textColor: Color,
        backgroundColor: Color,
        buttonText: String?,
        buttonColor: Color? = Color.SemanticV2.backgroundFogThin,
        shouldShowButton: Bool,
        onCopy: @escaping () -> Void,
        onButtonTap: @escaping () -> Void?
    ) {
        self.cardTitle = cardTitle
        self.bodyText = bodyText
        self.textColor = textColor
        self.backgroundColor = backgroundColor
        self.buttonText = buttonText
        self.buttonColor = buttonColor
        self.shouldShowButton = shouldShowButton
        self.onCopy = onCopy
        self.onButtonTap = onButtonTap
    }

    public var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            HStack {
                Text(cardTitle)
                    .typographyV1(.playerCardBody.ppNeueMontrealBold())
                    .foregroundColor(textColor)
                Spacer()

                if let bodyText, !bodyText.isEmpty {
                    Button {
                        UIPasteboard.general.string = bodyText
                        onCopy()
                    } label: {
                        Image.Icon.copy
                            .foregroundColor(.SemanticV2.foregroundTertiaryGlass)
                            .frame(width: 18, height: 18)
                    }
                    .buttonStyle(ScaleButtonStyle(scaleAmount: 0.9))
                }
            }

            if let bodyText, !bodyText.isEmpty {
                expandableBodyText
            }

            if let buttonText, shouldShowButton {
                Button(action: {
                    onButtonTap()
                }) {
                    Text(buttonText)
                        .typographyV1(.caption.neueMontrealRegular().kerning(0.28))
                        .foregroundStyle(Color.SemanticV2.foregroundPrimary)
                        .padding(.vertical, 14)
                        .frame(maxWidth: .infinity)
                }
                .background(buttonColor)
                .cornerRadius(50.0)
            }
        }
        .padding(16)
        .glassBackground(shape: .rect(cornerRadius: 16), fallbackStyle: backgroundColor)
        .onTapGesture {
            if let bodyText, !bodyText.isEmpty {
                withAnimation(.snappy(duration: 0.25)) {
                    self.isShowingExpandedBodyText.toggle()
                }
            }
        }
    }

    private var expandableBodyText: some View {
        ZStack(alignment: .topLeading) {
            // Truncated text (always rendered)
            Text(bodyText ?? "")
                .typographyV1(.playerCardBody.neueMontrealRegular())
                .lineLimit(isShowingExpandedBodyText ? nil : 12)
                .truncationMode(.tail)
                .frame(maxWidth: .infinity, alignment: .leading)
                .opacity(isShowingExpandedBodyText ? 0 : 1)
                .readSize(to: $cardSize)

            // Full text (always rendered)
            Text(bodyText ?? "")
                .typographyV1(.playerCardBody.neueMontrealRegular())
                .lineLimit(isShowingExpandedBodyText ? nil : 12)
                .truncationMode(.tail)
                .frame(maxWidth: .infinity, alignment: .leading)
        }
        .mask {
            if !isShowingExpandedBodyText {
                VStack(spacing: 0) {
                    Rectangle()
                        .fill(Color.white)

                    LinearGradient(
                        gradient: Gradient(stops: [
                            .init(color: Color.white, location: 0),
                            .init(color: Color.clear, location: 1),
                        ]),
                        startPoint: .top,
                        endPoint: .bottom
                    )
                    .frame(height: gradientOverlayHeight)
                }
            } else {
                Rectangle()
                    .fill(Color.white)
            }
        }
        .drawingGroup()
    }
}

#Preview {
    VStack {
        ClipDetailsSimpleCardView(
            cardTitle: "this is the card title",
            bodyText: "this is the body text!",
            textColor: Color.SemanticV1.textPrimary,
            backgroundColor: Color.SemanticV1.backgroundSecondary,
            buttonText: "Reuse Style",
            shouldShowButton: true,
            onCopy: {},
            onButtonTap: {}
        )

        ClipDetailsSimpleCardView(
            cardTitle: "this is the card title",
            bodyText: nil,
            textColor: Color.SemanticV2.foregroundPrimary,
            backgroundColor: Color.SemanticV2.backgroundFogThin,
            buttonText: "Reuse Style",
            shouldShowButton: false,
            onCopy: {},
            onButtonTap: {}
        )

        ClipDetailsSimpleCardView(
            cardTitle: "Card without copy",
            bodyText: "Some content here",
            textColor: Color.SemanticV1.textPrimary,
            backgroundColor: Color.SemanticV1.backgroundSecondary,
            buttonText: "Reuse Style",
            shouldShowButton: false,
            onCopy: {},
            onButtonTap: {}
        )
    }
}
