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

open class DataLine: View {
    
    lazy var heightConstraint = heightAnchor.constraint(equalToConstant: 4)
    lazy var widthConstraint = widthAnchor.constraint(equalToConstant: 20)
    
    open override func setupView() {
        super.setupView()
        heightConstraint.isActive = true
        widthConstraint.isActive = true
    }
}

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

    public let leftLabel = Label(fontStyle: .BoldBodySmall)
    public let rightLabel = Label(fontStyle: .RegularBodySmall)
    public let bar = DataLine()
    
    //-----------------------------------------------------
    // 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()
        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)
    }
    
    //--------------------------------------------------
    // 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 = (listItemModel?.accessibilityTraits) ?? ((accessoryView != nil) ? .button : .none)
    }
}
