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

import Foundation


open class LoadingSpinnerModel: MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------

    public var backgroundColor: Color?
    public static var identifier: String = "loadingSpinner"
    public var strokeColor = Color(uiColor: .mvmBlack)
    public var lineWidth: CGFloat = 4
    public var diameter: CGFloat = 40
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case strokeColor
        case lineWidth
        case diameter
    }

    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        
        if let diameter = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .diameter) {
            self.diameter = diameter
        }
        
        if let strokeColor = try typeContainer.decodeIfPresent(Color.self, forKey: .strokeColor) {
            self.strokeColor = strokeColor
        }
        
        if let lineWidth = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .lineWidth) {
            self.lineWidth = lineWidth
        }
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(diameter, forKey: .diameter)
        try container.encode(strokeColor, forKey: .strokeColor)
        try container.encode(lineWidth, forKey: .lineWidth)
    }
}
