import APIClient
import ComposableArchitecture
import Foundation
import Localization
import SwiftUI

public struct HookVideoCellActionBar: View {
    let hook: Hook
    let isFollowing: Bool
    let isLiked: Bool
    let likeCount: Int
    let commentCount: Int
    let onRemixTapped: (Hook) -> Void
    let onLikeTapped: (Hook) -> Void
    let onProfileTapped: (String) -> Void
    let onFollowTapped: (Hook) -> Void
    let onShareTapped: (Hook) -> Void
    let onCommentsTapped: (Hook) -> Void
    let onMoreTapped: (Hook) -> Void

    public init(
        hook: Hook,
        isFollowing: Bool = false,
        isLiked: Bool = false,
        likeCount: Int = 0,
        commentCount: Int = 0,
        onRemixTapped: @escaping (Hook) -> Void,
        onLikeTapped: @escaping (Hook) -> Void,
        onProfileTapped: @escaping (String) -> Void,
        onFollowTapped: @escaping (Hook) -> Void,
        onShareTapped: @escaping (Hook) -> Void,
        onCommentsTapped: @escaping (Hook) -> Void,
        onMoreTapped: @escaping (Hook) -> Void
    ) {
        self.hook = hook
        self.isFollowing = isFollowing
        self.isLiked = isLiked
        self.likeCount = likeCount
        self.commentCount = commentCount
        self.onRemixTapped = onRemixTapped
        self.onLikeTapped = onLikeTapped
        self.onProfileTapped = onProfileTapped
        self.onFollowTapped = onFollowTapped
        self.onShareTapped = onShareTapped
        self.onCommentsTapped = onCommentsTapped
        self.onMoreTapped = onMoreTapped
    }

    private var formattedCommentCount: String? {
        guard commentCount > 0 else { return L10n.FeatureHooks.comment.uppercased() }
        return commentCount.formatted(.number.notation(.compactName))
    }

    private var formattedLikeCount: String? {
        guard likeCount > 0 else { return L10n.FeatureHooks.like.uppercased() }
        return likeCount.formatted(.number.notation(.compactName))
    }

    private var showRemixButton: Bool {
        /// Deviates from normal `Clip` remixability logic in that:
        /// 1. `clip` may be optional. To be safe, we default to `false`.
        /// 2. You can normally remix your own clip regardless of remixability permissions. It's a bigger and fundamental architecture change to pipe `Me` into this action bar, so this functionality is disabled for now.
        hook.clip?.canRemix ?? false
    }

    public var body: some View {
        VStack(spacing: 12) {
            Spacer()

            if showRemixButton {
                ActionButton(
                    icon: Image.Icon.remix,
                    label: L10n.FeatureOmniPlayer.remix.uppercased()
                ) {
                    onRemixTapped(hook)
                }
            }

            ActionButton(
                icon: Image.Icon.heartFilled,
                isHighlighted: isLiked,
                label: formattedLikeCount,
                shouldAnimateOnTap: true
            ) {
                onLikeTapped(hook)
            }

            ActionButton(
                icon: Image.Omniplayer.comment,
                label: formattedCommentCount
            ) {
                onCommentsTapped(hook)
            }

            ActionButton(
                icon: Image.Omniplayer.share.renderingMode(.template),
                label: L10n.FeatureOmniPlayer.share.uppercased()
            ) {
                onShareTapped(hook)
            }

            ActionButton(
                icon: Image.Omniplayer.more,
                isMore: true
            ) {
                onMoreTapped(hook)
            }
        }
        .glassEffectContainer()
    }
}
