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


@objcMembers open class ListRightVariableButtonAllTextAndLinks: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------
    
    public let button = PillButton()
    public let eyebrowHeadlineBodyLink = EyebrowHeadlineBodyLink()
    
    //-----------------------------------------------------
    // MARK: - Properties
    //-----------------------------------------------------
    
    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)),
                                                     (view: button, model: StackItemModel(horizontalAlignment: .fill))],
                                              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()
        accessibilityValue = button.accessibilityValue
        accessibilityHint = button.accessibilityHint
        accessibilityTraits = .button
    }
    
    //-----------------------------------------------------
    // 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? ListRightVariableButtonAllTextAndLinksModel else { return }
        
        button.set(with: model.button, 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 buttonText = button.titleLabel?.text else {
            return eyebrowHeadlineBodyLink.getAccessibilityMessage()
        }
        
        guard let label = eyebrowHeadlineBodyLink.getAccessibilityMessage() else {
            return buttonText
        }
        
        return label + ", " + buttonText
    }
    
    func updateAccessibilityLabel() {
        
        let linkShowing = eyebrowHeadlineBodyLink.link.titleLabel?.text?.count ?? 0 > 0
        
        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 {
                button.accessibilityLabel = getAccessibilityMessage()
            }
            
            elements.append(button)
            
            if linkShowing {
                elements.append(eyebrowHeadlineBodyLink.link)
            }
            
            accessibilityElements = elements
        }
    }
    
    open override func accessibilityActivate() -> Bool {
        return button.accessibilityActivate()
    }
}
