import UIKit

import SwiftUI
import Foundation
import BioPassID
import React

@available(iOS 15.0, *)
@objc(FaceSdkReactNative)
class FaceSdkReactNative: RCTEventEmitter {
    
    var hasListiners = false
    
    override func startObserving() {
        hasListiners = true
    }
    
    override func stopObserving() {
        hasListiners = false
    }
    
    override func supportedEvents() -> [String]! {
        return ["face-sdk-react-native/onFaceCapture", "face-sdk-react-native/onFaceDetected"]
    }
    
    @objc(takeFace:withResolver:withRejecter:)
    func takeFace(
        faceConfig: NSDictionary,
        resolve: @escaping RCTPromiseResolveBlock,
        reject: @escaping RCTPromiseRejectBlock
    ) {
        let config = Utils.dictionaryToFaceConfig(config: faceConfig)
        
        let cameraView = FaceView(
            with: config,
            onFaceCapture: { faceImage, faceAttributes in
                if (config.liveness.enabled) {
                    let attributesDictionary = Utils.livenessAttributesToDictionary(attributes: faceAttributes!)
                    let onFaceCaptureDictionary = ["image": faceImage.imageBase64, "faceAttributes": attributesDictionary]
                    
                    if (self.hasListiners) {
                        self.sendEvent(withName: "face-sdk-react-native/onFaceCapture", body: onFaceCaptureDictionary)
                    }
                } else {
                    let onFaceCaptureDictionary = ["image": faceImage.imageBase64, "faceAttributes": nil]
                    
                    if (self.hasListiners) {
                        self.sendEvent(withName: "face-sdk-react-native/onFaceCapture", body: onFaceCaptureDictionary)
                    }
                }
            },
            onFaceDetected: { faceAttributes in
                let attributesDictionary = Utils.livenessAttributesToDictionary(attributes: faceAttributes)
                let onFaceDetectedDictionary = ["faceAttributes": attributesDictionary]
                
                if (self.hasListiners) {
                    self.sendEvent(withName: "face-sdk-react-native/onFaceDetected", body: onFaceDetectedDictionary)
                }
            }
        )
        
        DispatchQueue.main.async {
            let rootViewController: UIViewController! = UIApplication.shared.delegate?.window??.rootViewController
            
            let contentView = UIHostingController(rootView: cameraView)
            contentView.modalPresentationStyle = .fullScreen
            rootViewController.present(contentView, animated: true, completion: nil)
        }
        
        resolve(nil)
    }
    
    @objc(startRecognition:withResolver:withRejecter:)
    func startRecognition(
        licenseKey: NSString,
        resolve: @escaping RCTPromiseResolveBlock,
        reject: @escaping RCTPromiseRejectBlock
    ) {
        resolve(nil)
    }
    
    @objc(closeRecognition:withRejecter:)
    func closeRecognition(
        resolve: @escaping RCTPromiseResolveBlock,
        reject: @escaping RCTPromiseRejectBlock
    ) {
        resolve(nil)
    }
    
    @objc(extract:withResolver:withRejecter:)
    func extract(
        artifact: NSString,
        resolve: @escaping RCTPromiseResolveBlock,
        reject: @escaping RCTPromiseRejectBlock
    ) {
        resolve(nil)
    }
    
    @objc(verify:withTemplateB:withResolver:withRejecter:)
    func verify(
        templateA: NSString,
        templateB: NSString,
        resolve: @escaping RCTPromiseResolveBlock,
        reject: @escaping RCTPromiseRejectBlock
    ) {
        resolve(nil)
    }
}
