//
//  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: LocalisedElementsButton!
    @IBOutlet weak var noBtn: LocalisedElementsButton!
    @IBOutlet weak var content: LocalisedElementsLabel!
    @IBOutlet weak var mainTitle: LocalisedElementsLabel!
    
    // 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()
        updateUI()
    }
    
    func updateUI(){
        self.content.text = contentLabel.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)

        //If the title is provided then set it
        if let mainTitleText{
            self.mainTitle.text = mainTitleText.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
        }
        
        if let yesBtnText{
            self.yesBtn.setTitle(yesBtnText.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue), for: .normal)
        }
        
        if let noBtnText{
            self.noBtn.setTitle(noBtnText.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue), for: .normal)
        }
    }

    // MARK: - Button Handler

    @IBAction func closeBtnTapped(_ sender: UIButton) {
        PersistentStorage.shared.addOthersCount()
        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)
    }
    
}
