//
//  ListLeftVariableIconAllTextLinks.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 20/05/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers open class ListLeftVariableIconAllTextLinks: TableViewCell {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    public let leftImage = LoadImageView(pinnedEdges: .all)
    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: leftImage, model: StackItemModel(horizontalAlignment: .fill)), (view: eyebrowHeadlineBodyLink, model: StackItemModel(horizontalAlignment: .leading))], 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()
        
        leftImage.addSizeConstraintsForAspectRatio = true
        leftImage.imageView.contentMode = .scaleAspectFit
        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? ListLeftVariableIconAllTextLinksModel else { return }
        
        leftImage.set(with: model.image, delegateObject, additionalData)
        eyebrowHeadlineBodyLink.set(with: model.eyebrowHeadlineBodyLink, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 140
    }
    
    func getAccessibilityMessage() -> String? {
        
        guard let leftImageLabel = leftImage.imageView.accessibilityLabel else {
            return eyebrowHeadlineBodyLink.getAccessibilityMessage()
        }
        
        guard let label = eyebrowHeadlineBodyLink.getAccessibilityMessage() else {
            return leftImageLabel
        }
        
        return leftImageLabel + ", " + 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 leftImageLabel = leftImage.imageView.accessibilityLabel, !leftImageLabel.isEmpty {
                elements.append(leftImage.imageView)
            }
            
            if let otherElements = eyebrowHeadlineBodyLink.getAccessibilityElements() {
                elements.append(otherElements)
            }
            
            accessibilityElements = elements
        }
    }
}
