import UIKit

/// unit members view delegate protocol
protocol ClenoveJednotkyViewDelegate: AnyObject {
    func segueToPerson(id: String)
}

/// unit members view
class ClenoveJednotkyView: UITableView {
    weak var viewDelegate: ClenoveJednotkyViewDelegate?
    var viewModel: ClenoveJednotkyViewModelProtocol? {
        didSet {
            guard let viewModel = viewModel else { return }
            if viewModel.loading {
                activityIndicator.startAnimating()
            } else {
                activityIndicator.stopAnimating()
            }
            reloadData()
        }
    }
    
    private let activityIndicator: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.style = .large
        view.color = scoutBlue
        view.startAnimating()
        return view
    }()
    
    /// initialization function
    /// - Parameters:
    ///   - frame: frame
    ///   - style: style for table
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: .insetGrouped)
        addSubview(activityIndicator)
        delegate = self
        dataSource = self
        tableFooterView = UIView()
        register(OsobyTableViewCell.self, forCellReuseIdentifier: OsobyTableViewCell.reuseString)
        setupConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupConstraints() {
        NSLayoutConstraint.activate([
            activityIndicator.centerYAnchor.constraint(equalTo: centerYAnchor),
            activityIndicator.centerXAnchor.constraint(equalTo: centerXAnchor)
        ])
    }
}

extension ClenoveJednotkyView: UITableViewDelegate, UITableViewDataSource {
    /// function that returns number of table rows for given section
    /// - Parameters:
    ///   - tableView: table view
    ///   - section: section
    /// - Returns: number of rows
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        viewModel?.items.count ?? 0
    }
    
    /// function that returns table view cell for given path
    /// - Parameters:
    ///   - tableView: tableview
    ///   - indexPath: index path
    /// - Returns: table view cell for given path
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let item = viewModel?.items[indexPath.row],
            let cell = tableView.dequeueReusableCell(withIdentifier: OsobyTableViewCell.reuseString, for: indexPath) as? OsobyTableViewCell
                else { return UITableViewCell() }
        cell.title.text = item.Person
        return cell
    }
    
    /// function that gets called when cell is selected
    /// - Parameters:
    ///   - tableView: tableview
    ///   - indexPath: index that was selected
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        guard let item = viewModel?.items[indexPath.row],
            item.ID_Person != "" else { return }
        viewDelegate?.segueToPerson(id: item.ID_Person)
    }
    
}
