//
//  NavigationItemButtonModel.swift
//  
//
//  Created by Scott Pfeil on 5/18/20.
//


public class NavigationImageButtonModel: NavigationButtonModelProtocol, MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var backgroundColor: Color?
    public var accessibilityIdentifier: String?
    public static var identifier: String = "navigationImageButton"
    public var image: String
    public var action: ActionModelProtocol
    public var accessibilityText: String?
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(with image: String, action: ActionModelProtocol) {
        self.image = image
        self.action = action
    }
    
    //--------------------------------------------------
    // MARK: - Coding Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case image
        case action
        case accessibilityIdentifier
        case moleculeName
        case accessibilityText
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
        image = try typeContainer.decode(String.self, forKey: .image)
        action = try typeContainer.decodeModel(codingKey: .action)
        accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
    }
    
    open func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(image, forKey: .image)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeModel(action, forKey: .action)
        try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
    }
    
    //--------------------------------------------------
    // MARK: - Method
    //--------------------------------------------------
    
    /// Convenience function that creates a BarButtonItem for the model.
    public func createNavigationItemButton(delegateObject: MVMCoreUIDelegateObject? = nil, additionalData: [AnyHashable: Any]? = nil) -> UIBarButtonItem {
        let uiImage = MVMCoreCache.shared()?.getImageFromRegisteredBundles(image)
        let buttonItem = ImageBarButtonItem.create(with: uiImage, model: self, delegateObject: delegateObject, additionalData: additionalData)
        buttonItem.accessibilityIdentifier = accessibilityIdentifier ?? image
        if let accessibilityString = accessibilityText {
            buttonItem.accessibilityLabel = accessibilityString
            buttonItem.isAccessibilityElement = true
        }
        
        return buttonItem
    }
}
