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

import SwiftUI
import Bedrock
import MijickCamera

// MARK: - Camera Settings

extension SettingsViewModel {
    
    // MARK: - Camera Position
    
    /// Raw camera position string for comparison (avoids MijickCamera enum issues)
    var cameraPositionRaw: String {
        cloudSync.data.cameraPositionRaw
    }
    
    var cameraPosition: CameraPosition {
        get {
            // Access the data through cloudSync to ensure observation is tracked
            let raw = cloudSync.data.cameraPositionRaw
            return raw == "back" ? .back : .front
        }
        set {
            let rawValue = newValue == .back ? "back" : "front"
            
            // Use updateSettings to ensure modificationCount is incremented and observers are notified
            updateSettings { $0.cameraPositionRaw = rawValue }
        }
    }
    
    // MARK: - Flash Mode
    
    var flashMode: CameraFlashMode {
        get { CameraFlashMode(rawValue: cloudSync.data.flashModeRaw) ?? .off }
        set { updateSettings { $0.flashModeRaw = newValue.rawValue } }
    }
    
    /// Whether flash is synced with ring light color (PREMIUM)
    var isFlashSyncedWithRingLight: Bool {
        get { PremiumGate.get(cloudSync.data.isFlashSyncedWithRingLight, default: false, isPremium: isPremiumUnlocked) }
        set { 
            guard PremiumGate.canSet(isPremium: isPremiumUnlocked) else { return }
            updateSettings { $0.isFlashSyncedWithRingLight = newValue } 
        }
    }
    
    // MARK: - HDR Mode (Premium)
    
    /// HDR mode setting (PREMIUM)
    var hdrMode: CameraHDRMode {
        get { 
            let stored = CameraHDRMode(rawValue: cloudSync.data.hdrModeRaw) ?? .off
            return PremiumGate.get(stored, default: .off, isPremium: isPremiumUnlocked)
        }
        set { 
            guard PremiumGate.canSet(isPremium: isPremiumUnlocked) else { return }
            updateSettings { $0.hdrModeRaw = newValue.rawValue } 
        }
    }
    
    // MARK: - Photo Quality (High is Premium)
    
    /// Photo quality setting (high is PREMIUM)
    var photoQuality: PhotoQuality {
        get { 
            let stored = PhotoQuality(rawValue: cloudSync.data.photoQualityRaw) ?? PhotoQuality.high
            return PremiumGate.get(stored, default: .medium, premiumValues: Self.premiumPhotoQualities, isPremium: isPremiumUnlocked)
        }
        set { 
            guard PremiumGate.canSet(newValue, premiumValues: Self.premiumPhotoQualities, isPremium: isPremiumUnlocked) else { return }
            updateSettings { $0.photoQualityRaw = newValue.rawValue } 
        }
    }
    
    // MARK: - Center Stage (Premium)
    
    /// Whether Center Stage is enabled (PREMIUM)
    var isCenterStageEnabled: Bool {
        get { PremiumGate.get(cloudSync.data.isCenterStageEnabled, default: false, isPremium: isPremiumUnlocked) }
        set { 
            guard PremiumGate.canSet(isPremium: isPremiumUnlocked) else { return }
            updateSettings { $0.isCenterStageEnabled = newValue } 
        }
    }
    
    // MARK: - Timer (5s/10s are Premium)
    
    /// Selected timer option (5s and 10s are PREMIUM)
    var selectedTimer: TimerOption {
        get { 
            let stored = TimerOption(rawValue: cloudSync.data.selectedTimerRaw) ?? .off
            return PremiumGate.get(stored, default: .three, premiumValues: Self.premiumTimerOptions, isPremium: isPremiumUnlocked)
        }
        set { 
            guard PremiumGate.canSet(newValue, premiumValues: Self.premiumTimerOptions, isPremium: isPremiumUnlocked) else { return }
            updateSettings { $0.selectedTimerRaw = newValue.rawValue } 
        }
    }
}
