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


@objcMembers open class ListRightVariableTotalData: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------

    public let leftLabel = Label(fontStyle: .BoldBodySmall)
    public let rightLabel = Label(fontStyle: .RegularBodySmall)
    public let bar = Line()
    
    //-----------------------------------------------------
    // MARK: - Properties
    //-----------------------------------------------------
    
    public var stack: Stack<StackModel>
    
    //-----------------------------------------------------
    // MARK: - Initializers
    //-----------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        stack = Stack<StackModel>.createStack(with: [(view: leftLabel, model: StackItemModel(horizontalAlignment: .leading)),
                                                     (view: bar, model: StackItemModel(horizontalAlignment: .fill)),
                                                     (view: rightLabel, model: StackItemModel(spacing: 4, horizontalAlignment: .fill))],
                                              axis: .horizontal)
        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()
        bar.setStyle(.heavy)
        bar.widthAnchor.constraint(equalToConstant: 20).isActive = true
        rightLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        addMolecule(stack)
        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? ListRightVariableTotalDataModel else { return }
        
        leftLabel.set(with: model.leftLabel, delegateObject, additionalData)
        rightLabel.set(with: model.rightLabel, delegateObject, additionalData)
        bar.set(with: model.bar, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with molecule: MoleculeModelProtocol?, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 70
    }
    
    open override func reset() {
        super.reset()
        leftLabel.setFontStyle(.BoldBodySmall)
        rightLabel.setFontStyle(.RegularBodySmall)
        bar.setStyle(.heavy)
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        isAccessibilityElement = true
        var message = ""
        
        if let leftLabelText = leftLabel.text, !leftLabelText.isEmpty {
            message += leftLabelText + ", "
        }
        
        if let rightLabelText = rightLabel.text, !rightLabelText.isEmpty {
            message += rightLabelText
        }
        
        accessibilityLabel = message
        accessibilityTraits = (accessoryView != nil) ? .button : .none
    }
}
