//
//  ImageButton.swift
//  MobileFirstFramework
//
//  Created by Chintakrinda, Arun Kumar (Arun) on 07/10/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

@objcMembers open class ImageButton: Button {
    
    public let image = LoadImageView(pinnedEdges: .all)
    
    open override func setupView() {
        super.setupView()
        insertSubview(image, at: 0)
        image.isUserInteractionEnabled = false
        NSLayoutConstraint.constraintPinSubview(toSuperview: image)
    }
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        guard let castModel = model as? ImageButtonModel else {
            super.set(with: model, delegateObject, additionalData)
            return
        }
        image.setOptional(with: castModel.image, delegateObject, additionalData)
        castModel.updateUI = { [weak self] in
            MVMCoreDispatchUtility.performBlock(onMainThread: {
                self?.setState()
            })
        }
        
        super.set(with: model, delegateObject, additionalData)
        FormValidator.setupValidation(for: castModel, delegate: delegateObject?.formHolderDelegate)

    }
    
    public func setState() {
        guard let castModel = model as? ImageButtonModel else {
            return
        }
        if castModel.enabled {
            if let enabledTintColor = castModel.enabledTintColor {
                image.imageView.tintColor = enabledTintColor.uiColor
            }
        } else if let disabledTintColor = castModel.disabledTintColor {
            image.imageView.tintColor = disabledTintColor.uiColor
        }
    }
    
    

}
