//
//  ListThreeColumnSpeedTestDivider.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 03/04/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation


@objcMembers open class ListThreeColumnSpeedTestDivider: TableViewCell {
    //-------------------------------------------------------
    // MARK: - Outlets
    //-------------------------------------------------------
    let leftLabel = Label(fontStyle: .BoldBodySmall)
    let centerLabel = Label(fontStyle: .BoldBodySmall)
    let rightLabel = Label(fontStyle: .BoldBodySmall)
    var stack: Stack<StackModel>
    
    //-------------------------------------------------------
    // MARK: - Initializers
    //-------------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        stack = Stack<StackModel>.createStack(with: [(view: leftLabel, model: StackItemModel(percent: 37, horizontalAlignment: .leading)),
                                                     (view: centerLabel, model: StackItemModel(percent: 33, horizontalAlignment: .leading)),
                                                     (view: rightLabel, model: StackItemModel(percent: 30, 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
    //-------------------------------------------------------
    
    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? ListThreeColumnSpeedTestDividerModel else { return }
        
        leftLabel.set(with: model.leftLabel, delegateObject, additionalData)
        centerLabel.set(with: model.centerLabel, delegateObject, additionalData)
        rightLabel.set(with: model.rightLabel, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 121
    }
    
    open override func reset() {
        super.reset()
        leftLabel.setFontStyle(.BoldBodySmall)
        centerLabel.setFontStyle(.BoldBodySmall)
        rightLabel.setFontStyle(.BoldBodySmall)
    }
    
    //----------------------------------------------------
    // MARK: - Accessibility
    //----------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        var message = ""
        
        if let leftLabeltext = leftLabel.text, !leftLabeltext.isEmpty {
            message += leftLabeltext + ", "
        }
        
        if let centerLabeltext = centerLabel.text, !centerLabeltext.isEmpty {
            message += centerLabeltext + ", "
        }
        
        if let rightLabeltext = rightLabel.text, !rightLabeltext.isEmpty {
            message += rightLabeltext
        }
        
        accessibilityLabel = message
    }
}
