import Foundation
import UIKit

/// people view controller
class OsobyViewController: UIViewController {
    
    /// initialization function
    init() {
        super.init(nibName: nil, bundle: nil)
        let view = OsobyView()
        view.delegate = self
        self.view = view
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// function that gets called after view appeared
    /// - Parameter animated: animated
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        navigationController?.setNavigationBarHidden(true, animated: false)
    }
}

extension OsobyViewController: OsobyViewDelegate {
    
    /// function that shows an error alert
    /// - Parameters:
    ///   - title: title for alert
    ///   - subtite: sutitle for alert
    func showError(title: String, subtite: String) {
        let alert = UIAlertController(title: title, message: subtite, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
        present(alert, animated: true)
    }
    
    /// function that presents a search view with given quert
    /// - Parameters:
    ///   - query: query
    ///   - jednotky: should search in unit
    ///   - skauti: should search in whole system
    func searchForPerson(query: String, jednotky: Bool, skauti: Bool) {
        let viewModel = OsobyListViewModel()
        viewModel.reloadData(query: query, jednotky: jednotky, skauti: skauti)
        navigationController?.pushViewController(OsobyListViewController(viewModel: viewModel), animated: true)
    }
    
}
