//
//  LocalisedElements.swift
//  WOKA
//
//  Created by MacBook Pro on 29/04/24.
//

import UIKit

class LocalisedElementsLabel: UILabel {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        // This will call `awakeFromNib` in your code
        setup()
    }
    
    private func setup() {
        updateText()
        NotificationCenter.default.addObserver(self, selector: #selector(languageDidChange), name: .languageDidChange, object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self, name: .languageDidChange, object: nil)
    }
    
    @objc private func languageDidChange() {
        updateText()
    }
    
    private func updateText() {
        self.text = self.text?.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
    }
}


class LocalisedElementsButton : UIButton {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        // This will call `awakeFromNib` in your code
        setup()
    }
    
    private func setup() {
        updateText()
        NotificationCenter.default.addObserver(self, selector: #selector(updateBtnText), name: .languageDidChange, object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self, name: .languageDidChange, object: nil)
    }
    
    @objc private func updateBtnText() {
        updateText()
    }
    
    private func updateText() {
        self.setTitle(self.titleLabel?.text?.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue), for: .normal)
    }
}
