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


@objcMembers open class ListLeftVariableIconWithRightCaretBodyText: TableViewCell {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    public let leftImage = LoadImageView(pinnedEdges: .all)
    public let headlineBody = HeadlineBody()
    public let rightLabel = Label(fontStyle: .RegularBodySmall)
    public let rightLabelStackItem: StackItem
    public var stack: Stack<StackModel>

    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        rightLabelStackItem = StackItem(andContain: rightLabel)
        let stackItems = [StackItem(andContain: leftImage), StackItem(andContain: headlineBody), rightLabelStackItem]
        let stackModel = StackModel(molecules: [StackItemModel(horizontalAlignment: .fill), StackItemModel(horizontalAlignment: .fill), StackItemModel(horizontalAlignment: .fill)], axis: .horizontal)
        stack = Stack<StackModel>(with: stackModel, stackItems: stackItems)
        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 label is centered vertically with headline.
        let heroCenter = super.alignAccessoryToHero()
        if let heroCenter = heroCenter {
            let convertedPoint = stack.convert(heroCenter, from: self)
            rightLabelStackItem.containerHelper.alignCenterVerticalConstraint?.constant = convertedPoint.y - stack.bounds.midY
        }
        return heroCenter
    }
    
    //-----------------------------------------------------
    // MARK: - View Lifecycle
    //--------------------------------------------------
    
    override open func setupView() {
        super.setupView()
        
        leftImage.addSizeConstraintsForAspectRatio = true
        leftImage.contentMode = .scaleAspectFit
        rightLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        rightLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 900), 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? ListLeftVariableIconWithRightCaretBodyTextModel else { return }
        
        leftImage.set(with: model.image, delegateObject, additionalData)
        headlineBody.set(with: model.headlineBody, 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()
        rightLabel.setFontStyle(.RegularBodySmall)
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------

    func updateAccessibilityLabel() {
        
        isAccessibilityElement = true
        var message = ""
        
        if let leftImageLabel = leftImage.imageView.accessibilityLabel {
            message += leftImageLabel + ", "
        }
        
        if let headlineLabel = headlineBody.headlineLabel.text {
            message += headlineLabel + ", "
        }
        
        if let messageLabel = headlineBody.messageLabel.text {
            message += messageLabel + ", "
        }
        
        if let rightLabel = rightLabel.text {
            message += rightLabel
        }
        
        accessibilityLabel = message
        accessibilityTraits = (accessoryView != nil) ? .button : .none
    }
}
