//
//  ThreeColumnPlanDataDividerList.swift
//  MVMCoreUI
//
//  Created by Acharya, Subhankar on 04/02/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation


@objcMembers open class ListThreeColumnPlanDataDivider: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Properties
    //-----------------------------------------------------
    
    let leftHeadlineBody  = HeadlineBody()
    let centerHeadLineBody = HeadlineBody()
    let rightHeadLineBody = HeadlineBody()
    var stack: Stack<StackModel>
    
    //-----------------------------------------------------
    // MARK: - Initializers
    //-----------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        stack = Stack<StackModel>.createStack(with: [(view: leftHeadlineBody, model: StackItemModel(percent: 33, horizontalAlignment: .leading)),
                                                     (view: centerHeadLineBody, model: StackItemModel(percent: 34, horizontalAlignment: .center)),
                                                     (view: rightHeadLineBody, model: StackItemModel(percent: 33, horizontalAlignment: .trailing))],
                                              axis: .horizontal)
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //-----------------------------------------------------
    // MARK: - MFViewProtocol
    //-----------------------------------------------------
    
    open override 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? ListThreeColumnPlanDataDividerModel else { return }
        
        leftHeadlineBody.set(with: model.leftHeadlineBody, delegateObject, additionalData)
        centerHeadLineBody.set(with: model.centerHeadlineBody, delegateObject, additionalData)
        rightHeadLineBody.set(with: model.rightHeadlineBody, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 121
    }
    
    //----------------------------------------------------
    // MARK: - Accessibility
    //----------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        var message = ""
        
        if let headlineLabel = leftHeadlineBody.headlineLabel.text {
            message += headlineLabel + ", "
        }
        
        if let messageLabel = leftHeadlineBody.messageLabel.text {
            message += messageLabel + ", "
        }
        
        if let headlineLabel = centerHeadLineBody.headlineLabel.text {
            message += headlineLabel + ", "
        }
        
        if let messageLabel = centerHeadLineBody.messageLabel.text {
            message += messageLabel + ", "
        }
        
        if let headlineLabel = rightHeadLineBody.headlineLabel.text {
            message += headlineLabel + ", "
        }
        
        if let messageLabel = rightHeadLineBody.messageLabel.text {
            message += messageLabel
        }
        
        accessibilityLabel = message
    }
}
