//
//  StarModel.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 15/09/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

open class StarModel: MoleculeModelProtocol {
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    public static var identifier: String = "star"
    public var backgroundColor: Color?
    @Percent public var percent: CGFloat = 0
    public var borderColor: Color?
    public var fillColor: Color?
    public var size: CGFloat = 30.0
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case percent
        case borderColor
        case fillColor
        case size
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        if let percent = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .percent) {
            self.percent = percent
        }
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        borderColor = try typeContainer.decodeIfPresent(Color.self, forKey: .borderColor)
        fillColor = try typeContainer.decodeIfPresent(Color.self, forKey: .fillColor)
        if let size = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .size) {
            self.size = size
        }
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(percent, forKey: .percent)
        try container.encodeIfPresent(borderColor, forKey: .borderColor)
        try container.encodeIfPresent(fillColor, forKey: .fillColor)
        try container.encode(size, forKey: .size)
    }
}
