//
//  ListFourColumnDataUsageDivider.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 18/02/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation


@objcMembers open class ListFourColumnDataUsageDivider: TableViewCell {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    // MARK: - MFViewProtocol
    let label1 = Label(fontStyle: .BoldBodySmall)
    let label2 = Label(fontStyle: .BoldBodySmall)
    let label3 = Label(fontStyle: .BoldBodySmall)
    let label4 = Label(fontStyle: .BoldBodySmall)
    var stack: Stack<StackModel>
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        stack = Stack<StackModel>.createStack(with: [(view: label1, model: StackItemModel(percent: 19, horizontalAlignment: .leading)),
                                                     (view: label2, model: StackItemModel(percent: 44, horizontalAlignment: .leading)),
                                                     (view: label3, model: StackItemModel(percent: 17, horizontalAlignment: .leading)),
                                                     (view: label4, model: StackItemModel(percent: 20, horizontalAlignment: .leading))],
                                              axis: .horizontal, spacing: 8)
        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? ListFourColumnDataUsageDividerModel else { return }
        
        label1.set(with: model.label1, delegateObject, additionalData)
        label2.set(with: model.label2, delegateObject, additionalData)
        label3.set(with: model.label3, delegateObject, additionalData)
        label4.set(with: model.label4, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 121
    }
    
    open override func reset() {
        super.reset()
        label1.setFontStyle(.BoldBodySmall)
        label2.setFontStyle(.BoldBodySmall)
        label3.setFontStyle(.BoldBodySmall)
        label4.setFontStyle(.BoldBodySmall)
    }
    
    //----------------------------------------------------
    // MARK: - Accessibility
    //----------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        var message = ""
        
        if let label1Text = label1.text, !label1Text.isEmpty {
            message += label1Text + ", "
        }
        
        if let label2Text = label2.text, !label2Text.isEmpty {
            message += label2Text + ", "
        }
        if let label3Text = label3.text, !label3Text.isEmpty {
            message += label3Text + ", "
        }
        if let label4Text = label4.text, !label4Text.isEmpty {
            message += label4Text
        }
        
        accessibilityLabel = message
    }
}
