//
//  ListLeftVariableNumberedListBodyText.swift
//  MVMCoreUI
//
//  Created by Subhankar Acharya on 12/06/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers open class ListLeftVariableNumberedListBodyText: TableViewCell {
    //--------------------------------------------------
    // MARK: - Outlets
    //----------------------------------------------------
    
    public let leftLabel = Label(fontStyle: .Title2XLarge)
    public let headlineBody = HeadlineBody()
    public var stack: Stack<StackModel>
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        stack = Stack<StackModel>.createStack(with: [(view: leftLabel, model: StackItemModel(horizontalAlignment: .fill)),
                                                     (view: headlineBody, model: StackItemModel(horizontalAlignment: .leading))],
                                              axis: .horizontal)
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //--------------------------------------------------
    // MARK: - Life Cycle
    //--------------------------------------------------
    override open func setupView() {
        super.setupView()
        leftLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        leftLabel.setContentHuggingPriority(.defaultHigh, for: .vertical)
        leftLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        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? ListLeftVariableNumberedListBodyTextModel else { return }
        leftLabel.text = String(model.number)
        headlineBody.set(with: model.headlineBody, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 140
    }
    
    open override func reset() {
        super.reset()
        leftLabel.setFontStyle(.Title2XLarge)
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        isAccessibilityElement = true
        
        var message = ""
        
        if let leftLabel = leftLabel.text {
            message += leftLabel + ", "
        }
        
        if let headlineLabel = headlineBody.headlineLabel.text {
            message += headlineLabel + ", "
        }
        
        if let messageLabel = headlineBody.messageLabel.text {
            message += messageLabel
        }
        
        accessibilityLabel = message
        accessibilityTraits = (accessoryView != nil) ? .button : .none
    }
}

