//
//  ListOneColumnTextWithWhitespaceDividerTall.swift
//  MVMCoreUI
//
//  Created by Dhamodaram Nandi on 09/03/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation


@objcMembers open class ListOneColumnTextWithWhitespaceDividerTall: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------
    
    public var stack: Stack<StackModel>
    public let headline = Label(fontStyle: .BoldTitleMedium)
    public let body = Label(fontStyle: .RegularBodySmall)
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        stack = Stack<StackModel>.createStack(with: [(view: headline, model: StackItemModel(horizontalAlignment: .leading)),
                                                     (view: body, model: StackItemModel(spacing: 0, horizontalAlignment: .leading))],
                                              axis: .vertical)
        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()
        isAccessibilityElement = true
        updateAccessibilityLabel()
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? ListOneColumnTextWithWhitespaceDividerTallModel else { return }
        stack.updateContainedMolecules(with: [model.headline, model.body], delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 90
    }
    
    open override func reset() {
        super.reset()
        headline.setFontStyle(.BoldTitleMedium)
        body.setFontStyle(.RegularBodySmall)
    }
    
    //----------------------------------------------------
    // MARK: - Accessibility
    //----------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        var message = ""
        
        if let headlineLabel = headline.text, !headlineLabel.isEmpty {
            message += headlineLabel + ", "
        }
        
        if let bodyLabel = body.text, !bodyLabel.isEmpty {
            message += bodyLabel
        }
        
        accessibilityLabel = message
    }
}
