import APIClient
import Foundation
import StatsigClient
import SwiftUI

public struct SongDetailsTile: View {
    let clip: Clip

    public init(clip: Clip) {
        self.clip = clip
    }

    private var shouldShowDisplayTags: Bool {
        guard let displayTags = clip.displayTags, !displayTags.isEmpty else {
            return false
        }
        switch clip.highLevelModel {
        case .v4_5, .v4_5Plus, .v5:
            return true
        default:
            return false
        }
    }

    public var body: some View {
        HStack {
            clipThumbnail
            songInfo
        }
        .padding(12)
        .background(Color.SemanticV2.backgroundFogThin)
        .cornerRadius(16)
    }

    @ViewBuilder
    private var clipThumbnail: some View {
        ZStack {
            RemoteImage(url: clip.imageUrl, fallbackId: "1")
                .frame(width: 67, height: 86)
                .cornerRadius(12)

            VStack {
                Spacer()
                HStack {
                    Spacer()
                    Text(clip.duration.format_m_ss)
                        .typographyV1(.durationFont)
                        .foregroundStyle(Color.SemanticV1.textPrimary)
                        .padding(4)
                        .background(.ultraThinMaterial)
                        .overlay(Color.SemanticV2.backgroundSmokeThick)
                        .clipShape(Capsule())
                }
                .padding(.trailing, 1)
            }
            .padding(.bottom, 4)
        }
        .frame(width: 67, height: 86)
        .padding(.trailing, 16)
    }

    @ViewBuilder
    private var songInfo: some View {
        VStack(alignment: .leading, spacing: 0) {
            songTitleAndModel
            songTags
        }
    }

    @ViewBuilder
    private var songTitleAndModel: some View {
        HStack(spacing: 0) {
            ViewThatFits(in: .horizontal) {
                // Try regular text first - it will only fit if there's enough space
                HStack(spacing: 0) {
                    Text(clip.title)
                        .typographyV1(.songTitleFont)
                        .foregroundStyle(Color.SemanticV1.textPrimary)
                        .lineLimit(1)
                        .padding(.trailing, 4)

                    versioningPill
                }

                // Fall back to MarqueeText if regular text doesn't fit
                HStack(spacing: 0) {
                    MarqueeText(
                        text: clip.title,
                        font: TypographyV1.songTitleFont.uiFont ?? .systemFont(ofSize: 16),
                        leftFade: 4,
                        rightFade: 4,
                        startDelay: 1,
                        alignment: .leading
                    )
                    .foregroundStyle(Color.SemanticV1.textPrimary)
                    .padding(.trailing, 4)

                    versioningPill
                }
            }
            Spacer(minLength: 0)
        }
    }

    @ViewBuilder
    private var songTags: some View {
        if shouldShowDisplayTags, let displayTags = clip.displayTags {
            TagText(displayTags, fontSize: 12, font: .tagText, textColor: .SemanticV1.textSecondary)
        } else if !clip.tags.isEmpty {
            TagText(clip.tags, fontSize: 12, font: .tagText, textColor: .SemanticV1.textSecondary)
        }
    }

    @ViewBuilder
    private var versioningPill: some View {
        // Use ModelBadgeStyle if feature flag is enabled and style is available
        if FeatureFlag.legacy.v5LaunchModelStyles,
           let modelBadgeStyle = clip.modelBadgeStyle
        {
            Pill(type: .modelBadge(style: modelBadgeStyle))
        } else {
            // Fallback to default versioning pills
            switch clip.highLevelModel {
            case .previousToV4:
                EmptyView()

            case .v5:
                Pill(type: .versioning(version: .v5))

            case .v4:
                Pill(type: .versioning(version: .v4))

            case .v4_5:
                Pill(type: .versioning(version: .v4_5))

            case .v4_5Plus:
                Pill(type: .versioning(version: .v4_5Plus))
            }
        }
    }
}

private extension TypographyV1 {
    static let dataCountFont: TypographyV1 = .init(
        name: "Data Count Font",
        size: 10,
        style: .body,
        weight: .inputSans,
        lineHeight: 16
    )

    static let durationFont: TypographyV1 = .init(
        name: "Duration Font",
        size: 10,
        style: .body,
        weight: .inputSans,
        kerning: 0.2,
        lineHeight: 16
    )

    static let songTitleFont: TypographyV1 = .init(
        name: "Song Title Font",
        size: 16,
        style: .body,
        weight: .ppNeueMontrealMedium,
        kerning: 0.32,
        lineHeight: 24
    )

    static let tagText: TypographyV1 = .init(
        name: "Tag Text",
        size: 12,
        style: .body,
        weight: .ppNeueMontrealRegular,
        kerning: 0.24,
        lineHeight: 16
    )
}
