//
//  ListRightVariableTextLinkAllTextAndLinks.swift
//  MVMCoreUI
//
//  Created by Dhamodaram Nandi on 19/03/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers open class ListRightVariableTextLinkAllTextAndLinks: TableViewCell {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    public let link = Link()
    public let eyebrowHeadlineBodyLink = EyebrowHeadlineBodyLink()
    
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    public var stack: Stack<StackModel>
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        stack = Stack<StackModel>.createStack(with: [(view: eyebrowHeadlineBodyLink, model: StackItemModel(horizontalAlignment: .leading, verticalAlignment: .top)),
                                                     (view: link, model: StackItemModel(horizontalAlignment:.fill, verticalAlignment: .top))],
                                              axis: .horizontal)
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //-----------------------------------------------------
    // MARK: - Lifecycle
    //-----------------------------------------------------
    
    override open func setupView() {
        super.setupView()
        
        addMolecule(stack)
        stack.restack()
    }
    
    //--------------------------------------------------
    // MARK: - Molecular
    //--------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? ListRightVariableTextLinkAllTextAndLinksModel else { return }
        
        link.set(with: model.link, delegateObject, additionalData)
        eyebrowHeadlineBodyLink.set(with: model.eyebrowHeadlineBodyLink, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 90
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    
    func getAccessibilityMessage() -> String? {
        
        guard let linkText = link.titleLabel?.text else {
            return eyebrowHeadlineBodyLink.getAccessibilityMessage()
        }
        
        guard let label = eyebrowHeadlineBodyLink.getAccessibilityMessage() else {
            return linkText
        }
        
        return label + ", " + linkText
    }
    
    func updateAccessibilityLabel() {
        
        let linkShowing = eyebrowHeadlineBodyLink.link.titleLabel?.text?.count ?? 0 > 0
        accessibilityTraits = .button
        
        if !linkShowing && accessoryView == nil {
            // Make whole cell focusable if one action
            isAccessibilityElement = true
            accessibilityLabel = getAccessibilityMessage()
        } else {
            // Make buttons focusable.
            isAccessibilityElement = false
            var elements: [Any] = []
            
            if let accessoryView = accessoryView {
                accessoryView.accessibilityLabel = eyebrowHeadlineBodyLink.getAccessibilityMessage()
                elements.append(accessoryView)
            } else {
                link.accessibilityLabel = getAccessibilityMessage()
            }
            
            elements.append(link)
            
            if linkShowing {
                elements.append(eyebrowHeadlineBodyLink.link)
            }
            
            accessibilityElements = elements
        }
    }
            
    open override func accessibilityActivate() -> Bool {
        return link.accessibilityActivate()
    }
}
