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


@objcMembers open class ListRightVariablePayments: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-------------------------------------------------------
    
    let leftLabel = Label(fontStyle: .BoldBodySmall)
    let rightImage = LoadImageView(pinnedEdges: .all)
    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: rightImage, model: StackItemModel(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()
        rightImage.addSizeConstraintsForAspectRatio = true
        addMolecule(stack)
        stack.restack()
    }
    
    //----------------------------------------------------
    // MARK: - Molecule
    //------------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? ListRightVariablePaymentsModel else { return }
        
        leftLabel.set(with: model.leftLabel, delegateObject, additionalData)
        rightImage.set(with: model.image, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 65
    }
    
    open override func reset() {
        super.reset()
        leftLabel.setFontStyle(.BoldBodySmall)
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        isAccessibilityElement = true
        var message = ""
        
        if let leftLabelText = leftLabel.text, !leftLabelText.isEmpty {
            message += leftLabelText + ", "
        }
        
        if let rightImageText = rightImage.imageView.accessibilityLabel, !rightImageText.isEmpty {
            message += rightImageText
        }
        
        accessibilityLabel = message
        accessibilityTraits = (accessoryView != nil) ? .button : .none
    }
}
