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


@objcMembers public class HeadlineBodyLink: View {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    let headlineBody = HeadlineBody()
    let link = Link()
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    var spaceBetweenConstant: CGFloat = 0
    
    //--------------------------------------------------
    // MARK: - Constraints
    //--------------------------------------------------
    
    var spaceBetween: NSLayoutConstraint?
    
    //--------------------------------------------------
    // MARK: - Setup
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        
        guard subviews.isEmpty else { return }
        
        addSubview(headlineBody)
        addSubview(link)
        headlineBody.styleListItem()
        
        headlineBody.topAnchor.constraint(equalTo: topAnchor).isActive = true
        headlineBody.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        rightAnchor.constraint(greaterThanOrEqualTo: headlineBody.rightAnchor).isActive = true
        var constraint = rightAnchor.constraint(equalTo: headlineBody.rightAnchor)
        constraint.priority = .defaultHigh
        constraint.isActive = true
        
        spaceBetween = link.topAnchor.constraint(equalTo: headlineBody.bottomAnchor, constant: spaceBetweenConstant)
        spaceBetween?.isActive = true
        
        link.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        bottomAnchor.constraint(equalTo: link.bottomAnchor).isActive = true
        rightAnchor.constraint(greaterThanOrEqualTo: link.rightAnchor).isActive = true
        constraint = rightAnchor.constraint(equalTo: link.rightAnchor)
        constraint.priority = .defaultHigh
        constraint.isActive = true
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    public func setSpacing() {
        if headlineBody.hasText() && (link.titleLabel?.text?.count ?? 0) > 0 {
            spaceBetween?.constant = spaceBetweenConstant
        } else {
            spaceBetween?.constant = 0
        }
    }
    
    //--------------------------------------------------
    // MARK: - MVMCoreViewProtocol
    //--------------------------------------------------
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        headlineBody.updateView(size)
        link.updateView(size)
        setSpacing()
    }
    
    open override func reset() {
        super.reset()
        headlineBody.reset()
        headlineBody.styleListItem()
        link.reset()
    }
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let model = model as? HeadlineBodyLinkModel else { return }
        headlineBody.set(with: model.headlineBody, delegateObject, additionalData)
        link.set(with: model.link, delegateObject, additionalData)
    }
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject:  MVMCoreUIDelegateObject?) -> CGFloat? {
        return 60
    }
}
