import APIClient
import ComponentLibrary
import ComposableArchitecture
import SwiftUI

public struct ShareCardView: View {
    let title: String
    let tags: String?
    let cta: String
    let caption: String?
    let authorInfo: AuthorInfo?
    let imageUrl: String?
    let fallbackId: String?
    let type: Share.State.ShareType
    let width: CGFloat
    let height: CGFloat
    @State var downloadedImage: UIImage?

    public init(
        _ type: Share.State.ShareType,
        title: String,
        tags: String?,
        cta: String,
        caption: String?,
        authorInfo: AuthorInfo?,
        imageUrl: String?,
        fallbackId: String?
    ) {
        self.type = type
        self.title = title
        self.tags = tags
        self.cta = cta
        self.caption = caption
        self.authorInfo = authorInfo
        self.imageUrl = imageUrl
        self.fallbackId = fallbackId

        if UIScreen.isCompact {
            width = ShareCardSize.small.width
            height = ShareCardSize.small.height
        } else {
            width = ShareCardSize.regular.width
            height = ShareCardSize.regular.height
        }
    }

    public var body: some View {
        VStack(spacing: 0) {
            if let caption = caption {
                ShareCardCaptionView(caption: caption, type: type)
                    .padding(.top, 16)
            }
            Spacer()
            Text(title)
                .typographyV1(.headline3.editorialNewLight())
                .minimumScaleFactor(0.8)
                .foregroundColor(.SemanticV1.textOnDark)
                .fixedSize(horizontal: false, vertical: true)
                .multilineTextAlignment(.center)
                .lineLimit(2)
            if let tags = tags {
                Text(tags.truncated(to: 3, separator: ",").capitalized)
                    .typographyV1(.caption4)
                    .foregroundColor(.SemanticV1.textOnDark)
                    .multilineTextAlignment(.center)
                    .fixedSize(horizontal: false, vertical: true)
                    .lineLimit(2)
                    .minimumScaleFactor(0.8)
                    .opacity(0.5)
                    .padding(.horizontal, 60)
            }
            if let authorInfo = authorInfo {
                ShareCardAuthorCapsuleView(imageUrl: authorInfo.imageUrl, authorId: authorInfo.authorId, displayName: authorInfo.displayName)
                    .padding(.top, 12)
                    .padding(.bottom, 3)
            } else {
                Spacer().frame(height: 36)
            }
            HStack(spacing: 14) {
                AppIcon()
                    .frame(width: 40, height: 40)
                    .clipShape(RoundedRectangle(cornerRadius: 8))
                    .scaledToFit()
                Text(cta)
                    .typographyV1(.body1)
                    .foregroundStyle(Color.SemanticV1.textOnDark)
                    .lineLimit(2)
                    .multilineTextAlignment(.leading)
            }
            .padding([.vertical, .leading], 9)
            .padding(.trailing, 17)
            .background(Color.SemanticV1.backgroundPrimary.opacity(0.25))
            .clipShape(RoundedRectangle(cornerRadius: 12))
            .padding(12.91)
        }
        .frame(width: width, height: height)
        .background(backdrop)
        .clipShape(RoundedRectangle(cornerRadius: 18))
        .overlay(ShareCardConstants.whiteBorderWithGradient())
        .clipped()
        .shadow(
            color: ShareCardConstants.CardShadow.color,
            radius: ShareCardConstants.CardShadow.radius,
            x: ShareCardConstants.CardShadow.x,
            y: ShareCardConstants.CardShadow.y
        )
    }

    @ViewBuilder
    var backdrop: some View {
        RemoteImage(url: imageUrl, fallbackId: fallbackId)
            .environment(\.colorScheme, .dark)
            .overlay(alignment: .bottom) {
                if let downloadedImage {
                    AverageColorView(uiImage: downloadedImage)
                        .frame(height: 240)
                }
            }
    }
}

struct ShareCardAuthorCapsuleView: View {
    let imageUrl: String?
    let authorId: String?
    let displayName: String

    var body: some View {
        HStack(spacing: 4) {
            RemoteImage(url: imageUrl, fallbackId: authorId)
                .frame(width: 21, height: 21)
                .clipShape(.circle)

            Text(displayName)
                .typographyV1(.bodySmall)
                .foregroundColor(.SemanticV1.textOnDark)
        }
        .padding(4)
        .padding(.trailing, 8)
    }
}

struct ShareCardCaptionView: View {
    let caption: String
    let type: Share.State.ShareType

    var body: some View {
        HStack(alignment: .center, spacing: 4) {
            Image.Icon.note
                .foregroundColor(.SemanticV1.iconBrand)
            Text(caption)
                .textCase(useStatFont ? .uppercase : .none)
                .typographyV1(useStatFont ? .monospace : .subtitleSmall)
                .foregroundColor(.SemanticV1.textBrand)
        }
        .padding(.vertical, 4)
        .padding(.leading, 6)
        .padding(.trailing, 12)
        .background(Capsule().fill(Color.SemanticV1.backgroundTertiary))
    }

    private var useStatFont: Bool {
        switch type {
        case .profile, .playlist:
            return true
        default:
            return false
        }
    }
}
