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

import Foundation

@objcMembers public class SingleProgressBarModel: Codable {
    @Percent var percent: CGFloat
    var color: Color
    
    init(_ percent: CGFloat, color: Color) {
        self.percent = percent
        self.color = color
    }
}

@objcMembers public class MultiProgressBarModel: MoleculeModelProtocol {
    public static var identifier: String = "multiProgressBar"
    public var progressList: [SingleProgressBarModel]
    public var backgroundColor: Color?
    public var thickness: CGFloat?
    public var roundedCorners: Bool?
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case progressList
        case thickness
        case roundedCorners
        case backgroundColor
    }
    
    public init(_ progressList: [SingleProgressBarModel]) {
        self.progressList = progressList
    }
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        progressList = try typeContainer.decode([SingleProgressBarModel].self, forKey: .progressList)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        thickness = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .thickness)
        roundedCorners = try typeContainer.decodeIfPresent(Bool.self, forKey: .roundedCorners)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(progressList, forKey: .progressList)
        try container.encodeIfPresent(thickness, forKey: .thickness)
        try container.encodeIfPresent(roundedCorners, forKey: .roundedCorners)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
    }
}
