//
//  Logger.swift
//  GetStarted
//
//  Created by Fabian Braach on 12.04.18.
//

public enum LogLevel: Int {
    case debug = 0, verbose, info, warning, error
}

public protocol Logger {
    
    func log(_ level: LogLevel, message: String, additionalData: [String: Any])
}

public extension Logger {
    
    func debug(message: String, additionalData: [String: Any] = [:]) {
        log(.debug, message: message, additionalData: additionalData)
    }
    
    func verbose(message: String, additionalData: [String: Any] = [:]) {
        log(.verbose, message: message, additionalData: additionalData)
    }
    
    func info(message: String, additionalData: [String: Any] = [:]) {
        log(.info, message: message, additionalData: additionalData)
    }
    
    func warning(message: String, additionalData: [String: Any] = [:]) {
        log(.warning, message: message, additionalData: additionalData)
    }
    
    func error(message: String, additionalData: [String: Any] = [:]) {
        log(.error, message: message, additionalData: additionalData)
    }
    
    func operation<O: Operation>(_ operation: O) {
        let message = """
        
        ---------------------------------------------
        Request: \(operation.response?.request.debugDescription ?? String(describing: operation.request))
        ---------------------------------------------
        Response: \(operation.response?.response?.debugDescription ?? "No response available.")
        ---------------------------------------------
        \(String(data: operation.response?.data, encoding: .utf8) ?? "No data available.")
        
        """
        log(.debug, message: message, additionalData: [:])
    }
}
