//
//  CameraProtocols.swift
//  CameraTester
//
//  Created by Matt Bruce on 1/2/26.
//

import AVFoundation
import SwiftUI

/// Protocol defining camera session management capabilities
protocol CameraSessionManaging: AnyObject {
    var isSessionRunning: Bool { get }

    func setupCamera() async
    func startSession() async
    func stopSession()
}

/// Protocol defining photo capture capabilities
protocol PhotoCapturing: AnyObject {
    var capturedImage: UIImage? { get }

    func takePhoto() async throws
}

/// Protocol defining camera permission handling
protocol CameraPermissionHandling: AnyObject {
    func requestCameraPermission() async -> Bool
}

/// Protocol defining camera controls (flash, zoom, switch camera)
protocol CameraControllingAdvanced: AnyObject {
    var currentCameraPosition: AVCaptureDevice.Position { get }
    var isFlashAvailable: Bool { get }
    var currentFlashMode: AVCaptureDevice.FlashMode { get }
    var zoomFactor: CGFloat { get }
    var maxZoomFactor: CGFloat { get }

    func switchCamera() async
    func setFlashMode(_ mode: AVCaptureDevice.FlashMode)
    func setZoomFactor(_ factor: CGFloat)
}

/// Protocol defining Center Stage control capabilities
/// Center Stage automatically keeps users centered in the frame using the front camera
protocol CenterStageControlling {
    /// Whether Center Stage is currently available on this device
    var isCenterStageAvailable: Bool { get }
    
    /// Whether Center Stage is currently enabled
    var isCenterStageEnabled: Bool { get }
    
    /// Enable or disable Center Stage
    /// - Parameter enabled: Whether to enable Center Stage
    /// - Throws: If Center Stage cannot be configured
    func setCenterStageEnabled(_ enabled: Bool) throws
}

// MARK: - Center Stage Default Implementation

extension CenterStageControlling {
    var isCenterStageAvailable: Bool {
        AVCaptureDevice.isCenterStageEnabled || canEnableCenterStage
    }
    
    private var canEnableCenterStage: Bool {
        guard let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front) else {
            return false
        }
        // Check if the device supports center stage by checking active format
        return device.activeFormat.isCenterStageSupported
    }
}

/// Combined protocol for full camera functionality
protocol CameraControlling: CameraSessionManaging, PhotoCapturing, CameraPermissionHandling, CameraControllingAdvanced {}

// MARK: - Default Implementations

extension CameraSessionManaging {
    func startSession() async {
        // Default implementation can be provided if needed
    }

    func stopSession() {
        // Default implementation can be provided if needed
    }
}

extension PhotoCapturing {
    func takePhoto() async throws {
        // Default implementation can be provided if needed
    }
}

extension CameraPermissionHandling {
    func requestCameraPermission() async -> Bool {
        await withCheckedContinuation { continuation in
            AVCaptureDevice.requestAccess(for: .video) { granted in
                continuation.resume(returning: granted)
            }
        }
    }
}

extension CameraControllingAdvanced {
    func switchCamera() async {
        // Default implementation can be provided if needed
    }

    func setFlashMode(_ mode: AVCaptureDevice.FlashMode) {
        // Default implementation can be provided if needed
    }

    func setZoomFactor(_ factor: CGFloat) {
        // Default implementation can be provided if needed
    }
}
