//
//  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, ParentMoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "imageButton"
    public var id: String = UUID().uuidString
    
    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 accessibilityTraits: UIAccessibilityTraits?
    public var groupName: String = ""
    
    public var updateUI: ActionBlock?
    
    public var children: [MoleculeModelProtocol] {
        [image].compactMap({$0})
    }
    
    public func replaceChildMolecule(with molecule: MoleculeModelProtocol) throws -> Bool {
        return try replaceChildMolecule(at: &image, with: molecule)
    }
    
    public init(image: ImageViewModel?, action: ActionModelProtocol) {
        self.image = image
        self.action = action
    }
    
    private enum CodingKeys: String, CodingKey {
        case id
        case moleculeName
        case image
        case backgroundColor
        case accessibilityText
        case action
        case enabled
        case groupName
        case enabledTintColor
        case disabledTintColor
        case accessibilityTraits
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
        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)
        accessibilityTraits = try typeContainer.decodeIfPresent(UIAccessibilityTraits.self, forKey: .accessibilityTraits) ?? .button
        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(id, forKey: .id)
        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)
        try container.encodeIfPresent(accessibilityTraits, forKey: .accessibilityTraits)
    }
}
