//
//  ListDeviceRightVariableCaret.swift
//  MobileFirstFramework
//
//  Created by Lekshmi S on 09/12/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//
import MVMCoreUI

@objcMembers open class ListDeviceRightVariableCaret: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------
    public let eyebrow = Label(fontStyle: .RegularMicro)
    public let headline = Label(fontStyle: .BoldBodySmall)
    public let body1 = Label(fontStyle: .RegularBodySmall)
    public let body2 = Label(fontStyle: .RegularBodySmall)
    public let body3 = Label(fontStyle: .RegularBodySmall)
    public let rightLabel = Label(fontStyle: .RegularBodySmall)
    public let rightImage = LoadImageView()
    public weak var rightVerticalStackTopConstraint: NSLayoutConstraint?
    
    public lazy var leftVerticalStack: Stack<StackModel> = {
        return Stack<StackModel>.createStack(with: [eyebrow, headline, body1, body2, body3], axis: .vertical, spacing: 2)
    }()
    
    public lazy var rightVerticalStack: Stack<StackModel> = {
        return Stack<StackModel>.createStack(with: [(view: rightLabel, model: StackItemModel(horizontalAlignment: .trailing)),
                                                    (view: rightImage, model: StackItemModel(horizontalAlignment: .trailing))],
                                             axis: .vertical)
    }()
    
    public lazy var stack: Stack<StackModel> = {
        return Stack<StackModel>.createStack(with: [(view: leftVerticalStack, model: StackItemModel(verticalAlignment: .top)),
                                                    (view: rightVerticalStack, model: StackItemModel(verticalAlignment: .top))],
                                             axis: .horizontal)
    }()
    
    //-----------------------------------------------------
    // MARK: - Lifecycle
    //-----------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        addMolecule(stack)
        rightImage.imageView.contentMode = .scaleAspectFit
        rightLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        rightLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 900), for: .horizontal)
        rightLabel.numberOfLines = 1
        leftVerticalStack.restack()
        rightVerticalStack.restack()
        stack.restack()
    }
    
    func alignRightStack(toHeadline: Bool) {
        if toHeadline {
            rightVerticalStack.containerHelper.topConstraint?.isActive = false
            if rightVerticalStackTopConstraint == nil {
                rightVerticalStackTopConstraint = headline.topAnchor.constraint(equalTo: rightVerticalStack.contentView.topAnchor)
            }
            rightVerticalStackTopConstraint?.isActive = true
        } else {
            rightVerticalStack.containerHelper.topConstraint?.isActive = true
            rightVerticalStackTopConstraint?.isActive = false
        }
    }
    
    //------------------------------------------------------
    // 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? ListDeviceRightVariableCaretModel else { return }
        leftVerticalStack.updateContainedMolecules(with: [model.eyebrow, model.headline, model.body1, model.body2, model.body3], delegateObject, additionalData)
        rightVerticalStack.updateContainedMolecules(with: [model.rightLabel, model.image], delegateObject, additionalData)
        alignRightStack(toHeadline: model.headline != nil)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 230
    }
    
    open override func reset() {
        super.reset()
        eyebrow.setFontStyle(.RegularMicro)
        headline.setFontStyle(.BoldBodySmall)
        body1.setFontStyle(.RegularBodySmall)
        body2.setFontStyle(.RegularBodySmall)
        body3.setFontStyle(.RegularBodySmall)
        rightLabel.setFontStyle(.RegularBodySmall)
        eyebrow.textColor = .mvmCoolGray6
    }
    
    //----------------------------------------------------
    // MARK: - Accessibility
    //----------------------------------------------------
    
    func updateAccessibilityLabel() {
        isAccessibilityElement = true
        var message = ""
        
        if let eyebrowText = eyebrow.text, !eyebrowText.isEmpty {
            message += eyebrowText + ", "
        }
        if let headlineText = headline.text, !headlineText.isEmpty {
            message += headlineText + ", "
        }
        if let body1Text = body1.text, !body1Text.isEmpty {
            message += body1Text + ", "
        }
        if let body2Text = body2.text, !body2Text.isEmpty {
            message += body2Text + ", "
        }
        if let body3Text = body3.text, !body3Text.isEmpty {
            message += body3Text + ", "
        }
        if let rightLabelText = rightLabel.text, !rightLabelText.isEmpty {
            message += rightLabelText + ", "
        }
        if let rightImageViewText = rightImage.imageView.accessibilityLabel, !rightImageViewText.isEmpty {
            message += rightImageViewText
        }
        
        accessibilityLabel = message
        accessibilityTraits = (accessoryView != nil) ? .button : .none
    }
}
