//
//  ImageHeadlineBody.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 8/1/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit

@objcMembers open class ImageHeadlineBody: View {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    let headlineBody = HeadlineBody()
    let imageView = LoadImageView(pinnedEdges: .all)
        
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    var constraintBetweenImageLabelsConstant: CGFloat = 16
    var constraintBetweenImageLabels: NSLayoutConstraint?
    
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------

    open override func setupView() {
        super.setupView()
        headlineBody.headlineLabel.setFontStyle(.BoldBodySmall)
        headlineBody.spaceBetweenLabelsConstant = 0
        imageView.addSizeConstraintsForAspectRatio = true

        addSubview(headlineBody)
        addSubview(imageView)
        
        NSLayoutConstraint.pinViewsVerticalExpandableAlignCenter([imageView, headlineBody])
        trailingAnchor.constraint(equalTo: headlineBody.trailingAnchor).isActive = true
        imageView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        
        constraintBetweenImageLabels = headlineBody.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: constraintBetweenImageLabelsConstant)
        constraintBetweenImageLabels?.isActive = true
    }
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        headlineBody.updateView(size)
        imageView.updateView(size)
    }
    
    // MARK: - MoleculeViewProtocol
    open override func reset() {
        super.reset()
        headlineBody.reset()
        headlineBody.headlineLabel.setFontStyle(.BoldBodySmall)
        headlineBody.spaceBetweenLabelsConstant = 0
        imageView.reset()
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { 95 }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? ImageHeadlineBodyModel else { return }
        
        headlineBody.set(with: model.headlineBody, delegateObject, additionalData)
        imageView.set(with: model.image, delegateObject, additionalData)
    }
}
