//
//  CaretViewModel.swift
//  MVMCoreUI
//
//  Created by Ryan on 11/20/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import Foundation


@objcMembers public class CaretViewModel: MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "caretView"
    public var backgroundColor: Color?
    public var strokeColor: Color = Color(uiColor: .mvmBlack)
    public var strokeColor_inverted: Color = Color(uiColor: .mvmWhite)
    public var isHidden: Bool = false
    public var isOpaque: Bool = false
    public var inverted: Bool = false
    public var lineWidth: CGFloat?
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case strokeColor
        case strokeColor_inverted
        case isHidden
        case isOpaque
        case lineWidth
        case inverted
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        if let strokeColor = try typeContainer.decodeIfPresent(Color.self, forKey: .strokeColor) {
            self.strokeColor = strokeColor
        }
        
        if let strokeColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .strokeColor_inverted) {
            self.strokeColor_inverted = strokeColor_inverted
        }
        
        if let inverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) {
            self.inverted = inverted
        }
        
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        
        if let isHidden = try typeContainer.decodeIfPresent(Bool.self, forKey: .isHidden) {
            self.isHidden = isHidden
        }
        
        if let isOpaque = try typeContainer.decodeIfPresent(Bool.self, forKey: .isOpaque) {
            self.isOpaque = isOpaque
        }
        
        lineWidth = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .lineWidth)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(strokeColor, forKey: .strokeColor)
        try container.encode(strokeColor_inverted, forKey: .strokeColor_inverted)
        try container.encode(inverted, forKey: .inverted)
        try container.encodeIfPresent(isHidden, forKey: .isHidden)
        try container.encodeIfPresent(isOpaque, forKey: .isOpaque)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(lineWidth, forKey: .lineWidth)
    }
}
