//
//  HeadlineBodyButton.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 9/12/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//


@objcMembers open class HeadlineBodyButton: View {
    //------------------------------------------------------
    // MARK: - Outlets
    //------------------------------------------------------
    
    let headlineBody = HeadlineBody()
    let button = PillButton()
    
    //------------------------------------------------------
    // MARK: - Properties
    //------------------------------------------------------
    
    var buttonHeadlinePadding: CGFloat = 16
    
    //------------------------------------------------------
    // MARK: - Constraints
    //------------------------------------------------------
    
    var buttonTopConstraint: NSLayoutConstraint?
    
    //------------------------------------------------------
    // MARK: - View Lifecycle
    //------------------------------------------------------
    
    override open func setupView() {
        super.setupView()
        
        defaultState()
        
        addSubview(headlineBody)
        addSubview(button)
        
        headlineBody.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true
        headlineBody.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true
        layoutMarginsGuide.trailingAnchor.constraint(equalTo: headlineBody.trailingAnchor).isActive = true
        
        buttonTopConstraint = button.topAnchor.constraint(equalTo: headlineBody.bottomAnchor, constant: buttonHeadlinePadding)
        buttonTopConstraint?.isActive = true
        
        button.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true
        layoutMarginsGuide.bottomAnchor.constraint(equalTo: button.bottomAnchor).isActive = true
        layoutMarginsGuide.trailingAnchor.constraint(greaterThanOrEqualTo: button.trailingAnchor).isActive = true
    }
    
    override open func updateView(_ size: CGFloat) {
        super.updateView(size)
        
        headlineBody.updateView(size)
        button.updateView(size)
        
        buttonTopConstraint?.constant = headlineBody.hasText() && !button.isHidden ? buttonHeadlinePadding : 0
    }
    
    private func defaultState() {
        
        headlineBody.headlineLabel.font = Styler.Font.BoldTitleMedium.getFont()
        headlineBody.messageLabel.font = Styler.Font.RegularMicro.getFont()
        button.styleSecondary()
        button.isHidden = false
        buttonHeadlinePadding = PaddingTwo
    }
    
    //------------------------------------------------------
    // MARK: - Molecule
    //------------------------------------------------------
    
    override open func reset() {
        super.reset()
        
        headlineBody.reset()
        button.reset()
        defaultState()
    }
    
    //------------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //------------------------------------------------------
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 320
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? HeadlineBodyButtonModel else { return }
        
        buttonHeadlinePadding = model.buttonHeadlinePadding
        headlineBody.set(with: model.headlineBody, delegateObject, additionalData)
        button.set(with: model.button, delegateObject, additionalData)
    }
}
