//
//  Extension.swift
//  TunnelServices
//
//  Created by Harry on 2020/11/06.
//  Copyright © 2020 Harry. All rights reserved.
//

import UIKit

import CNIOBoringSSL
import NIOSSL

extension FileManager {
   
   public static func fileExists(url : URL?) -> Bool {
      
      let   stFileManager              = FileManager.default;
      var   isDirectory    : ObjCBool  = false;
      var   isExists       : Bool      = false;
      
      isExists = stFileManager.fileExists(atPath: url!.path, isDirectory: &isDirectory);
      
      return (isExists && !isDirectory.boolValue);
   }
   
   public static func dirExists(url : URL?) -> Bool {
      
      let   stFileManager              = FileManager.default;
      var   isDirectory    : ObjCBool  = false;
      var   isExists       : Bool      = false;
      
      isExists = stFileManager.fileExists(atPath: url!.path, isDirectory: &isDirectory);
      
      return (isExists && isDirectory.boolValue);
   }
   
   public static func copyItem(atPath srcPath: String, toPath dstPath: String) {
      
      let   stFileManager  = FileManager.default;
      
      try? stFileManager.copyItem(atPath: srcPath, toPath: dstPath);
      
      return;
   }
}

extension String {
   public func isIPAddress() -> Bool {
      // We need some scratch space to let inet_pton write into.
      var ipv4Addr = in_addr()
      var ipv6Addr = in6_addr()
      
      return self.withCString { ptr in
         return inet_pton(AF_INET, ptr, &ipv4Addr)  == 1 ||
                inet_pton(AF_INET6, ptr, &ipv6Addr) == 1
      }
   }
}

//extension NIOSSLPrivateKey {
//   public func getRef () -> UnsafeMutablePointer<EVP_PKEY> {
//      return self.ref;
//   }
//}
