//
//  EyebrowHeadlineBodyLink.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 9/23/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//


@objcMembers open class EyebrowHeadlineBodyLink: View {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    public let stack = Stack<StackModel>(frame: .zero)
    public let eyebrow = Label(fontStyle: .RegularMicro)
    public let headline = Label(fontStyle: .BoldBodySmall)
    public let body = Label(fontStyle: .RegularBodySmall, true)
    public let link = Link()
    
    var castModel: EyebrowHeadlineBodyLinkModel? {
        get { return model as? EyebrowHeadlineBodyLinkModel }
    }
    
    //--------------------------------------------------
    // MARK: - Initialization
    //--------------------------------------------------
    
    public convenience init(spacing: CGFloat) {
        self.init(frame: .zero)
        stack.stackModel?.spacing = spacing
        stack.restack()
    }
    
    //--------------------------------------------------
    // MARK: - MFViewProtocol
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        stack.setAndCreateModel(with: [eyebrow, headline, body, link])
        stack.stackModel?.spacing = 0
        addSubview(stack)
        NSLayoutConstraint.constraintPinSubview(toSuperview: stack)
        stack.restack()
    }
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        stack.updateView(size)
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open override func reset() {
        super.reset()
        stack.reset()
        eyebrow.setFontStyle(.RegularMicro)
        headline.setFontStyle(.BoldBodySmall)
        body.setFontStyle(.RegularBodySmall)
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        stack.updateContainedMolecules(with: [castModel?.eyebrow,
                                              castModel?.headline,
                                              castModel?.body,
                                              castModel?.link],
                                       delegateObject, additionalData)
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 65
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility Helpers
    //--------------------------------------------------
    
    /// Returns the labels text in one message.
    func getAccessibilityMessage() -> String? {
        
        var message = ""
        
        if let eyebrowLabel = eyebrow.text {
            message += eyebrowLabel + ", "
        }
        
        if let headlineLabel = headline.text {
            message += headlineLabel + ", "
        }
        
        if let bodyLabel = body.text {
            message += bodyLabel
        }
        
        return message.count > 0 ? message : nil
    }
    
    /// Returns an array of the appropriate accessibility elements.
    func getAccessibilityElements() -> [Any]? {
        
        var elements: [UIView] = []
        
        if eyebrow.hasText {
            elements.append(eyebrow)
        }
        
        if headline.hasText {
            elements.append(headline)
        }
        
        if body.hasText {
            elements.append(body)
        }
        
        if link.titleLabel?.text?.count ?? 0 > 0 {
            elements.append(link)
        }
        
        return elements.count > 0 ? elements : nil
    }
}
