//
//  ListLeftVariableCheckboxAllTextAndLinks.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 11/02/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers open class ListLeftVariableCheckboxAllTextAndLinks: TableViewCell {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    public let checkbox = Checkbox()
    public let eyebrowHeadlineBodyLink = EyebrowHeadlineBodyLink()
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public 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: checkbox, 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()
        addMolecule(stack)
        stack.restack()
        
        accessibilityHint = checkbox.accessibilityHint
        accessibilityTraits = checkbox.accessibilityTraits
        // Update accessibility label on checkbox state change.
        observation = observe(\.checkbox.isSelected, options: [.new]) { [weak self] _, _ in
            self?.updateAccessibilityLabel()
        }
        
        updateAccessibilityLabel()
    }
    
    //--------------------------------------------------
    // MARK: - MVMCoreUIMoleculeViewProtocol
    //--------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? ListLeftVariableCheckboxAllTextAndLinksModel else { return }
        
        checkbox.set(with: model.checkbox, delegateObject, additionalData)
        eyebrowHeadlineBodyLink.set(with: model.eyebrowHeadlineBodyLink, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 140
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        var message = ""
        
        checkbox.updateAccessibilityLabel()
        
        if let checkboxLabel = checkbox.accessibilityLabel, !checkboxLabel.isEmpty {
            message += checkboxLabel + ", "
        }
        
        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 checkbox and link to be focused on.
            checkbox.accessibilityLabel = message
            accessibilityElements = [checkbox, eyebrowHeadlineBodyLink.link]
        }
    }
    
    override open func accessibilityActivate() -> Bool {
        
        if isAccessibilityElement {
            return checkbox.accessibilityActivate()
        }
            
        return false
    }
}
