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


@objcMembers open class ListProgressBarThin: TableViewCell {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    public let progressBar = ProgressBar()
    public let leftHeadline = Label(fontStyle: .BoldBodySmall)
    public let leftBody = Label(fontStyle: .BoldBodySmall)
    public let rightBar = Line()
    public let rightLabel = Label(fontStyle: .BoldBodySmall)
    private let barStackItem: StackItem
    private let rightLabelStackItem: StackItem
    public var labelStack: Stack<StackModel>
    public var horizontalStack: Stack<StackModel>
    public var stack: Stack<StackModel>
    
    //------------------------------------------------------
    // MARK: - Initializers
    //------------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        //vertical stack with leftHeadline, leftBody
        labelStack = Stack<StackModel>.createStack(with: [leftHeadline, leftBody], axis: .vertical, spacing: 2)
        
        //horizontal stack with leftHeadline, leftBody, bar, rightLabel
        barStackItem = StackItem(andContain: rightBar)
        rightLabelStackItem = StackItem(andContain: rightLabel)
        let horizontalStackItems = [StackItem(andContain: labelStack), barStackItem, rightLabelStackItem]
        let horizontalStackModel = StackModel(molecules: [StackItemModel(horizontalAlignment: .leading), StackItemModel(horizontalAlignment: .fill), StackItemModel(spacing: 5, horizontalAlignment: .fill)],
                                              axis: .horizontal)
        horizontalStack = Stack<StackModel>(with: horizontalStackModel, stackItems: horizontalStackItems)
        
        //stack with all components
        stack = Stack<StackModel>.createStack(with: [horizontalStack, progressBar], axis: .vertical, spacing: PaddingDefaultVerticalSpacing3)
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open override func alignAccessoryToHero() -> CGPoint? {
        // Ensures that the right items are centered with the arrow.
        let heroCenter = super.alignAccessoryToHero()
        
        if let heroCenter = heroCenter {
            let convertedPoint = horizontalStack.convert(heroCenter, from: self)
            barStackItem.containerHelper.alignCenterVerticalConstraint?.constant = convertedPoint.y - horizontalStack.bounds.midY
            rightLabelStackItem.containerHelper.alignCenterVerticalConstraint?.constant = convertedPoint.y - horizontalStack.bounds.midY
        }
        
        return heroCenter
    }
    
    //-------------------------------------------------------
    // MARK: -  Lifecycle
    //-------------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        
        rightBar.widthAnchor.constraint(equalToConstant: 20).isActive = true
        rightLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        rightLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        addMolecule(stack)
        stack.restack()
        horizontalStack.restack()
        labelStack.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? ListProgressBarThinModel else { return }
        
        labelStack.updateContainedMolecules(with: [model.leftHeadline, model.leftBody], delegateObject, additionalData)
        progressBar.set(with: model.progressBar, delegateObject, additionalData)
        rightBar.set(with: model.rightBar, delegateObject, additionalData)
        rightLabel.set(with: model.rightLabel, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 120
    }
    
    open override func reset() {
        super.reset()
        leftHeadline.styleB1(true)
        leftBody.styleB2(true)
        rightLabel.styleB2(true)
        rightBar.setStyle(.medium)
    }
    
    //------------------------------------------------------
    // MARK: - Accessibility
    //------------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        isAccessibilityElement = true
        var message = ""
        
        if let leftHeadlineText = leftHeadline.text {
            message += leftHeadlineText + ", "
        }
        
        if let leftBodyText = leftBody.text {
            message += leftBodyText + ", "
        }
        
        if let progressLabel = progressBar.accessibilityLabel, let progressValue = progressBar.accessibilityValue {
            message +=  progressLabel + ", " + progressValue + ", "
        }
        
        if let rightLabelText = rightLabel.text {
            message += rightLabelText + ", "
        }
        
        accessibilityLabel = message
        accessibilityTraits = (accessoryView != nil) ? .button : .none
    }
}
