import UIKit
import PKHUD
import AutoCatCore

extension UIViewController {
    func show(error: Error, animated: Bool = true, completion: (() -> Void)? = nil) {
        let msg = (error as NSError).displayMessage
        let alert = UIAlertController(title: msg.title, message: msg.body, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
            completion?()
        }))
        self.present(alert, animated: animated)
    }
    
    func showAlert(title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true)
    }
}

extension HUD {
    static func show(error: Error) {
        let msg = (error as NSError).displayMessage
        self.flash(.labeledError(title: msg.title, subtitle: msg.body), delay: 2.0)
    }
}
