//
//  ListLeftVariableRadioButtonBodyText.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 4/1/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


open class ListLeftVariableRadioButtonBodyText: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------
    
    let radioButton = RadioButton()
    let headlineBody = HeadlineBody()
    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: headlineBody, 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()
        
        // Make the whole cell focusable.
        isAccessibilityElement = true
        accessibilityHint = radioButton.accessibilityHint
        accessibilityTraits = radioButton.accessibilityTraits
        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? ListLeftVariableRadioButtonBodyTextModel else { return }
        
        radioButton.set(with: model.radioButton, delegateObject, additionalData)
        headlineBody.set(with: model.headlineBody, 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 headlineLabel = headlineBody.headlineLabel.text {
            message += headlineLabel + ", "
        }
        
        if let messageLabel = headlineBody.messageLabel.text {
            message += messageLabel
        }
        
        accessibilityLabel = message
    }
}
