//
//  ProgressBarModel.swift
//  MVMCoreUI
//
//  Created by Ryan on 11/20/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import Foundation

@objcMembers public class ProgressBarModel: MoleculeModelProtocol {
    public static var identifier: String = "progressBar"
    @Percent public var percent: CGFloat
    public var color: Color = Color(uiColor: .mfCerulean())
    public var backgroundColor: Color? = Color(uiColor: .mfLightSilver())
    public var roundedCorners: Bool?
    public var thickness: CGFloat?
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case roundedCorners
        case thickness
        case percent
        case color
        case backgroundColor
    }
    
    public init(_ percent: CGFloat) {
        self.percent = percent
    }
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        percent = try typeContainer.decode(CGFloat.self, forKey: .percent)
        if let color = try typeContainer.decodeIfPresent(Color.self, forKey: .color) {
            self.color = color
        }
        if let color = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) {
            backgroundColor = color
        }
        roundedCorners = try typeContainer.decodeIfPresent(Bool.self, forKey: .roundedCorners)
        thickness = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .thickness)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(percent, forKey: .percent)
        try container.encode(color, forKey: .color)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(roundedCorners, forKey: .roundedCorners)
        try container.encodeIfPresent(thickness, forKey: .thickness)
    }
}
