//
//  CarouselItemModel.swift
//  MVMCoreUI
//
//  Created by Suresh, Kamlesh on 11/26/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//


@objcMembers open class CarouselItemModel: MoleculeCollectionItemModel, CarouselItemModelProtocol {
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    open override class var identifier: String { "carouselItem" }
    
    public var peakingUI: Bool?
    public var peakingArrowColor: Color?
    public var analyticsData: JSONValueDictionary?
    public var fieldValue: String?
    public var enabled: Bool = true
    public var readOnly: Bool = false
    public var fieldKey: String?
    public var groupName: String = FormValidator.defaultGroupName
    public var baseValue: AnyHashable?
    
    public func formFieldValue() -> AnyHashable? {
        guard enabled else { return nil }
        return fieldValue
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case peakingUI
        case peakingArrowColor
        case analyticsData
        case fieldValue
        case fieldKey
        case groupName
        case enabled
        case readOnly
    }

    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        peakingUI = try typeContainer.decodeIfPresent(Bool.self, forKey: .peakingUI)
        peakingArrowColor = try typeContainer.decodeIfPresent(Color.self, forKey: .peakingArrowColor)
        analyticsData = try typeContainer.decodeIfPresent(JSONValueDictionary.self, forKey: .analyticsData)
        fieldValue = try typeContainer.decodeIfPresent(String.self, forKey: .fieldValue)
        fieldKey = try typeContainer.decodeIfPresent(String.self, forKey: .fieldKey)
        if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) {
            self.groupName = groupName
        }
        baseValue = fieldValue
        if let enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) {
            self.enabled = enabled
        }
        if let readOnly = try typeContainer.decodeIfPresent(Bool.self, forKey: .readOnly) {
            self.readOnly = readOnly
        }
        try super.init(from: decoder)
    }

    open override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(peakingUI, forKey: .peakingUI)
        try container.encodeIfPresent(peakingArrowColor, forKey: .peakingArrowColor)
        try container.encodeIfPresent(analyticsData, forKey: .analyticsData)
        try container.encodeIfPresent(fieldValue, forKey: .fieldValue)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(readOnly, forKey: .readOnly)
    }
}
