//
//  NavigationLabelButtonModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 5/27/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


open class NavigationLabelButtonModel: NavigationButtonModelProtocol, MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    open var backgroundColor: Color?
    open class var identifier: String { "navigationLabelButton" }
    open var accessibilityIdentifier: String?
    open var title: String
    open var action: ActionModelProtocol
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(with title: String, action: ActionModelProtocol) {
        self.title = title
        self.action = action
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case accessibilityIdentifier
        case title
        case action
    }
    
    //--------------------------------------------------
    // 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)
        title = try typeContainer.decode(String.self, forKey: .title)
        action = try typeContainer.decodeModel(codingKey: .action)
    }
    
    open func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encode(title, forKey: .title)
        try container.encodeModel(action, forKey: .action)
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    /// Convenience function that creates a BarButtonItem for the model.
    open func createNavigationItemButton(delegateObject: MVMCoreUIDelegateObject? = nil, additionalData: [AnyHashable: Any]? = nil) -> UIBarButtonItem {
        return LabelBarButtonItem.create(with: title, model: self, delegateObject: delegateObject, additionalData: additionalData)
    }
}
