//
//  HeadLineBodyCaretLinkImage.swift
//  MVMCoreUI
//
//  Created by Arora, Prateek on 06/01/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers public class HeadLineBodyCaretLinkImage: Container {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    let headlineBody = HeadlineBody()
    let caretButton = CaretLink()
    let backgroundImageView = LoadImageView(pinnedEdges: .all)
    
    //--------------------------------------------------
    // MARK: - Constraints
    //--------------------------------------------------
    
    let maxWidth: CGFloat = 350
    static let heightConstant: CGFloat = 320
    var heightConstraint: NSLayoutConstraint?
    
    //--------------------------------------------------
    // MARK: - Setup
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        
        heightConstraint = heightAnchor.constraint(equalToConstant: Self.heightConstant)
        heightConstraint?.isActive = true
        
        backgroundImageView.addSizeConstraintsForAspectRatio = true
        
        let container = MVMCoreUICommonViewsUtility.commonView()
        addAndContain(container)
        
        container.addSubview(headlineBody)
        container.addSubview(caretButton)
        
        headlineBody.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 0).isActive = true
        headlineBody.topAnchor.constraint(equalTo: container.topAnchor, constant: 0).isActive = true
        
        headlineBody.widthAnchor.constraint(equalTo: container.widthAnchor, multiplier: 0.67).isActive = true
        let headLineBodyWidth = headlineBody.widthAnchor.constraint(lessThanOrEqualToConstant: maxWidth)
        headLineBodyWidth.priority = UILayoutPriority(250)
        headLineBodyWidth.isActive = true
        
        caretButton.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 0).isActive = true
        container.bottomAnchor.constraint(equalTo: caretButton.bottomAnchor, constant: 0).isActive = true
        caretButton.topAnchor.constraint(greaterThanOrEqualTo: headlineBody.bottomAnchor, constant: PaddingTwo).isActive = true
        
        backgroundImageView.imageView.contentMode = .scaleAspectFill
        backgroundImageView.pinEdges(.all)
        addSubview(backgroundImageView)
        NSLayoutConstraint.constraintPinSubview(toSuperview: backgroundImageView)
        sendSubviewToBack(backgroundImageView)
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        headlineBody.updateView(size)
        caretButton.updateView(size)
        backgroundImageView.updateView(size)
        backgroundImageView.pinEdges(.all)
    }
    
    open override func reset() {
        super.reset()
        headlineBody.reset()
        backgroundImageView.reset()
    }
    
    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? HeadlineBodyCaretLinkImageModel else { return }
        
        headlineBody.set(with: model.headlineBody, delegateObject, additionalData)
        caretButton.setOptional(with: model.caretLink, delegateObject, additionalData)
        caretButton.isHidden = model.caretLink == nil
        backgroundImageView.set(with: model.image, delegateObject, additionalData)
        backgroundImageView.pinEdges(.all)
    }
}

