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

@objcMembers open class DateDropdownEntryFieldModel: BaseDropdownEntryFieldModel {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public override class var identifier: String { "dateDropdownEntryField" }
    
    public var dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeZone = NSTimeZone.system
        formatter.locale = .current
        formatter.formatterBehavior = .default
        return formatter
    }()
    
    /// Update the property value to alter the format of how the date is presented.
    public var dateFormat: String = "MMM d, y" {
        didSet { dateFormatter.dateFormat = dateFormat }
    }
    
    public var date: Date?
    public var minDate: Date?
    public var maxDate: Date?
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case date
        case dateFormat
        case minDate
        case maxDate
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        try super.init(from: decoder)
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        if let dateFormat = try typeContainer.decodeIfPresent(String.self, forKey: .dateFormat) {
            self.dateFormat = dateFormat
            dateFormatter.dateFormat = dateFormat
        }
        
        if let date = try typeContainer.decodeIfPresent(String.self, forKey: .date) {
            baseValue = date
            self.date = dateFormatter.date(from: date)
        }
        
        if let minDate = try typeContainer.decodeIfPresent(String.self, forKey: .minDate) {
            self.minDate = dateFormatter.date(from: minDate)
        }
        
        if let maxDate = try typeContainer.decodeIfPresent(String.self, forKey: .maxDate) {
            self.maxDate = dateFormatter.date(from: maxDate)
        }
    }
    
    public override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(dateFormat, forKey: .dateFormat)
        
        if let date = date {
            try container.encode(dateFormatter.string(from: date), forKey: .date)
        }
        
        if let minDate = minDate {
            try container.encode(dateFormatter.string(from: minDate), forKey: .minDate)
        }
        
        if let maxDate = maxDate {
            try container.encode(dateFormatter.string(from: maxDate), forKey: .maxDate)
        }
    }
}
