//
/* NOTICE: All information contained herein is, and remains the property of Brightinsight Inc. or its customer. 
The intellectual and technical concepts contained herein are proprietary to Brightinsight Inc. or its customer and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret or copyright law.
Dissemination of this information or reproduction of this material is strictly forbidden unless prior written permission is obtained from Brightinsight Inc. or its customer. Access to the source code contained herein is hereby forbidden to anyone except current Brightinsight Inc. employees, managers or contractors who have executed
Confidentiality and Non-disclosure agreements explicitly covering such access.
*/
/**
* System Name : BISDKCore
* Component ID : BISDKError
* Description : 
* Author: Vitor Cesco
* Copyright © : Brightinsight, Inc.
* -----------------------
* Revision Change
* -----------------------
* Author            Date             Version       Change-Description
*========================================================================================================
*/

import Foundation

public enum BISDKError: Error, LocalizedError {
    case notStartedSDK
    case alreadyStartedSDK
    case unconfiguredSDK
    case moduleNotFound
    case unauthorized(sdkPrefix: String)
    case runtimeError(sdkPrefix: String, errorMessage: String?)
    case platformError(data: Data?, response: HTTPURLResponse, error: Error?)
    
    public var details: String { errorDescription ?? "" }
    public var severity: String {
        switch self {
        case .platformError:
            return "PLATFORM_ERROR"
        default:
            return "ERROR"
        }
    }
    public var code: String {
        switch self {
        case .notStartedSDK, .alreadyStartedSDK, .unconfiguredSDK, .moduleNotFound:
            return prefixedCode(BISDKError.coreModulePrefix)
        case .unauthorized(let sdkPrefix):
            return prefixedCode(sdkPrefix)
        case .runtimeError(let sdkPrefix, _):
            return prefixedCode(sdkPrefix)
        case .platformError(_, let response, _):
            return "\(response.statusCode)"
        }
    }
    
    public var errorDescription: String? {
        switch self {
        case .notStartedSDK:
            return "This module is not initialized."
        case .alreadyStartedSDK:
            return "This module has already been initialized."
        case .unconfiguredSDK:
            return "This module is not configured."
        case .moduleNotFound:
            return "This module could not be found."
        case .unauthorized:
            return "You must authenticate before using this action."
        case .runtimeError(_, let errorMessage):
            return errorMessage ?? "Unknown runtime error."
        case .platformError(let data, _, let error):
            guard let data = data else { return error?.localizedDescription ?? "Unknown error." }
            
            return String(data: data, encoding: .utf8)
        }
    }
}
extension BISDKError {
    static var coreModulePrefix: String { "CORESDK" }
    
    var rawCode: String {
        switch self {
        case .notStartedSDK:
            return "NOT_INITIALIZED"
        case .alreadyStartedSDK:
            return "ALREADY_INITIALIZED"
        case .unconfiguredSDK:
            return "NOT_CONFIGURED"
        case .moduleNotFound:
            return "NOT_FOUND"
        case .unauthorized:
            return "AUTH_REQUIRED"
        case .runtimeError:
            return "INTERNAL_ERROR"
        case .platformError:
            return ""
        }
    }
    func prefixedCode(_ string: String) -> String {
        return "\(string)_\(rawCode)"
    }
}
