//
//  ListThreeColumnBillHistory.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 29/07/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers open class ListThreeColumnBillHistory: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-------------------------------------------------------
    
    public let leftLabel = Label(fontStyle: .RegularBodySmall)
    public let centerLabel = Label(fontStyle: .RegularBodySmall)
    public let rightLabel = Label(fontStyle: .RegularBodySmall)
    var stack: Stack<StackModel>
    
    //------------------------------------------------------
    // MARK: - Initializers
    //------------------------------------------------------
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        stack = Stack<StackModel>.createStack(with: [(view: leftLabel, model: StackItemModel(percent: 34, horizontalAlignment: .leading)),
                                                     (view: centerLabel, model: StackItemModel(percent: 42, horizontalAlignment: .leading)),
                                                     (view: rightLabel, model: StackItemModel(percent: 24, horizontalAlignment: .trailing))],
                                              axis: .horizontal)
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //------------------------------------------------------
    // MARK: - Lifecycle
    //------------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        addMolecule(stack)
        stack.restack()
    }
    
    //--------------------------------------------------
    // MARK: - ModelMoleculeViewProtocol
    //--------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let model = model as? ListThreeColumnBillHistoryModel 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(.RegularBodySmall)
        centerLabel.setFontStyle(.RegularBodySmall)
        rightLabel.setFontStyle(.RegularBodySmall)
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        isAccessibilityElement = true
        var message = ""
        
        if let leftText = leftLabel.text, !leftText.isEmpty {
            message += leftText + ", "
        }
        
        if let centerText = centerLabel.text, !centerText.isEmpty {
            message += centerText + ", "
        }
        
        if let rightText = rightLabel.text, !rightText.isEmpty {
            message += rightText
        }
        
        accessibilityLabel = message
    }
}
