//
//  DigitEntryFieldModel.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 1/22/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers open class DigitEntryFieldModel: TextEntryFieldModel {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public override class var identifier: String { "digitTextField" }
    
    public var digits: Int = 4
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case digits
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        try super.init(from: decoder)
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        if let digits = try typeContainer.decodeIfPresent(Int.self, forKey: .digits) {
            self.digits = digits
        }
    }
    
    public override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(digits, forKey: .digits)
    }
}
