//
//  ListMoleculeContainer.swift
//  MVMCoreUI
//
//  Created by Murugan, Vimal on 03/01/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit


// This class is only temporarily necessary. Eventually we will have initWithModel instad of just init for moleculeviews, which will remove this need.
open class StringAndMoleculeStack: MoleculeStackView {
    override open func createStackItemsFromModel(_ model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        
        guard let model = model as? StackModelProtocol,
              let molcules = model.molecules as? [MoleculeStackItemModel]
        else { return }
        
        for stackItemModel in molcules {
            guard let stringAndMoleculeModel = stackItemModel.molecule as? StringAndMoleculeModel,
                let molecule = ModelRegistry.createMolecule(stringAndMoleculeModel.molecule, delegateObject: delegateObject
                    , additionalData: additionalData) else {
                // Throw error
                return
            }
            let view = StringAndMoleculeView(string: stringAndMoleculeModel.string, molecule: molecule)
            let stackItem = MoleculeStackItem(andContain: view)
            stackItem.set(with: stackItemModel, delegateObject, nil)
            stackItems.append(stackItem)
        }
    }
}
