//
//  TextViewEntryFieldModel.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 5/7/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit


class TextViewEntryFieldModel: TextEntryFieldModel {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public override class var identifier: String { "textView" }
    
    public var accessibilityText: String?
    public var fontStyle: Styler.Font = Styler.Font.RegularBodyLarge
    public var height: CGFloat?
    public var placeholderTextColor: Color = Color(uiColor: .mvmCoolGray3)
    public var placeholderFontStyle: Styler.Font = Styler.Font.RegularMicro
    public var editable: Bool = true
    public var showsPlaceholder: Bool = false
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case accessibilityText
        case fontStyle
        case height
        case placeholderFontStyle
        case placeholderTextColor
        case editable
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        try super.init(from: decoder)
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        if let placeholderFontStyle = try typeContainer.decodeIfPresent(Styler.Font.self, forKey: .placeholderFontStyle) {
            self.placeholderFontStyle = placeholderFontStyle
        }
        
        if let placeholderTextColor = try typeContainer.decodeIfPresent(Color.self, forKey: .placeholderTextColor) {
            self.placeholderTextColor = placeholderTextColor
        }
        
        if let fontStyle = try typeContainer.decodeIfPresent(Styler.Font.self, forKey: .fontStyle) {
            self.fontStyle = fontStyle
        }
        
        if let editable = try typeContainer.decodeIfPresent(Bool.self, forKey: .editable) {
            self.editable = editable
        }
        
        accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
        height = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .height)
    }
    
    public override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
        try container.encodeIfPresent(height, forKey: .height)
        try container.encode(fontStyle, forKey: .fontStyle)
        try container.encode(editable, forKey: .editable)
        try container.encode(placeholderFontStyle, forKey: .placeholderFontStyle)
        try container.encode(placeholderTextColor, forKey: .placeholderTextColor)
    }
}
