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

import Foundation

open class ImageButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWatcherFieldProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "imageButton"
    public var backgroundColor: Color?

    public var image: ImageViewModel?
    
    public var accessibilityText: String?
    public var action: ActionModelProtocol
    public var enabled: Bool = true
    public var enabledTintColor: Color?
    public var disabledTintColor: Color?

    public var groupName: String = ""
    
    public var updateUI: ActionBlock?
    
    public init(image: ImageViewModel?, action: ActionModelProtocol) {
        self.image = image
        self.action = action
    }
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case image
        case backgroundColor
        case accessibilityText
        case action
        case enabled
        case groupName
        case enabledTintColor
        case disabledTintColor
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        image = try typeContainer.decodeIfPresent(ImageViewModel.self, forKey: .image)
        accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
        action = try typeContainer.decodeModel(codingKey: .action)
        
        if let enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) {
            self.enabled = enabled
        }
        
        if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) {
            self.groupName = groupName
        }
        
        if let enabledTintColor = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledTintColor) {
            self.enabledTintColor = enabledTintColor
        }
        
        if let disabledTintColor = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledTintColor) {
            self.disabledTintColor = disabledTintColor
        }
        
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(image, forKey: .image)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
        try container.encode(enabled, forKey: .enabled)
        try container.encodeModel(action, forKey: .action)
        try container.encodeIfPresent(groupName, forKey: .groupName)
        try container.encodeIfPresent(enabledTintColor, forKey: .enabledTintColor)
        try container.encodeIfPresent(disabledTintColor, forKey: .disabledTintColor)
    }
}
