//
//  Header.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 3/6/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


open class HeaderView: Container {
    public let line = Line()
    
    /// Convenience for doing some default setting
    open var molecule: MoleculeViewProtocol?
    
    var headerModel: HeaderModel? {
        get { model as? HeaderModel }
    }
    
    /// Convenience function to add a molecule to the view.
    open func addMolecule(_ molecule: MoleculeViewProtocol) {
        addSubview(molecule)
        containerHelper.constrainView(molecule)
        self.molecule = molecule
    }
    
    // MARK: - MVMCoreViewProtocol
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        line.updateView(size)
        (molecule as? MVMCoreViewProtocol)?.updateView(size)
    }
    
    public override func setupView() {
        super.setupView()
        line.setStyle(.heavy)
        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
    }
    
    // MARK: - MoleculeViewProtocol
    open override func reset() {
        super.reset()
        line.setStyle(.heavy)
        molecule?.reset()
    }
    
    // MARK: - MoleculeViewProtocol
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let headerModel = headerModel else { return }
        if let lineModel = headerModel.line {
            line.set(with: lineModel, delegateObject, additionalData)
        }
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        PaddingDefaultVerticalSpacing + PaddingDefaultVerticalSpacing
    }
}
