//
//  ListRVWheel.swift
//  MVMCoreUI
//
//  Created by Arora, Prateek on 20/01/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers open class ListRVWheel: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Properties
    //-----------------------------------------------------
    
    let wheel = Wheel(frame: .zero)
    let leftLabel = Label(fontStyle: .BoldBodySmall)
    let rightLabel = Label(fontStyle: .RegularBodySmall)
    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: wheel, 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: - Setup
    //-------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        
        rightLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        addMolecule(stack)
        stack.restack()
    }
    
    //-------------------------------------------------
    // 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? ListRVWheelModel else { return }
        
        leftLabel.set(with: model.leftLabel, delegateObject, additionalData)
        rightLabel.set(with: model.rightLabel, delegateObject, additionalData)
        wheel.set(with: model.wheel, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override func reset() {
        super.reset()
        leftLabel.setFontStyle(.BoldBodySmall)
        rightLabel.setFontStyle(.RegularBodySmall)
    }
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 70
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    
    func updateAccessibilityLabel() {
        isAccessibilityElement = true
        var message = ""
        
        if let leftLabelText = leftLabel.text, !leftLabelText.isEmpty {
            message += leftLabelText
        }
        
        if let wheelLabel = wheel.accessibilityLabel, !wheelLabel.isEmpty {
            message += wheelLabel
        }
        
        if let rightLabelText = rightLabel.text, !rightLabelText.isEmpty {
            message += rightLabelText
        }
        
        accessibilityLabel = message
        accessibilityTraits = (accessoryView != nil) ? .button : .none
    }
}
