import Foundation
import UIKit

/// unit view controller
class JednotkyViewController: UIViewController {
    
    var viewModel: JednotkaViewModelProtocol?
    
    /// initialization function
    /// - Parameter viewModel: view model for unit
    init(viewModel: JednotkaViewModelProtocol) {
        super.init(nibName: nil, bundle: nil)
        self.viewModel = viewModel
        self.viewModel?.delegate = self
        let view = JednotkyView()
        view.delegate = self
        view.headerView.delegate = 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)
        if viewModel?.isFirst() ?? false {
            navigationController?.setNavigationBarHidden(true, animated: false)
        } else {
            navigationController?.setNavigationBarHidden(false, animated: false)
        }
    }
}

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

extension JednotkyViewController: JednotkyViewDelegate {
    
    /// function that gets called when the navigation segues to unit detail
    /// - Parameter id: id for unit
    func segueToUnit(id: String) {
        let viewModel = JednotkaViewModel(id: id, first: false)
        let controller = JednotkyViewController(viewModel: viewModel)
        navigationController?.pushViewController(controller, animated: true)
    }
    
    /// 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)
    }
}

extension JednotkyViewController: JednotkaHeaderViewDelegate {
    
    /// function that pushes the members of unit view onto navigation stack
    func osobyTap() {
        guard let id = viewModel?.detail?.ID else { return }
        let viewModel = ClenoveJednotkyViewModel(id: id)
        let controller = ClenoveJednotkyViewController(viewModel: viewModel)
        navigationController?.pushViewController(controller, animated: true)
    }
    
    /// function that pushes the registration view onto navigation stack
    func registraceTap() {
        guard let id = viewModel?.detail?.ID else { return }
        let viewModel = RegistraceViewModel(id: id)
        let controller = RegistraceViewController(viewModel: viewModel)
        navigationController?.pushViewController(controller, animated: true)
    }
}
