import Adamantium
import AVFoundation
import ComponentLibrary
import ShareAssetClient
import SwiftUI

struct VisualizerGraphic: View {
    let size: CGSize
    let isPlaying: Bool
    let overrideTime: CMTime
    let videoGravity: AVLayerVideoGravity
    var assetConfig: VisualizerAssetConfig

    var body: some View {
        ZStack {
            let preset = assetConfig.shareAssetConfig.backgroundPreset
            let id = "\(preset.presetID)-\(preset.presetStyle)"
            let backgroundPresetStyle = preset.enumPresetStyle
            switch backgroundPresetStyle {
            case .aura(let style):
                switch style {
                case .blue:
                    AuraCard(
                        id: id,
                        size: size,
                        colorSet: .custom(
                            .custom(0.0, 0.29, 0.553),
                            .custom(0.0, 0.098, 0.341),
                            .custom(0.145, 0.255, 0.455)
                        ),
                        seed: 100.0,
                        isPaused: !isPlaying
                    )

                case .standard:
                    AuraCard(
                        id: id,
                        size: size,
                        colorSet: .custom(
                            .custom(0.922, 0.353, 0.506),
                            .custom(0.922, 0.38, 0.267),
                            .custom(0.953, 0.722, 0.314)
                        ),
                        seed: 0.0,
                        isPaused: !isPlaying
                    )

                case .purple:
                    let b: Float = 1.0
                    AuraCard(
                        id: id,
                        size: size,
                        colorSet: .custom(
                            .custom(0.129411764705882 * b, 0.098039215686275 * b, 0.474509803921569 * b),
                            .custom(0.435294117647059 * b, 0.098039215686275 * b, 0.631372549019608 * b),
                            .custom(0.647058823529412 * b, 0.270588235294118 * b, 0.356862745098039 * b)
                        ),
                        seed: 50.0,
                        isPaused: !isPlaying
                    )
                }

            case .neon:
                AuraShaderView(id: id, colorSet: .maroonVioletRed)

            case .space:
                AuraShaderView(id: id, colorSet: .maroonVioletRed)

            case .goo:
                GooWavesViewComponent(id: id, timeScale: 1.0, isPaused: !isPlaying)

            case .gridWarp(let style):
                switch style {
                case .blue:
                    WarpedGridViewComponent(
                        id: id,
                        timeScale: 1.0,
                        colorA: SIMD3<Float>(0.1, 0.5, 0.644),
                        colorB: SIMD3<Float>(0.2, 0.3, 0.4),
                        vortexRadius: 4.0,
                        vortexStrength: 0.4,
                        frameRate: 60,
                        isPaused: !isPlaying
                    )

                case .pink:
                    WarpedGridViewComponent(
                        id: id,
                        timeScale: 1.0,
                        frameRate: 60,
                        isPaused: !isPlaying
                    )
                }

            case .clouds:
                CloudSkyViewComponent(id: id, timeScale: 0.03, isPaused: !isPlaying)
                    .overlay {
                        Color.black.opacity(0.5)
                    }

            case .cover(let style):
                switch style {
                case .image:
                    if let coverImageURL = assetConfig.coverImageURL {
                        RemoteImage(url: coverImageURL, fallbackId: "1")
                            .aspectRatio(1, contentMode: .fill)
                        Color.black.opacity(0.5)
                    } else {
                        Color.clear
                    }

                case .video:
                    if let videoURL = assetConfig.coverVideoURL,
                       let imageURL = assetConfig.coverImageURL
                    {
                        LoopingVideoPlayer(
                            playableUrl: URL(string: videoURL),
                            restartBeforePlaying: false,
                            isPlaying: Binding(
                                get: { isPlaying },
                                set: { _ in }
                            ),
                            overrideTime: overrideTime,
                            videoGravity: videoGravity,
                            fallbackView: {
                                RemoteImage(url: imageURL, fallbackId: "1")
                            }
                        )
                        .background {
                            Color.SemanticV1.backgroundSecondary
                        }
                        Color.black.opacity(0.5)
                    }
                }
            }
        }
    }
}

struct AuraCard: View {
    let id: String
    let size: CGSize
    let colorSet: SunoTriColorSet
    let seed: Float
    let isPaused: Bool
    @State var stillImage: UIImage?
    var body: some View {
        ZStack {
            Color.black
            AuraShaderView(
                id: "visualizer-\(id)",
                colorSet: colorSet,
                seed: 100.0,
                isPaused: isPaused
            )
        }
    }
}
