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


@objcMembers open class ListTwoColumnPriceDetails: TableViewCell {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    let leftLabel = Label(fontStyle: .RegularBodySmall)
    let rightLabel = Label(fontStyle: .RegularBodySmall)
    let view = MVMCoreUICommonViewsUtility.commonView()

    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        view.addSubview(leftLabel)
        view.addSubview(rightLabel)
        NSLayoutConstraint.pinViews(leftView: leftLabel, rightView: rightLabel, alignTop: true)
        contentView.addSubview(view)
        containerHelper.constrainView(view)
        rightLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        rightLabel.setContentHuggingPriority(.defaultHigh, for: .vertical)
    }
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        leftLabel.updateView(size)
        rightLabel.updateView(size)
    }
    
    //--------------------------------------------------
    // 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? ListTwoColumnPriceDetailsModel else { return }
        
        leftLabel.set(with: model.leftLabel, delegateObject, additionalData)
        rightLabel.set(with: model.rightLabel, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override func reset() {
        super.reset()
        leftLabel.reset()
        rightLabel.reset()
        leftLabel.setFontStyle(.RegularBodySmall)
        rightLabel.setFontStyle(.RegularBodySmall)
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 15
    }
    
    //----------------------------------------------------
    // MARK: - Accessibility
    //----------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        isAccessibilityElement = true
        var message = ""
        
        if let leftLabel = leftLabel.text, !leftLabel.isEmpty {
            message += leftLabel + ", "
        }
        
        if let rightLabel = rightLabel.text, !rightLabel.isEmpty {
            message += rightLabel
        }
        
        accessibilityLabel = message
    }
}
