//
//  CaretLinkModel.swift
//  MVMCoreUI
//
//  Created by Suresh, Kamlesh on 12/13/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import Foundation
import MVMCore


public class CaretLinkModel: ButtonModelProtocol, MoleculeModelProtocol, EnableableModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "caretLink"
    public var backgroundColor: Color?
    public var accessibilityIdentifier: String?
    public var title: String
    public var action: ActionModelProtocol
    public var enabledColor: Color = Color(uiColor: .mvmBlack)
    public var disabledColor: Color = Color(uiColor: .mvmCoolGray6)
    public var enabledColor_inverted: Color = Color(uiColor: .mvmWhite)
    public var disabledColor_inverted: Color = Color(uiColor: .mvmCoolGray10)
    public var enabled = true
    public var inverted = false
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(title: String, action: ActionModelProtocol) {
        self.title = title
        self.action = action
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case backgroundColor
        case accessibilityIdentifier
        case title
        case action
        case enabledColor_inverted
        case disabledColor_inverted
        case enabledColor
        case disabledColor
        case enabled
        case inverted
        case moleculeName
    }
    
    //--------------------------------------------------
    // 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)
        accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
        title = try typeContainer.decode(String.self, forKey: .title)
        
        if let enabledColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledColor_inverted) {
            self.enabledColor_inverted = enabledColor_inverted
        }
        
        if let disabledColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledColor_inverted) {
            self.disabledColor_inverted = disabledColor_inverted
        }
        
        if let color = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledColor) {
            enabledColor = color
        }
        
        if let color = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledColor) {
            disabledColor = color
        }
        
        if let enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) {
            self.enabled = enabled
        }
        
        if let inverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) {
            self.inverted = inverted
        }
        
        action = try typeContainer.decodeModel(codingKey: .action)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(title, forKey: .title)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encodeModel(action, forKey: .action)
        try container.encode(enabledColor, forKey: .enabledColor)
        try container.encodeIfPresent(disabledColor, forKey: .disabledColor)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(enabledColor_inverted, forKey: .enabledColor_inverted)
        try container.encode(disabledColor_inverted, forKey: .disabledColor_inverted)
        try container.encode(inverted, forKey: .inverted)
    }
}
