import UIKit

/// person view controller
class OsobaViewController: UIViewController {

    var viewModel: OsobaViewModel? {
        didSet {
            guard let viewModel = viewModel else { return }
            viewModel.delegate = self
        }
    }
    
    /// initialize function
    init() {
        super.init(nibName: nil, bundle: nil)
        let view = OsobaView()
        self.view = view
    }
    
    /// function that gets called before te view appears
    /// - Parameter animated: animated
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: false)
        title = "Detail Osoby"
        viewModel?.reloadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension OsobaViewController: OsobaViewModelDelegate {
    
    /// function for updating view model
    func didUpdate() {
        DispatchQueue.main.async {
            guard let view = self.view as? OsobaView else { return }
            view.viewModel = self.viewModel
        }
    }
}
