//
//  CaretLink.swift
//  MVMCoreUI
//
//  Created by Kolli, Praneeth on 1/5/18.
//  Created by Christiano, Kevin on 3/18/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//
import VDS

open class CaretLink: VDS.TextLinkCaret, VDSMoleculeViewProtocol {

    //--------------------------------------------------
    // MARK: - Public Properties
    //--------------------------------------------------
    open var viewModel: CaretLinkModel!
    open var delegateObject: MVMCoreUIDelegateObject?
    open var additionalData: [AnyHashable : Any]?
    
    //--------------------------------------------------
    // MARK: - Public Functions
    //--------------------------------------------------
    open func viewModelDidUpdate() {
        isEnabled = viewModel.enabled
        iconPosition = viewModel.iconPosition
        text = viewModel.title
        surface = viewModel.surface
        onClick = { [weak self] control in
            guard let self else { return }
            MVMCoreUIActionHandler.performActionUnstructured(with: self.viewModel.action,
                                                             sourceModel: self.viewModel,
                                                             additionalData: self.additionalData,
                                                             delegateObject: self.delegateObject)
        }
    }

    //--------------------------------------------------
    // MARK: - Overrides
    //--------------------------------------------------
    open override func updateAccessibility() {
        super.updateAccessibility()
        
        guard let viewModel = viewModel else { return }
        if let accessibilityText = viewModel.accessibilityText {
            self.accessibilityLabel = accessibilityText
        }
        
        if let accessibilityIdentifier = viewModel.accessibilityIdentifier {
            self.accessibilityIdentifier = accessibilityIdentifier
        }
    }

    //--------------------------------------------------
    // MARK: - MVMCoreViewProtocol
    //--------------------------------------------------
    public func updateView(_ size: CGFloat) {}
    
    open class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { 10.5 }
}

// MARK: - MVMCoreUIViewConstrainingProtocol
extension CaretLink: MVMCoreUIViewConstrainingProtocol {
    public func needsToBeConstrained() -> Bool { true }
    
    open func horizontalAlignment() -> UIStackView.Alignment { .leading }
}
