//
//  ListThreeColumnInternationalData.swift
//  MVMCoreUI
//
//  Created by Kruthika KP on 06/04/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers public class ListThreeColumnInternationalData: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------
    
    open var stack: Stack<StackModel>
    public let leftLabel = Label(fontStyle: .RegularBodySmall)
    public let centerLabel = Label(fontStyle: .RegularBodySmall)
    public let rightLabel = Label(fontStyle: .RegularBodySmall)
    public let arrow = Arrow()
    public let arrowAndLabel2Stack: Stack<StackModel>
    
    //-----------------------------------------------------
    // MARK: - Initializers
    //-----------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        arrowAndLabel2Stack = Stack<StackModel>.createStack(with: [(view: arrow, model: StackItemModel(horizontalAlignment: .fill)),
                                                                   (view: centerLabel, model: StackItemModel(horizontalAlignment: .leading))],
                                                            axis: .horizontal, spacing: 4)
        centerLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        centerLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        stack = Stack<StackModel>.createStack(with: [(view: leftLabel, model: StackItemModel(percent: 30, horizontalAlignment: .leading)),
                                                     (view: arrowAndLabel2Stack, model: StackItemModel(percent: 50, horizontalAlignment: .fill)),
                                                     (view: rightLabel, model: StackItemModel(percent:20,horizontalAlignment: .leading))],
                                              axis: .horizontal)
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //-----------------------------------------------------
    // MARK: - Lifecycle
    //-----------------------------------------------------
    
    override open func setupView() {
        super.setupView()
        
        addMolecule(stack)
        arrowAndLabel2Stack.restack()
        stack.restack()
    }
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? ListThreeColumnInternationalDataModel else { return }
        
        leftLabel.set(with: model.leftLabel, delegateObject, additionalData)
        centerLabel.set(with: model.centerLabel, delegateObject, additionalData)
        rightLabel.set(with: model.rightLabel, delegateObject, additionalData)
        arrow.set(with: model.arrow, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 90
    }
    
    open override func reset() {
        super.reset()
        leftLabel.setFontStyle(.RegularBodySmall)
        centerLabel.setFontStyle(.RegularBodySmall)
        rightLabel.setFontStyle(.RegularBodySmall)
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        isAccessibilityElement = true
        var message = ""
        
        if let leftText = leftLabel.text, !leftText.isEmpty {
            message += leftText + ", "
        }
        
        if let centerText = centerLabel.text, !centerText.isEmpty {
            message += centerText + ", "
        }
        
        if let rightText = rightLabel.text, !rightText.isEmpty {
            message += rightText
        }
        
        accessibilityLabel = message
    }
}
