//
//  YesNoAlertVC.swift
//  WOKA
//
//  Created by MacBook Pro on 14/05/24.
//

import UIKit

enum YesNoMode{
    case yes
    case no
}

class YesNoAlertVC: UIViewController {

    @IBOutlet weak var yesBtn: UIButton!
    @IBOutlet weak var noBtn: UIButton!
    @IBOutlet weak var content: UILabel!
    
    // Properties
    var contentLabel = String()
    var yesBtnText: String?
    var noBtnText: String?
    var mainTitleText: String?
    
    // Completion block to be executed when the alert is dismissed
    var onDoneBlock: ((YesNoMode) -> Void)?

    // MARK: - View LifeCycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        yesBtn.roundCorner()
        noBtn.roundCorner()
        self.content.text = contentLabel
        
        if let yesBtnText{
            self.yesBtn.setTitle(yesBtnText, for: .normal)
        }
        if let noBtnText{
            self.yesBtn.setTitle(noBtnText, for: .normal)
        }
    }

    // MARK: - Button Handler

    @IBAction func closeBtnTapped(_ sender: UIButton) {
        self.dismiss()
    }
    
    @IBAction func btnTapped(_ sender: UIButton) {
        switch sender{
        case yesBtn:
            self.onDoneBlock?(.yes)
            self.dismiss()
        case noBtn:
            self.onDoneBlock?(.no)
            self.dismiss()
        default:
            break
        }
    }
    
    // Dismiss the alert with fade transition
    private func dismiss() {
        let transition = CATransition().fadeTransition()
        self.view.layer.add(transition, forKey: kCATransition)
        self.dismiss(animated: true)
    }
    
}
