//
//  UIStackView+Extension.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 3/16/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

extension UIStackView: MVMCoreViewProtocol {
    public func updateView(_ size: CGFloat) {
        for view in arrangedSubviews {
            (view as? MVMCoreViewProtocol)?.updateView(size)
        }
    }
    
    /// A convenience function for updating molecules. If model is nil, view is hidden.
    open func updateContainedMolecules(with models: [MoleculeModelProtocol?], _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        for (index, item) in arrangedSubviews.enumerated() {
            if let model = models[index] {
                (item as? MoleculeViewProtocol)?.set(with: model, delegateObject, additionalData)
                item.isHidden = false
            } else {
                item.isHidden = true
            }
        }
        layoutIfNeeded()
    }
}

extension UIStackView: MoleculeViewProtocol {
    public func reset() {
        for view in arrangedSubviews {
            (view as? MoleculeViewProtocol)?.reset()
        }
    }
}

