import CommentsClient
import Foundation
import SwiftUI

struct CommentActionButton: View {
    let commentCount: ClipCommentTotalCountMap.CountStyle
    let fallbackCommentCount: Int

    init(
        commentCount: ClipCommentTotalCountMap.CountStyle,
        fallbackCommentCount: Int
    ) {
        self.commentCount = commentCount
        self.fallbackCommentCount = fallbackCommentCount
    }

    var body: some View {
        switch commentCount {
        case .known(let count):
            if count == .zero {
                zeroStyleBubbleOnly
            } else {
                VStack(spacing: 2.0) {
                    Image.Icon.commentBubble
                        .resizable()
                        .frame(width: 16.0, height: 16.0)
                    Text("\(count.formatted(.number.notation(.compactName)))")
                        .typographyV1(.monospaceSmall)
                        .foregroundStyle(Color.SemanticV1.textPrimary)
                }
                .offset(y: 4.0)
            }

        case .unknown:
            if fallbackCommentCount == .zero {
                zeroStyleBubbleOnly
            } else {
                VStack(spacing: 2.0) {
                    Image.Icon.commentBubble
                        .resizable()
                        .frame(width: 16.0, height: 16.0)
                    Text("\(fallbackCommentCount.formatted(.number.notation(.compactName)))")
                        .typographyV1(.monospaceSmall)
                        .foregroundStyle(Color.SemanticV1.textPrimary)
                }
                .offset(y: 4.0)
            }
        }
    }
}

private extension CommentActionButton {
    @ViewBuilder
    var zeroStyleBubbleOnly: some View {
        Image.Icon.commentBubble
            .resizable()
            .frame(width: 20.0, height: 20.0)
    }
}
