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

@objcMembers open class ListStarRating: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------
    public let stars = Stars()
    public var reviewsCount = Label(fontStyle: .RegularBodySmall)
    public lazy var stack: Stack<StackModel> = {
        return Stack<StackModel>.createStack(with: [(view: stars, model: StackItemModel(horizontalAlignment: .fill)),
                                                    (view: reviewsCount, model: StackItemModel(horizontalAlignment: .leading))],
                                             axis: .horizontal, spacing: Padding.Two)
      }()
    
    //-----------------------------------------------------
    // MARK: - Lifecycle
    //-----------------------------------------------------
    override open func setupView() {
        super.setupView()
        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? ListStarRatingModel else { return }
        stars.set(with: model.stars, delegateObject, additionalData)
        reviewsCount.set(with: model.reviewsCount, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 90
    }
    
    // MARK: - Accessibility
    func updateAccessibilityLabel() {
        isAccessibilityElement = true
        var message = ""
        if let starsLabel = stars.accessibilityLabel {
            message +=  starsLabel + ", "
        }
        
        if let reviewsCountLabelText = reviewsCount.text, let reviewText =  MVMCoreUIUtility.hardcodedString(withKey: "review_text") {
            message += reviewsCountLabelText + reviewText
        }
        accessibilityLabel = message
    }
}
