import Adamantium
import ComposableArchitecture
import SwiftUI

public struct AuraShaderView: View {
    public enum AppPreset {
        /*
            Not using extension on SunoTriColorSet intentionally
            in order to avoid unnecessary import Adamantium

            Shared app specific custom palette are defined here.
            Suno branding styles are already part of SunoTriColorSet in Adamantium
         */
        case purpleOrange(brightness: Float = 1.0)

        case pinkYellowOrange

        var colorSet: SunoTriColorSet {
            switch self {
            case .purpleOrange(let brightness):
                // Purple and Orange
                let b = brightness
                return .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)
                )

            case .pinkYellowOrange:
                return .custom(
                    .custom(0.99607843, 0.23921569, 0.47058824),
                    .custom(1.0, 0.28235295, 0.16078432),
                    .custom(1.0, 0.69803923, 0.003921569)
                )
            }
        }
    }

    @ObservedObject var coordinator = AuraView.Coordinator()
    let id: String
    let colorSet: SunoTriColorSet
    let morphSpeed: Float
    let scale: Float
    let seed: Float
    let isPaused: Bool

    public init(
        id: String,
        appPreset: AppPreset,
        morphSpeed: Float = 0.1,
        scale: Float = 1.0,
        seed: Float = .zero,
        isPaused: Bool = false
    ) {
        self.init(
            id: id,
            colorSet: appPreset.colorSet,
            morphSpeed: morphSpeed,
            scale: scale,
            seed: seed,
            isPaused: isPaused
        )
    }

    public init(
        id: String,
        colorSet: SunoTriColorSet,
        morphSpeed: Float = 0.1,
        scale: Float = 1.0,
        seed: Float = .zero,
        isPaused: Bool = false
    ) {
        self.id = id
        self.colorSet = colorSet
        self.morphSpeed = morphSpeed
        self.scale = scale
        self.seed = seed
        self.isPaused = isPaused
    }

    public var body: some View {
        #if targetEnvironment(simulator)
            Color.black
        #else
            AuraView(
                coordinator,
                id: id,
                colorSet: colorSet,
                morphSpeed: morphSpeed,
                scale: scale,
                seed: seed,
                isPaused: isPaused
            )
        #endif
    }
}
