//
//  Alert.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 16/11/21.
//

import Foundation
import UIKit

struct Alert {
    
    static func showAlert(on vc:UIViewController, with title:String, message:String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: AppConstant.Ok, style: .default, handler: nil))
        DispatchQueue.main.async {
            vc.present(alert, animated: true, completion: nil)
        }
    }
    
    static func showAlertWithAction(vc:UIViewController,
                                    title: String,
                                    message: String,
                                    alertStyle:UIAlertController.Style,
                                    actionTitles:[String],
                                    actionStyles:[UIAlertAction.Style],
                                    actions: [((UIAlertAction) -> Void)]){
        
        let alertController = UIAlertController(title: title, message: message, preferredStyle: alertStyle)
        for(index, indexTitle) in actionTitles.enumerated(){
            let action = UIAlertAction(title: indexTitle, style: actionStyles[index], handler: actions[index])
            alertController.addAction(action)
        }
        DispatchQueue.main.async {
            vc.present(alertController, animated: true, completion: nil)
        }
    }
    
//    //Usage Alert
//    self.showAlertWithAction(title: "alert", message: "add your message", alertStyle: .alert, actionTitles: ["Okay", "Cancel"], actionStyles: [.default, .cancel], actions: [{_ in
//        print("okay click")
//    },
//    {_ in
//        print("cancel click")
//    }
//    ])
//    //Usage ActionSheet
//    self.showAlertWithAction(title: "actionsheet",
//    message: "add your message",
//    alertStyle: .actionSheet,
//    actionTitles: ["Okay", "Cancel"],
//    actionStyles: [.default, .cancel],
//    actions: [
//    {_ in
//    print("okay click")
//    },
//    {_ in
//    print("cancel click")
//    }
//    ])
    
    //Show Common Alert like internet failure
    static func showInternetFailureAlert(on vc:UIViewController){
        showAlertWithAction(vc: vc, title: AppConstant.internetAlertTitle, message: AppConstant.internetAlertMessage, alertStyle: .alert, actionTitles: ["Cancel", "Open Setting"], actionStyles: [.cancel, .default], actions: [
            {_ in
                print("cancel click")
            },
            {_ in
                if let url = URL.init(string: UIApplication.openSettingsURLString) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
            }
        ])
    }
    //Show Specific Alerts like Valid or Invalid
    static func showInvalidAlert(on vc:UIViewController){
        showAlert(on: vc, with: AppConstant.defaultTitle, message: AppConstant.defaultMsg)
    }
    
}

