//
//  ListOneColumnFullWidthTextBodyText.swift
//  MVMCoreUI
//
//  Created by Kruthika KP on 05/03/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation


@objcMembers open class ListOneColumnFullWidthTextBodyText: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------
    
    public var headlineBody = HeadlineBody()
    
    //-----------------------------------------------------
    // MARK: - Lifecycle
    //-----------------------------------------------------
    
    override open func setupView() {
        super.setupView()
        
        addMolecule(headlineBody)
    }
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?){
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? ListOneColumnFullWidthTextBodyTextModel else { return }
        
        headlineBody.set(with: model.headlineBody, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 90
    }
    
    //-----------------------------------------------------
    // MARK: - Accessibility
    //-----------------------------------------------------
    
    /// Ensures voice over does not read "selected" after user triggers action on cell.
    override public var accessibilityTraits: UIAccessibilityTraits {
        get {
            return (accessoryView != nil) ? .button : .none
        }
        set {}
    }
    
    func updateAccessibilityLabel() {
        isAccessibilityElement = true
        var message = ""
        
        if let headlineLabel = headlineBody.headlineLabel.text {
            message += headlineLabel + ", "
        }
        
        if let messageLabel = headlineBody.messageLabel.text {
            message += messageLabel
        }
        
        accessibilityLabel = message
    }
}
