         let reachability = sender.object as! Reachability
         
         if reachability.isReachable {
-            //on first change, don't react to the notification if online
+            //on first change, don't react to the notification of connection change
             if firstChange == true {
                 firstChange = false
                 return

        }catch{
            print("could not start reachability notifier")
        }
    }
    
    deinit {

            }
            NotificationCenter.default.post(Notification(name: kInternetIsBackNotification))
            if reachability.isReachableViaWiFi {
                reachabilityNotifyDelegates(internetConnection: true, via: .wifi)
            } else {
                reachabilityNotifyDelegates(internetConnection: true, via: .cellular)
            }
        } else {
            NotificationCenter.default.post(Notification(name: kNoInternetNotification))
            reachabilityNotifyDelegates(internetConnection: false, via: .noInternet)
        }
    }
    
    public func isReachable()->Bool{
        return reachability.isReachable
    }
    
    //MARK: - delegates handling
    public func addDelegate(_ delegate:SimpleApiDelegate) {
        self.delegates.append(delegate)
    }
    
    public func removeDelegate(_ delegate:SimpleApiDelegate) {
        if let index = self.delegates.index(where: { (item) -> Bool in item === delegate }) {
            self.delegates.remove(at: index)
        }
    }
    
    private func reachabilityNotifyDelegates(internetConnection:Bool, via:ConnectionType) {
        for delegate in delegates {
            delegate.reachabilityChanged(sender: self, isReachable: internetConnection, via: via)
        }
    }
    
    //MARK: - private saving/loading
    private func loadFromRealm(){
        isLoading = true
        
        let object = DataManager.shared.get(type: SimpleApiRealm.self, identifier: "SimpleApiRealm")
        autoSaveToRealm = object.autoSaveToRealm
        callLastApiCallAfterInternetComesBack = object.callLastApiCallAfterInternetComesBack
        defaultMethod = object.defaultMethod
        defaultParamEncoding = object.defaultParamEncoding
        defaultHeaders = object.defaultHeaders
        
        isLoading = false
    }
    
    private func saveToRealm(){
        if isLoading { return }
        
        let object = SimpleApiRealm()
        object.autoSaveToRealm = autoSaveToRealm
        object.callLastApiCallAfterInternetComesBack = callLastApiCallAfterInternetComesBack
        object.defaultMethod = defaultMethod
        object.defaultParamEncoding = defaultParamEncoding
        object.defaultHeaders = defaultHeaders
        DataManager.shared.save(object: object)
    }
}

//MARK: - protocol
public protocol SimpleApiDelegate: class {
    func reachabilityChanged(sender:SimpleApi, isReachable:Bool, via:ConnectionType)
}

//MARK: - enums
public enum ConnectionType {
    case wifi
    case cellular
    case noInternet
}

public enum ParamsEncoding: Int {
    case standard
    case httpBody
}
