//
//  ButtonIcon.swift
//  MVMCoreUI
//
//  Created by Matt Bruce on 4/12/24.
//  Copyright © 2024 Verizon Wireless. All rights reserved.
//

import Foundation
import VDS

open class ButtonIcon: VDS.ButtonIcon, VDSMoleculeViewProtocol {

    //--------------------------------------------------
    // MARK: - Public Properties
    //--------------------------------------------------
    public var viewModel: ButtonIconModel!
    
    public var delegateObject: MVMCoreUIDelegateObject?
    
    public var additionalData: [AnyHashable : Any]?
    
    //--------------------------------------------------
    // MARK: - Public Methods
    //--------------------------------------------------
    
    public func viewModelDidUpdate() {
        surface = viewModel.surface
        
        onClick = { [weak self] control in
            guard let self, let viewModel = self.viewModel else { return }
            MVMCoreUIActionHandler.performActionUnstructured(with: viewModel.action,
                                                             sourceModel: viewModel,
                                                             additionalData: self.additionalData,
                                                             delegateObject: self.delegateObject)
        }
        
        badgeIndicatorModel = viewModel.badgeIndicatorModel
        kind = viewModel.kind
        surfaceType = viewModel.surfaceType
        iconName = viewModel.iconName
        selectedIconName = viewModel.selectedIconName
        size = viewModel.size
        customSize = viewModel.customSize
        floating = viewModel.floating
        fitToIcon = viewModel.fitToIcon
        hideBorder = viewModel.hideBorder
        showBadgeIndicator = viewModel.showBadgeIndicator
        selectable = viewModel.selectable
        iconOffset = viewModel.iconOffset

    }
    
    public func updateView(_ size: CGFloat) {}
    
    //--------------------------------------------------
    // MARK: - Overrides
    //--------------------------------------------------
    open override func updateAccessibility() {
        super.updateAccessibility()
        
        if let viewModel {
            if let accessibilityText = viewModel.accessibilityText {
                //since this is a container control and the
                //icon & badgeIndicator (gets from it's own model) are traversed separatly
                icon.accessibilityLabel = accessibilityText
            }
        }
    }
    
}

//to deal with how it's parent constrains this control
extension ButtonIcon: MVMCoreUIViewConstrainingProtocol {

    public func needsToBeConstrained() -> Bool { true }
    
    public func horizontalAlignment() -> UIStackView.Alignment { .leading }
}

