import UIKit
import IQKeyboardManagerSwift

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
  
  func application(_ application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
      
      initializeDependencies()
      
      @Inject var handleAppLanguageUseCase: HandleAppLanguageUseCase
      handleAppLanguageUseCase.execute()
      
      if let savedFolders = UserDefaults.standard.object(forKey: "folders") as? Data {
          print("nooo!!")
      } else {
          AppConfigurator().setDefaults()
      }
      IQKeyboardManager.shared.enable = true
      IQKeyboardManager.shared.resignOnTouchOutside = true
      IQKeyboardManager.shared.toolbarConfiguration.previousNextDisplayMode = .alwaysShow
      IQKeyboardManager.shared.toolbarConfiguration.tintColor = .mainBlue

    return true
  }
  
  // MARK: UISceneSession Lifecycle
  
  func application(_ application: UIApplication,
                   configurationForConnecting connectingSceneSession: UISceneSession,
                   options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  }
  
  func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  }
    
    // MARK: - Set Language
//    private func setAppLanguage() {
//        if let language = UserDefaults.standard.string(forKey: Keys.appLanguage) {
//            if language == .en {
//                setEnglishLanguage()
//            } else {
//                setArabicLanguage()
//            }
//        } else {
//            if let deviceLanguage = Bundle.main.preferredLocalizations.first {
//                if deviceLanguage == "ar" || deviceLanguage == "ar-KW" {
//                    setArabicLanguage()
//                } else if deviceLanguage == "en" {
//                    setEnglishLanguage()
//                }
//            } else {
//                setEnglishLanguage()
//            }
//        }
//    }
//    
//    private func setArabicLanguage() {
//        LanguageManager.shared.setLanguage(language: .ar)
//        LanguageManager.shared.defaultLanguage = .ar
//        UserDefaults.standard.setValue(String.ar, forKey: Keys.appLanguage)
//    }
//    
//    private func setEnglishLanguage() {
//        LanguageManager.shared.setLanguage(language: .en)
//        LanguageManager.shared.defaultLanguage = .en
//        UserDefaults.standard.setValue(String.en, forKey: Keys.appLanguage)
//    }
    
    
}

struct AppConfigurator {
  // Privacy policies and term links
    enum Settings: String {
        case terms = "https://mistergrizzly.com/terms-and-conditions/"
        case privacy = "https://mistergrizzly.com/privacy-policy/"
    }
    
    enum Folders: String {
        case AllDocument = "AllDocument"
    }
    
    struct Folder: Codable, Hashable {
        var name: String
        var savedName: String
        var isSelected: Bool
    }
    
    func setDefaults()  {
        let allDocs = AppConfigurator.Folder(name: "All Documents", savedName: "all_documents", isSelected: true)
        let passports = AppConfigurator.Folder(name: "Passports", savedName: "passports", isSelected: false)
        let contracts = AppConfigurator.Folder(name: "Contracts", savedName: "contracts", isSelected: false)
        let identifiers = AppConfigurator.Folder(name: "Identifiers", savedName: "identifiers", isSelected: false)
        
        let folders = [allDocs, passports, contracts, identifiers]
        
        if let encoded = try? JSONEncoder().encode(folders) {
            UserDefaults.standard.set(encoded, forKey: "folders")
        }
    }
    
    func getFolders() -> [Folder] {
        if let savedFolders = UserDefaults.standard.object(forKey: "folders") as? Data {
            if let loadedFolders = try? JSONDecoder().decode([Folder].self, from: savedFolders) {
                return loadedFolders
            } else {
                return []
            }
        }
        return []
    }
    
  static let ThumbnailsFolderName = ".ThumbnailImages"
}

let deviceType = UIDevice.current.userInterfaceIdiom

extension Bundle {
  class var appName: String {
    let bundleDisplayname = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String
    let bundleName = Bundle.main.infoDictionary![kCFBundleNameKey as String] as! String
    
    return bundleDisplayname ?? bundleName
  }
}

extension AppDelegate {
    
    private func initializeDependencies() {
        _ = DependencyManager()
    }
}