//
//  ListFourColumnDataUsageListItem.swift
//  MVMCoreUI
//
//  Created by Kruthika KP on 27/03/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers public class ListFourColumnDataUsageListItem: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------
    
    var stack: Stack<StackModel>
    let label1 = Label(fontStyle: .RegularBodySmall)
    let label2 = Label(fontStyle: .RegularBodySmall)
    let label3 = Label(fontStyle: .RegularBodySmall)
    let label4 = Label(fontStyle: .RegularBodySmall)
    let arrow = Arrow()
    let arrowAndLabel2Stack: Stack<StackModel>
    
    //-----------------------------------------------------
    // MARK: - Initializers
    //-----------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        arrowAndLabel2Stack = Stack<StackModel>.createStack(with: [(view: arrow, model: StackItemModel(horizontalAlignment: .fill)),
                                                                   (view: label2, model: StackItemModel(horizontalAlignment: .leading))],
                                                            axis: .horizontal, spacing: 4)
        label2.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        label2.setContentHuggingPriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        stack = Stack<StackModel>.createStack(with: [(view: label1, model: StackItemModel(percent: 19, horizontalAlignment: .leading)),
                                                     (view: arrowAndLabel2Stack, model: StackItemModel(percent: 44, horizontalAlignment: .fill)),
                                                     (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: - Lifecycle
    //-----------------------------------------------------
    
    override open func setupView() {
        super.setupView()
        addMolecule(stack)
        arrowAndLabel2Stack.restack()
        stack.restack()
    }
    
    //-----------------------------------------------------
    // MARK: - Molecular
    //-----------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? ListFourColumnDataUsageListItemModel 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)
        arrow.set(with: model.arrow, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 121
    }
    
    open override func reset() {
        super.reset()
        label1.setFontStyle(.RegularBodySmall)
        label2.setFontStyle(.RegularBodySmall)
        label3.setFontStyle(.RegularBodySmall)
        label4.setFontStyle(.RegularBodySmall)
    }
    
    //-----------------------------------------------------
    // MARK: - Accessibility
    //-----------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        isAccessibilityElement = true
        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
    }
}
