//
//  Link.swift
//  MVMCoreUI
//
//  Created by Robinson, Blake on 11/26/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit
import VDSColorTokens
import VDS

open class Link: VDS.TextLink, VDSMoleculeViewProtocol {
    
    //--------------------------------------------------
    // MARK: - Public Properties
    //--------------------------------------------------
    open var viewModel: LinkModel!
    open var delegateObject: MVMCoreUIDelegateObject?
    open var additionalData: [AnyHashable : Any]?
    
    //--------------------------------------------------
    // MARK: - Public Functions
    //--------------------------------------------------
    open func viewModelDidUpdate() {
        isEnabled = viewModel.enabled
        size = viewModel.size
        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
    //--------------------------------------------------
    open class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject:  MVMCoreUIDelegateObject?) -> CGFloat? { 31 }
    
    open func updateView(_ size: CGFloat) { }
    
}

// MARK: - MVMCoreUIViewConstrainingProtocol
extension Link: MVMCoreUIViewConstrainingProtocol {
    
    open func horizontalAlignment() -> UIStackView.Alignment { .leading }
}
