//
//  ListLeftVariableNumberedListAllTextAndLinks.swift
//  MVMCoreUI
//
//  Created by Subhankar Acharya on 21/05/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers open class ListLeftVariableNumberedListAllTextAndLinks: TableViewCell {
    //--------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------
    
    public let leftLabel = Label(fontStyle: .Title2XLarge)
    public let eyebrowHeadlineBodyLink = EyebrowHeadlineBodyLink()
    public var stack: Stack<StackModel>
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        stack = Stack<StackModel>.createStack(with: [(view: leftLabel, model: StackItemModel(horizontalAlignment: .fill)),
                                                     (view: eyebrowHeadlineBodyLink, 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: - Life Cycle
    //--------------------------------------------------
    override open func setupView() {
        super.setupView()
        leftLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        leftLabel.setContentHuggingPriority(.defaultHigh, for: .vertical)
        leftLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        addMolecule(stack)
        stack.restack()
    }
    
    //---------------------------------------------------
    // MARK: - Molecule
    //---------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? ListLeftVariableNumberedListAllTextAndLinksModel else { return }
        
        leftLabel.text = String(model.number)
        eyebrowHeadlineBodyLink.set(with: model.eyebrowHeadlineBodyLink, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 140
    }
    
    open override func reset() {
        super.reset()
        leftLabel.setFontStyle(.Title2XLarge)
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    
    func getAccessibilityMessage() -> String? {
        
        guard let leftLabel = leftLabel.text else {
            return eyebrowHeadlineBodyLink.getAccessibilityMessage()
        }
        
        guard let label = eyebrowHeadlineBodyLink.getAccessibilityMessage() else {
            return leftLabel
        }
        
        return leftLabel + ", " + label
    }
    
    func updateAccessibilityLabel() {

        let linkShowing = eyebrowHeadlineBodyLink.link.titleLabel?.text?.count ?? 0 > 0
        isAccessibilityElement = !linkShowing
        accessibilityTraits = (isAccessibilityElement && accessoryView != nil) ? .button : .none
        
        if !linkShowing {
            // Make whole cell focusable if no link.
            accessibilityLabel = getAccessibilityMessage()
        } else if let accessoryView = accessoryView {
            // Both caret and link. Read all content on caret.
            accessoryView.accessibilityLabel = getAccessibilityMessage()
            accessibilityElements = [accessoryView, eyebrowHeadlineBodyLink.link]
        } else {
            // Only link. Manually add accessibility elements to ensure they are read in the right order.
            var elements: [Any] = []
            
            if let leftLabelText = leftLabel.text, !leftLabelText.isEmpty {
                elements.append(leftLabel)
            }
            
            if let otherElements = eyebrowHeadlineBodyLink.getAccessibilityElements() {
                elements.append(otherElements)
            }
            
            accessibilityElements = elements
        }
    }
}
