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

import Foundation
import MVMCore
import VDS

public class CaretLinkModel: ButtonModelProtocol, MoleculeModelProtocol, EnableableModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "caretLink"
    public var id: String = UUID().uuidString
    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 inverted = false
    public var iconPosition: TextLinkCaret.IconPosition = .right
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(title: String, action: ActionModelProtocol) {
        self.title = title
        self.action = action
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case id
        case backgroundColor
        case accessibilityIdentifier
        case accessibilityText
        case iconPosition
        case title
        case action
        case enabled
        case inverted
        case moleculeName
    }
    
    //--------------------------------------------------
    // 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)
        accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
        accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
        title = try typeContainer.decode(String.self, forKey: .title)
             
        if let iconPosition = try typeContainer.decodeIfPresent(TextLinkCaret.IconPosition.self, forKey: .iconPosition) {
            self.iconPosition = iconPosition
        }
        
        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(id, forKey: .id)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(iconPosition, forKey: .iconPosition)
        try container.encode(title, forKey: .title)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
        try container.encodeModel(action, forKey: .action)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(inverted, forKey: .inverted)
    }
}

extension CaretLinkModel {
    public var surface: Surface { inverted ? .dark : .light }
}
