//
//  TitleLockup.swift
//  MVMCoreUI
//
//  Created by Nadigadda, Sumanth on 04/05/22.
//  Copyright © 2022 Verizon Wireless. All rights reserved.
//

@objcMembers open class TitleLockup: View {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    public let eyebrow = Label(fontStyle: .RegularBodySmall)
    public let title = Label(fontStyle: .RegularBodySmall)
    public let subTitle = Label(fontStyle: .RegularBodySmall)
    public lazy var stack: UIStackView = {
        let stack = UIStackView(arrangedSubviews: [eyebrow, title, subTitle])
        stack.translatesAutoresizingMaskIntoConstraints = false
        stack.axis = .vertical
        return stack
    }()
    
    var castModel: TitleLockupModel? {
        get { return model as? TitleLockupModel }
    }
    
    //--------------------------------------------------
    // MARK: - Initialization
    //--------------------------------------------------
    
    public convenience init() {
        self.init(frame: .zero)
    }
    
    //--------------------------------------------------
    // MARK: - MFViewProtocol
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        addSubview(stack)
        NSLayoutConstraint.constraintPinSubview(toSuperview: stack)
    }
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        stack.updateView(size)
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open override func reset() {
        super.reset()
        stack.reset()
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let model  = model as? TitleLockupModel else { return }
        stack.setCustomSpacing(model.defaultEyebrowTitleSpacing(), after: eyebrow)
        stack.setCustomSpacing(model.defaultTitleSubTitleSpacing(), after: title)
        stack.updateContainedMolecules(with: [model.eyebrow,
                                              model.title,
                                              model.subTitle],
                                       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 = title.text {
            message += headlineLabel + ", "
        }
        
        if let bodyLabel = subTitle.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 title.hasText {
            elements.append(title)
        }
        
        if subTitle.hasText {
            elements.append(subTitle)
        }
        
        return elements.count > 0 ? elements : nil
    }
}

