import AnalyticsClient
import APIClient
import AVFoundation
import CommentsClient
import ComponentLibrary
import ComposableArchitecture
import FeatureClipDetail
import Foundation
import Localization
import MediaPlayer
import StatsigClient
import SwiftUI

public struct ExpandedPlayerActionBar: View {
    @Shared var playerState: OmniPlayerPlaybackState
    let remixTapped: () -> Void
    let likeTapped: () -> Void
    let dislikeTapped: () -> Void
    let commentsTapped: () -> Void
    let shareTapped: () -> Void
    let moreTapped: () -> Void

    public init(
        playerState: Shared<OmniPlayerPlaybackState>,
        remixTapped: @escaping () -> Void,
        likeTapped: @escaping () -> Void,
        dislikeTapped: @escaping () -> Void,
        commentsTapped: @escaping () -> Void,
        shareTapped: @escaping () -> Void,
        moreTapped: @escaping () -> Void
    ) {
        self._playerState = playerState
        self.remixTapped = remixTapped
        self.likeTapped = likeTapped
        self.dislikeTapped = dislikeTapped
        self.commentsTapped = commentsTapped
        self.shareTapped = shareTapped
        self.moreTapped = moreTapped
    }

    private var showRemixActionButton: Bool {
        playerState.hasRemixableClip
    }

    private var showCommentsButton: Bool {
        playerState.clip.type != .preview
    }

    private var clip: Clip {
        playerState.clip
    }

    private var commentCount: Int? {
        @Shared(.inMemory(.commentsCountMap)) var commentsCountMap: ClipCommentTotalCountMap = .defaultValue
        guard case .known(let count) = commentsCountMap.commentCountForClip(clip.id.remoteId) else {
            return clip.commentCount > 0 ? clip.commentCount : nil
        }
        return count > 0 ? count : nil
    }

    private var formattedCommentCount: String? {
        guard !playerState.isPlayingNewGeneration else { return nil }
        let count = commentCount ?? 0
        return count.formatted(.number.notation(.compactName))
    }

    private var remixCount: Int? {
        @Shared(.inMemory(.clipDirectChildrenCount)) var clipDirectChildrenCount: [ClipID: Int] = [:]
        return clipDirectChildrenCount[clip.id]
    }

    private var formattedRemixCount: String? {
        guard !playerState.isPlayingNewGeneration else { return nil }
        let count = remixCount ?? 0
        return count > 0 ? count.formatted(.number.notation(.compactName)) : L10n.FeatureOmniPlayer.remix.uppercased()
    }

    private var formattedLikeCount: String? {
        guard !playerState.isPlayingNewGeneration else { return nil }
        return clip.localUpvoteCount.formatted(.number.notation(.compactName))
    }

    private var dislikeLabel: String? {
        guard !playerState.isPlayingNewGeneration else { return nil }
        return L10n.FeatureOmniPlayer.dislike.uppercased()
    }

    private var shareLabel: String? {
        guard !playerState.isPlayingNewGeneration else { return nil }
        return L10n.FeatureOmniPlayer.share.uppercased()
    }

    public var body: some View {
        VStack(spacing: 12) {
            Spacer()
            if showRemixActionButton {
                ActionButton(
                    icon: Image.Icon.remix,
                    label: formattedRemixCount?.uppercased()
                ) { remixTapped() }
            }

            ActionButton(
                icon: Image.Omniplayer.thumbsUp,
                isHighlighted: clip.isLiked,
                highlightColor: .SemanticV1.alwaysBlack1,
                label: formattedLikeCount?.uppercased()
            ) { likeTapped() }

            // Only show the dislike button when listening to your songs
            if playerState.isMyClip {
                ActionButton(
                    icon: Image.Omniplayer.thumbsUp.rotationEffect(.degrees(180)),
                    isHighlighted: clip.isDisliked,
                    label: dislikeLabel
                ) { dislikeTapped() }
            }

            if showCommentsButton {
                ActionButton(
                    icon: Image.Omniplayer.comment,
                    label: formattedCommentCount
                ) { commentsTapped() }
            }

            ActionButton(
                icon: Image.Omniplayer.share.renderingMode(.template),
                label: shareLabel
            ) { shareTapped() }

            ActionButton(
                icon: Image.Omniplayer.more,
                isMore: true,
                tooltip: nil,
                tooltipAccentColor: Color.SemanticV2.accentBrand,
                action: {
                    moreTapped()
                }
            )
        }
        .opacity(playerState.isScrubbing ? 0.2 : 1)
        .animation(.easeInOut(duration: 0.3), value: playerState.isScrubbing)
        .glassEffectContainer()
    }
}
