//
//  MoleculeSectionHeader.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 10/2/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

@objcMembers open class MoleculeSectionHeader: SectionHeaderFooterView {
    open var molecule: MoleculeViewProtocol?
    public let containerHelper = ContainerHelper()
    public let line = Line()

    /// Adds the molecule to the view.
    open func addMolecule(_ molecule: MoleculeViewProtocol) {
        contentView.addSubview(molecule)
        containerHelper.constrainView(molecule)
        self.molecule = molecule
    }
    
    public override func setupView() {
        super.setupView()
        line.setStyle(.thin)
        contentView.addSubview(line)
        NSLayoutConstraint.pinViewBottom(toSuperview: line, useMargins: false, constant: 0).isActive = true
        NSLayoutConstraint.pinViewLeft(toSuperview: line, useMargins: true, constant: 0).isActive = true
        NSLayoutConstraint.pinViewRight(toSuperview: line, useMargins: true, constant: 0).isActive = true
    }
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        containerHelper.updateViewMargins(self, model: model as? MoleculeSectionHeaderModel, size: size)
        contentView.directionalLayoutMargins = directionalLayoutMargins
        (molecule as? MVMCoreViewProtocol)?.updateView(size)
        line.updateView(size)
    }
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let model = model as? MoleculeSectionHeaderModel else { return }
        if molecule != nil {
            molecule?.set(with: model.molecule, delegateObject, additionalData)
        } else if let moleculeView = ModelRegistry.createMolecule(model.molecule, delegateObject: delegateObject, additionalData: additionalData) {
            addMolecule(moleculeView)
        }
        containerHelper.set(with: model, for: molecule as? MVMCoreUIViewConstrainingProtocol)
        line.setOptional(with: model.line, delegateObject, additionalData)
    }
    
    open override func reset() {
        super.reset()
        line.setStyle(.thin)
        molecule?.reset()
    }

    public override class func nameForReuse(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> String? {
        return MoleculeContainer.nameForReuse(with: model, delegateObject)
    }
    
    public override class func requiredModules(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer<MVMCoreErrorObject?>?) -> [String]? {
        return MoleculeContainer.requiredModules(with: model, delegateObject, error: error)
    }
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return MoleculeContainer.estimatedHeight(with: model, delegateObject) ?? 80.0
    }
}
