//
//  ListLeftVariableIconWithRightCaret.swift
//  MVMCoreUI
//
//  Created by Kruthika KP on 03/02/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers open class ListLeftVariableIconWithRightCaret: TableViewCell {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    let leftImage = LoadImageView(pinnedEdges: .all)
    let leftLabel = Label(fontStyle: .RegularBodySmall)
    let rightLabel = Label(fontStyle: .RegularBodySmall)
    var stack: Stack<StackModel>

    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        stack = Stack<StackModel>.createStack(with: [(view: leftImage, model: StackItemModel(horizontalAlignment: .fill)),
                                                     (view: leftLabel, model: StackItemModel(horizontalAlignment: .fill)),
                                                     (view: rightLabel, model: StackItemModel(spacing: 4, horizontalAlignment: .trailing))],
                                              axis: .horizontal)
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //--------------------------------------------------
    // MARK: - View Lifecycle
    //--------------------------------------------------
    
    override open func setupView() {
        super.setupView()
        
        leftImage.addSizeConstraintsForAspectRatio = true
        leftImage.contentMode = .scaleAspectFit
        leftLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 901), for: .horizontal)
        rightLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 902), 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? ListLeftVariableIconWithRightCaretModel else { return}
        
        leftImage.set(with: model.image, delegateObject, additionalData)
        leftLabel.set(with: model.leftLabel, delegateObject, additionalData)
        rightLabel.set(with: model.rightLabel, 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)
        rightLabel.setFontStyle(.RegularBodySmall)
    }
    
    func updateAccessibilityLabel() {
        
        isAccessibilityElement = true
        var message = ""
        
        if let leftImageLabel = leftImage.imageView.accessibilityLabel {
            message += leftImageLabel + ", "
        }
        
        if let leftLabel = leftLabel.text {
            message += leftLabel + ", "
        }
        
        if let rightLabel = rightLabel.text {
            message += rightLabel
        }
        
        accessibilityLabel = message
        accessibilityTraits = (accessoryView != nil) ? .button : .none
    }
}
