//
//  MoleculeCollectionViewCell.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 7/2/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit

/// A collection item that is a container for any molecule.
open class MoleculeCollectionViewCell: CollectionViewCell {
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let collectionModel = model as? MoleculeCollectionItemModel else { return }
        
        if molecule == nil {
            if let moleculeView = ModelRegistry.createMolecule(collectionModel.molecule, delegateObject: delegateObject, additionalData: additionalData) {
                addMolecule(moleculeView)
            }
        } else {
            molecule?.set(with: collectionModel.molecule, delegateObject, additionalData)
        }
        
        guard let molecule = molecule as? (UIView & MVMCoreUIViewConstrainingProtocol) else { return }
        containerHelper.set(with: collectionModel, for: molecule)
    }
    
    open override func reset() {
        super.reset()
        molecule?.reset()
    }
    
    open override class func nameForReuse(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> String? {
        return MoleculeContainer.nameForReuse(with: model, delegateObject)
    }
    
    open override class func requiredModules(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer<MVMCoreErrorObject?>?) -> [String]? {
        return MoleculeContainer.requiredModules(with: model, delegateObject, error: error)
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return MoleculeContainer.estimatedHeight(with: model, delegateObject) ?? 100
    }
}
