//
//  MoleculeListProtocol.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 5/26/21.
//  Copyright © 2021 Verizon Wireless. All rights reserved.
//

public protocol MoleculeListProtocol {
    
    /// Asks the delegate for the index of molecule.
    func getIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol) -> IndexPath?
    
    /// Asks the delegate to add molecules.
    func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], indexPath: IndexPath, animation: UITableView.RowAnimation?)
    
    /// Asks the delegate to remove molecules.
    func removeMolecules(at indexPaths: [IndexPath], animation: UITableView.RowAnimation?)
}

extension MoleculeListProtocol {
    /// Convenience function that removes the passed molecule
    public func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], animation: UITableView.RowAnimation?) {
        var indexPaths: [IndexPath] = []
        for molecule in molecules {
            guard let indexPath = getIndexPath(for: molecule) else { continue }
            indexPaths.append(indexPath)
        }
        if indexPaths.count > 0 {
            removeMolecules(at: indexPaths, animation: animation)
        }
    }
}

public extension MVMCoreUIDelegateObject {
    weak var moleculeListDelegate: (MoleculeListProtocol & NSObjectProtocol)? {
        return (moleculeDelegate as? MoleculeListProtocol & NSObjectProtocol)
    }
}
