//
//  ListRightVariablePriceChangeBodyText.swift
//  MVMCoreUI
//
//  Created by Dhamodaram Nandi on 13/04/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


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

    public let headlineBody = HeadlineBody()
    public let arrow = Arrow()
    public let rightLabel = Label(fontStyle: .RegularBodySmall)
    let arrowAndRightLabelStack: Stack<StackModel>
    private let stack: Stack<StackModel>
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        arrowAndRightLabelStack = Stack<StackModel>.createStack(with: [(view: arrow, model: StackItemModel(horizontalAlignment: .fill)),
                                                                       (view: rightLabel, model: StackItemModel(horizontalAlignment: .fill))],
                                                                axis: .horizontal, spacing: 6)
        stack = Stack<StackModel>.createStack(with: [(view: headlineBody, model: StackItemModel(horizontalAlignment: .leading)),
                                                     (view: arrowAndRightLabelStack, model: StackItemModel(horizontalAlignment: .fill, verticalAlignment: .leading))],
                                              axis: .horizontal)
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //-----------------------------------------------------
    // MARK: - Lifecycle
    //-----------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        headlineBody.styleListItem()
        
        rightLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        rightLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        
        addMolecule(stack)
        stack.restack()
        arrowAndRightLabelStack.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? ListRightVariablePriceChangeBodyTextModel else { return }
        
        headlineBody.set(with: model.headlineBody, delegateObject, additionalData)
        rightLabel.set(with: model.rightLabel, delegateObject, additionalData)
        arrow.set(with: model.arrow, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 80
    }
    
    open override func reset() {
        super.reset()
        headlineBody.styleListItem()
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        isAccessibilityElement = true
        var message = ""
        
        if let headlineText = headlineBody.headlineLabel.text, !headlineText.isEmpty {
            message += headlineText + ", "
        }
        
        if let messageText = headlineBody.messageLabel.text, !messageText.isEmpty {
            message += messageText + ", "
        }
        
        if let rightLabelText = rightLabel.text, !rightLabelText.isEmpty {
            message += rightLabelText
        }
        
        accessibilityLabel = message
        accessibilityTraits = (accessoryView != nil) ? .button : .none
    }
}
