//  AlertCustomVC.swift
//  WOKA
//
//  Created by MacBook Pro on 07/05/24.
//

import UIKit

class AlertCustomVC: UIViewController {
    
    // Completion block to be executed when the alert is dismissed
    var onDoneBlock: ((Bool) -> Void)?
    
    // Outlets
    @IBOutlet weak var contentTextLbl: LocalisedElementsLabel!
    @IBOutlet weak var maintitle: UILabel!
    @IBOutlet weak var outsideView: UIView!
    @IBOutlet weak var yesBtn: UIButton!
    
    // Properties
    var contentLabel = String()
    var yesBtnText: String?
    var mainTitleText: String?
    
    var outsideViewTapGestureEnabled = true
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Set button names and content
        setBtnName()
        
        // Dismiss the alert when tapping outside
        if outsideViewTapGestureEnabled == true{
            outsideView.addTapGesture {
                self.dismiss(animated: true)
            }
        }
    }

    
    // Set button names and content
    func setBtnName() {
        self.contentTextLbl.isHidden = false
        self.contentTextLbl.text = contentLabel.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
        if let yesBtnText = yesBtnText {
            self.yesBtn.setTitle(yesBtnText, for: .normal)
        }else{
            self.yesBtn.setTitle("Ok,Got it.".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue), for: .normal)
        }
        
        if let mainTitleText = mainTitleText {
            self.maintitle.text = mainTitleText
        }
    }
    
    // Action when done button is tapped
    @IBAction func doneBtnTapped(_ sender: UIButton) {
        self.dismiss()
        // Execute completion block if provided
         self.onDoneBlock?(true)
    }
    
    // Action when cancel button is tapped
    @IBAction func cancelBtnTapped(_ sender: UIButton) {
        self.dismiss()
    }
    
    // Dismiss the alert with fade transition
    private func dismiss() {
        let transition = CATransition().fadeTransition()
        self.view.layer.add(transition, forKey: kCATransition)
        self.dismiss(animated: true)
    }
}

// MARK: - Extension for CATransition to create fade transition

extension CATransition {
    
    func fadeTransition() -> CATransition {
        let transition = CATransition()
        transition.duration = 0.2
        transition.type = CATransitionType.fade
        transition.subtype = CATransitionSubtype.fromTop
        return transition
    }
}
