//
//  ListProgressBarData.swift
//  MVMCoreUI
//
//  Created by Kruthika KP on 18/02/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit


@objcMembers open class ListProgressBarData: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------
    let progressBar = MultiProgress()
    let leftLabel = Label(fontStyle: .BoldBodySmall)
    let rightLabel = Label(fontStyle: .BoldBodySmall)
    let view = MVMCoreUICommonViewsUtility.commonView()
    
    //-----------------------------------------------------
    // MARK: - Lifecycle
    //-----------------------------------------------------
    
    override open func setupView() {
        super.setupView()
        
        rightLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        view.addSubview(progressBar)
        view.addSubview(leftLabel)
        view.addSubview(rightLabel)
        contentView.addSubview(view)
        containerHelper.constrainView(view)
        NSLayoutConstraint.pinViews(leftView: leftLabel, rightView: rightLabel, alignTop: true, bottomAnchor: progressBar.topAnchor, constant: Padding.Two)
        progressBar.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        progressBar.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        view.bottomAnchor.constraint(equalTo: progressBar.bottomAnchor).isActive = true
        isAccessibilityElement = true
        updateAccessibilityLabel()
    }
    
    public override func updateView(_ size: CGFloat) {
        super.updateView(size)
        progressBar.updateView(size)
        leftLabel.updateView(size)
        rightLabel.updateView(size)
    }
    
    override open func reset() {
        super.reset()
        progressBar.reset()
        leftLabel.reset()
        rightLabel.reset()
        leftLabel.setFontStyle(.BoldBodySmall)
        rightLabel.setFontStyle(.BoldBodySmall)
    }
    
    //-----------------------------------------------------
    // 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? ListProgressBarDataModel else { return }
        
        progressBar.set(with: model.progressBar, delegateObject, additionalData)
        leftLabel.set(with: model.leftLabel, delegateObject, additionalData)
        rightLabel.set(with: model.rightLabel, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 90
    }
    
    //------------------------------------------------------
    // MARK: - Accessibility
    //------------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        var message = ""
        
        if let leftLabelText = leftLabel.text, !leftLabelText.isEmpty {
            message += leftLabelText + ", "
        }
        
        if let rightLabelText = rightLabel.text, !rightLabelText.isEmpty {
            message += rightLabelText + ", "
        }
        
        if let progressList = progressBar.multiProgressModel?.progressList {
            for progress in progressList {
                message += "\(progress.percent)%, "
            }
        }
    
        accessibilityLabel = message
    }
}
