//
//  LinkModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 1/13/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit


open class LinkModel: ButtonModelProtocol, MoleculeModelProtocol, EnableableModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public class var identifier: String { "link" }
    
    public var backgroundColor: Color?
    public var accessibilityIdentifier: String?
    public var title: String
    public var accessibilityText: String?
    public var action: ActionModelProtocol
    public var enabled = true
    public var enabledColor = Color(uiColor: .mvmBlack)
    public var enabledColor_inverted = Color(uiColor: .mvmWhite)
    public var disabledColor = Color(uiColor: .mvmCoolGray6)
    public var disabledColor_inverted = Color(uiColor: .mvmCoolGray10)
    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 moleculeName
        case backgroundColor
        case accessibilityIdentifier
        case accessibilityText
        case title
        case action
        case enabled
        case enabledColor
        case enabledColor_inverted
        case disabledColor
        case disabledColor_inverted
        case inverted
    }

    //--------------------------------------------------
    // 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)
        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 inverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) {
            self.inverted = inverted
        }
        
        if let enabledColor = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledColor) {
            self.enabledColor = enabledColor
        }
        
        if let enabledColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledColor_inverted) {
            self.enabledColor_inverted = enabledColor_inverted
        }
        
        if let disabledColor = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledColor) {
            self.disabledColor = disabledColor
        }
        
        if let disabledColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledColor_inverted) {
            self.disabledColor_inverted = disabledColor_inverted
        }
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(title, forKey: .title)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encodeModel(action, forKey: .action)
        try container.encode(inverted, forKey: .inverted)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(enabledColor, forKey: .enabledColor)
        try container.encode(enabledColor_inverted, forKey: .enabledColor_inverted)
        try container.encode(disabledColor, forKey: .disabledColor)
        try container.encode(disabledColor_inverted, forKey: .disabledColor_inverted)
    }
}
