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

@objcMembers open class ListStoreLocator: TableViewCell {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    public let badge = Badge()
    public let leftHeadline = Label(fontStyle: .BoldTitleSmall)
    public let leftBody = Label(fontStyle: .RegularMicro)
    public let leftSubBody = Label(fontStyle: .RegularBodySmall)
    public let rightLabel = Label(fontStyle: .RegularBodySmall)
    public var model: ListStoreLocatorModel?
    
    public lazy var horizontalStack: Stack<StackModel> = {
        return Stack<StackModel>(with: StackModel(molecules: [StackItemModel(horizontalAlignment: .fill),
                                                              StackItemModel(horizontalAlignment: .trailing, verticalAlignment: .center)],
                                                                                                                axis: .horizontal, spacing: Padding.Two), stackItems: [StackItem(andContain: stack), StackItem(andContain: rightLabel)])
    }()
    
    public lazy var stack: Stack<StackModel> = {
        return Stack<StackModel>(with: .init(molecules: [StackItemModel(horizontalAlignment: .leading),
                                         StackItemModel(horizontalAlignment: .fill), StackItemModel(horizontalAlignment: .fill), StackItemModel(horizontalAlignment: .fill)],
                                   axis: .vertical, spacing: Padding.One), stackItems: [StackItem(andContain: badge), StackItem(andContain: leftHeadline), StackItem(andContain: leftBody), StackItem(andContain: leftSubBody)])
        
      }()
    
    //-------------------------------------------------------
    // MARK: -  Lifecycle
    //-------------------------------------------------------
    open override func setupView() {
        super.setupView()
        
        rightLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        rightLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        addMolecule(horizontalStack)
        stack.restack()
        horizontalStack.restack()
        leftSubBody.textColor = UIColor.mfTextLightGray()
    }
    
    //------------------------------------------------------
    // 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? ListStoreLocatorModel else { return }
        stack.updateContainedMolecules(with: [model.badge, model.leftHeadline, model.leftBody, model.leftSubBody], delegateObject, additionalData)
        rightLabel.set(with: model.rightLabel, delegateObject, additionalData)
        self.model = model
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { 120 }
    
    open override func reset() {
        super.reset()
        leftHeadline.setFontStyle(.BoldTitleSmall)
        leftBody.setFontStyle(.RegularMicro)
        leftSubBody.setFontStyle(.RegularBodySmall)
        rightLabel.setFontStyle(.RegularBodySmall)
        leftSubBody.textColor = UIColor.mfTextLightGray()
    }
    
    override public var accessibilityTraits: UIAccessibilityTraits {
        get {
            return (listItemModel?.accessibilityTraits) ?? ((accessoryView != nil) ? .button : .none)
        }
        set { }
    }
    
    func updateAccessibilityLabel() {
        isAccessibilityElement = true
        let message = [model?.badge?.text, model?.leftHeadline.text, model?.leftBody.text, model?.leftSubBody.text, model?.rightLabel.text].compactMap { $0 }.joined(separator: ", ")
        accessibilityLabel = message
    }
}
