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


@objcMembers open class ListLeftVariableRadioButtonAllTextAndLinks: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------
    
    let radioButton = RadioButton()
    let eyebrowHeadlineBodyLink = EyebrowHeadlineBodyLink()
    var stack: Stack<StackModel>

    private var observation: NSKeyValueObservation? = nil
    
    //-----------------------------------------------------
    // MARK: - Initializers
    //-----------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        stack = Stack<StackModel>.createStack(with: [(view: radioButton, 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: - View Lifecycle
    //-----------------------------------------------------
    
    override open func setupView() {
        super.setupView()
        addMolecule(stack)
        stack.restack()
        
        accessibilityTraits = radioButton.accessibilityTraits
        accessibilityHint = radioButton.accessibilityHint
        updateAccessibilityLabel()
        // Update accessibility label on radio button state change.
        observation = observe(\.radioButton.isSelected, options: [.new]) { [weak self] _, _ in
            self?.updateAccessibilityLabel()
        }
    }
    
    //----------------------------------------------------
    // 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? ListLeftVariableRadioButtonAllTextAndLinksModel else { return }
        
        radioButton.set(with: model.radioButton, delegateObject, additionalData)
        eyebrowHeadlineBodyLink.set(with: model.eyebrowHeadlineBodyLink, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 90
    }
    
    public override func didSelectCell(at index: IndexPath, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
        radioButton.tapAction()
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------

    func updateAccessibilityLabel() {
        
        var message = ""
        
        radioButton.updateAccessibilityLabel()
        
        if let radioButtonLabel = radioButton.accessibilityLabel {
            message += radioButtonLabel + ", "
        }
        
        if let label = eyebrowHeadlineBodyLink.getAccessibilityMessage() {
            message += label
        }
        
        let linkShowing = eyebrowHeadlineBodyLink.link.titleLabel?.text?.count ?? 0 > 0
        isAccessibilityElement = !linkShowing
        if !linkShowing {
            // Make whole cell focusable if no link.
            accessibilityLabel = message
        } else {
            // Allow only radio button and link to be focused on.
            radioButton.accessibilityLabel = message
            accessibilityElements = [radioButton, eyebrowHeadlineBodyLink.link]
        }
    }
}
