//
//  File.swift
//  WOKA
//
//  Created by MacBook Pro on 06/05/24.
//

import UIKit

class Utilities{
    let activityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.medium)

//    static func setupProgressHUD() {
//        let loaderColor = UIColor.green
//        let backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
//
//        SVProgressHUD.setInfoImage(UIImage(named: "AppLogo")!)
//        SVProgressHUD.setBackgroundColor(backgroundColor)
//        SVProgressHUD.setForegroundColor(loaderColor)
//        // don't let user press anything on screen while SVProgessHUD is active
//        SVProgressHUD.setDefaultMaskType(.black)
//    }
    
    static func startProgressHUD(progress: Float? = nil, msg : String? = nil) {
        LLSpinner.spin(text: (msg != "" && msg != nil) ? msg : "Please wait...".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue))
//        if let progress = progress {
//            SVProgressHUD.showProgress(progress)
//        } else {
//            SVProgressHUD.show()
//        }
    }

    
    static func dismissProgressHUD() {
        DispatchQueue.main.async {
            LLSpinner.stop()
        }
    }

    static func stopActivity(view : UIView){
        Utilities().activityIndicator.stopAnimating()
    }
    
    static func alertWithBtn(title : String , msgBody : String, okBtnStr : String?,vc : UIViewController){
        let alert = UIAlertController(title: title, message: msgBody, preferredStyle: .alert)

        let titleAttrString = NSMutableAttributedString(string: title != "" ? (title + "\n") : "", attributes: [NSAttributedString.Key.font: FontCustom.shareInstance.customFont(fontName: .Exo2_Regular, size: 16) as Any])
        alert.setValue(titleAttrString, forKey: "attributedTitle")

        
        var messageMutableString = NSMutableAttributedString()
        messageMutableString = NSMutableAttributedString(string: msgBody as String, attributes: [NSAttributedString.Key.font: FontCustom.shareInstance.customFont(fontName: .Exo2_Medium, size: 16)])
        messageMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.black, range: NSRange(location:0,length:msgBody.count))
        alert.setValue(messageMutableString, forKey: "attributedMessage")
        
        let okAction = UIAlertAction(title: okBtnStr ?? "OK", style: .default, handler: nil)
        okAction.setValue(UIColor.appColor(.TextDarkBlue), forKey: "titleTextColor")

        alert.addAction(okAction)

        vc.present(alert, animated: true, completion: nil)
    }

    static func alertWithBtnCompletion(title : String , msgBody : String, okBtnStr : String?,vc : UIViewController ,onCompletion : @escaping (Bool) -> Void){
        let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
        let okAction = UIAlertAction(title: okBtnStr ?? "OK", style: .default, handler: {_ in
            onCompletion(true)
        })
        
        let messageAttributes = [NSAttributedString.Key.font: FontCustom.shareInstance.customFont(fontName: .Exo2_Medium, size: 15), NSAttributedString.Key.foregroundColor: UIColor.black]
        let messageString = NSAttributedString(string: msgBody, attributes: messageAttributes)

        okAction.setValue(UIColor.appColor(.TextDarkBlue), forKey: "titleTextColor")

        alert.addAction(okAction)
        alert.setValue(messageString, forKey: "attributedMessage")

        vc.present(alert, animated: true, completion: nil)
    }
    
    static func alertWithBtnCancelCompletion(title: String, msgBody: String, okBtnStr: String?, vc: UIViewController, onCompletion: @escaping (Bool) -> Void) {
        let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
        
        // OK Action
        let okAction = UIAlertAction(title: okBtnStr ?? "OK", style: .default, handler: {_ in
            onCompletion(true)
        })
        okAction.setValue(UIColor.appColor(.TextDarkBlue), forKey: "titleTextColor")
        alert.addAction(okAction)
        
        // Cancel Action
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {_ in
            onCompletion(false)
        })
        cancelAction.setValue(UIColor.red, forKey: "titleTextColor")
        alert.addAction(cancelAction)

        // Custom Message
        let messageAttributes = [NSAttributedString.Key.font: FontCustom.shareInstance.customFont(fontName: .Exo2_Medium, size: 15), NSAttributedString.Key.foregroundColor: UIColor.black]
        let messageString = NSAttributedString(string: msgBody, attributes: messageAttributes)
        alert.setValue(messageString, forKey: "attributedMessage")

        // Presenting Alert
        vc.present(alert, animated: true, completion: nil)
    }

    
    static func reportBug(message: String, line: Int = #line, file: String = #file) {
        print("Line: \(line): File \(file): \(message)")
        //TODO: Send report to ferofly
    }
    
    static func alert(line: Int = #line, file: String = #file, title: String, message: String, viewController: UIViewController, toastTime: TimeInterval = 0, onCompletion: (() -> Void)? = nil) {
        print("Line: \(line): File \(file): \(title): \(message)")
        
        let alertVC = UIAlertController(title: title, message: message, preferredStyle: .alert)
        
        if toastTime == 0 {
            let action = UIAlertAction(title: "Ok", style: .default) { (action) in
                alertVC.dismiss(animated: true, completion: onCompletion)
            }
            alertVC.addAction(action)
        }
        
        DispatchQueue.main.async {
            viewController.present(alertVC, animated: true, completion: nil)
            
            if toastTime > 0 {
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + toastTime) {
                    alertVC.dismiss(animated: true, completion: onCompletion)
                }
            }
        }
    }
}
