//
//  SettingsViewModel+RingLight.swift
//  SelfieCam
//
//  Created by Matt Bruce on 1/4/26.
//

import SwiftUI
import Bedrock

// MARK: - Ring Light Settings

extension SettingsViewModel {
    
    // MARK: - Ring Size
    
    /// Ring border size in points (debounced save)
    var ringSize: CGFloat {
        get { _cachedRingSize ?? cloudSync.data.ringSize }
        set {
            _cachedRingSize = newValue
            debouncedSave(key: "ringSize") {
                self._cachedRingSize = nil
                self.updateSettings { $0.ringSize = newValue }
            }
        }
    }
    
    /// Convenience property for border width (same as ringSize)
    var borderWidth: CGFloat { ringSize }
    
    // MARK: - Ring Light Enabled/Opacity
    
    var isRingLightEnabled: Bool {
        get { cloudSync.data.isRingLightEnabled }
        set { updateSettings { $0.isRingLightEnabled = newValue } }
    }
    
    var ringLightOpacity: Double {
        get { cloudSync.data.ringLightOpacity }
        set { updateSettings { $0.ringLightOpacity = newValue } }
    }
    
    // MARK: - Light Color
    
    /// ID of the selected light color preset
    var lightColorId: String {
        get {
            let storedId = _cachedLightColorId ?? cloudSync.data.lightColorId
            return PremiumGate.get(storedId, default: Self.defaultFreeColorId, premiumValues: Self.premiumColorIds, isPremium: isPremiumUnlocked)
        }
        set {
            guard PremiumGate.canSet(newValue, premiumValues: Self.premiumColorIds, isPremium: isPremiumUnlocked) else { return }
            _cachedLightColorId = newValue
            updateSettings { $0.lightColorId = newValue }
        }
    }
    
    /// Custom color for ring light (premium feature, debounced save)
    var customColor: Color {
        get {
            _cachedCustomColor ?? Color(
                red: cloudSync.data.customColorRed,
                green: cloudSync.data.customColorGreen,
                blue: cloudSync.data.customColorBlue
            )
        }
        set {
            guard PremiumGate.canSet(isPremium: isPremiumUnlocked) else { return }
            _cachedCustomColor = newValue
            let rgb = CustomColorRGB(from: newValue)
            debouncedSave(key: "customColor") {
                self._cachedCustomColor = nil
                self.updateSettings {
                    $0.customColorRed = rgb.red
                    $0.customColorGreen = rgb.green
                    $0.customColorBlue = rgb.blue
                }
            }
        }
    }
    
    /// The currently selected light color preset
    var selectedLightColor: RingLightColor {
        get { RingLightColor.fromId(lightColorId, customColor: customColor) }
        set {
            lightColorId = newValue.id
            if newValue.isCustom {
                customColor = newValue.color
            }
        }
    }
    
    /// The actual color to use for the ring light
    var lightColor: Color {
        if lightColorId == RingLightColor.customId {
            return customColor
        }
        return selectedLightColor.color
    }
    
    /// Whether custom color is currently selected
    var isCustomColorSelected: Bool {
        lightColorId == RingLightColor.customId
    }
    
    /// Sets the custom color and selects it (PREMIUM)
    func selectCustomColor(_ color: Color) {
        guard PremiumGate.canSet(isPremium: isPremiumUnlocked) else { return }
        customColor = color
        lightColorId = RingLightColor.customId
    }
}
