//
//  Identomat.swift
//  Identomat
//
//  Created by Oto Siradze on 2/10/21.
//  Copyright © 2021 Facebook. All rights reserved.
//

import Foundation
import UIKit
import identomat

@objc(Identomat)

class Identomat: NSObject, RCTBridgeModule{
  
  static func moduleName() -> String!{
    return "Identomat";
  }
  
  static func requirement () -> Bool {
   return true;
  }
  
    var successCallback: RCTResponseSenderBlock? = nil;
  
  @objc
    func start(_ identomatConfig:String, withSession sessionId : String) -> Void {
        DispatchQueue.main.async {
            
            let topController = UIApplication.topViewController()
            let data: Data? = identomatConfig.data(using: .utf8)
            let json = (try? JSONSerialization.jsonObject(with: data!, options: [])) as! [String:Any]
            
            let flags = json["flags"] as! [String : Any]
            let documentTypes = self.parseDocuments(documentTypes: json["docTypes"] as! [String:Any])
                
            IdentomatManager.getInstance().setUp(token: sessionId, flags: flags, documentTypes: documentTypes)
            if let color = json["colors"] as? [String : String]{
                IdentomatManager.getInstance().setColors(color: color)
            }
            if let strings = json["strings"] as? [String : Any?]{
                IdentomatManager.getInstance().setStrings(dict: strings)
            }
            if let fonts = json["fonts"] as? [String : Any?]{
                if let head1 = fonts["Head1Font"] as? String{
                    IdentomatManager.getInstance().setHead1Font(fontname: head1)
                }
                if let head2 = fonts["Head2Font"] as? String{
                    IdentomatManager.getInstance().setHead2Font(fontname: head2)
                }
                if let body = fonts["BodyFont"] as? String{
                    IdentomatManager.getInstance().setBodyFont(fontname: body)
                }
            }
            //start identomat
            let identomatView = IdentomatManager.getInstance().getIdentomatView()
            identomatView.modalPresentationStyle = .fullScreen
            topController!.present(identomatView, animated: true)
            
        }
    }
    @objc
    func callBack(_ successCallback: @escaping RCTResponseSenderBlock){
        self.successCallback = successCallback;
        IdentomatManager.getInstance().callBack {
            print("process is done we are good")
            self.successCallback!([NSNull(),"done"])
        }
        
    }
    
    func parseDocuments(documentTypes : [String:Any]) -> [Int]{
        var docTypes : [Int] = []
        if let documentCard = documentTypes["id_card"] as? Bool{
            if(documentCard){
                docTypes.append(Document.CARD)
            }
        }
        if let documentPassport = documentTypes["passport"] as? Bool{
            if(documentPassport){
                docTypes.append(Document.PASSPORT)
            }
        }
        if let documentLicense = documentTypes["driving_license"] as? Bool{
            if(documentLicense){
                docTypes.append(Document.LICENSE)
            }
        }
        if let documentResidence = documentTypes["residence_permit"] as? Bool{
            if(documentResidence){
                docTypes.append(Document.RESIDENCE)
            }
        }
        return docTypes
    }
 
}
class Document {
    public static let CARD : Int = 0
    public static let LICENSE : Int = 1
    public static let RESIDENCE : Int = 2
    public static let PASSPORT : Int = 3
}


extension UIApplication {
    class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let navigationController = controller as? UINavigationController {
            return topViewController(controller: navigationController.visibleViewController)
        }
        if let tabController = controller as? UITabBarController {
            if let selected = tabController.selectedViewController {
                return topViewController(controller: selected)
            }
        }
        if let presented = controller?.presentedViewController {
            return topViewController(controller: presented)
        }
        return controller
    }
}
