import Foundation
import UIKit

/// unit members view controller
class ClenoveJednotkyViewController: UIViewController {
    
    var viewModel: ClenoveJednotkyViewModelProtocol?
    
    /// initialization function
    /// - Parameter viewModel: unit members view model
    init(viewModel: ClenoveJednotkyViewModelProtocol) {
        super.init(nibName: nil, bundle: nil)
        self.viewModel = viewModel
        self.viewModel?.delegate = self
        let view = ClenoveJednotkyView()
        view.viewDelegate = self
        self.view = view
        self.viewModel?.refreshData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// function that gets called before the view appears
    /// - Parameter animated: animated
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        navigationController?.setNavigationBarHidden(false, animated: false)
        title = "Členové Jednotky"
    }
}

extension ClenoveJednotkyViewController: ClenoveJednotkyViewModelDelegate {
    
    /// function that updates the view model
    func didUpdate() {
        DispatchQueue.main.async {
            guard let view = self.view as? ClenoveJednotkyView else { return }
            view.viewModel = self.viewModel
        }
    }
}

extension ClenoveJednotkyViewController: ClenoveJednotkyViewDelegate {
    
    /// function that gets called when the navigation segues to person detail
    /// - Parameter id: id for person
    func segueToPerson(id: String) {
        let controller = OsobaViewController()
        controller.viewModel = OsobaViewModel(id: id)
        navigationController?.pushViewController(controller, animated: true)
    }
}
