//
//  StringAndMoleculeModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 1/17/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


public class StringAndMoleculeModel: MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "stringAndMoleculeModel"
    public var backgroundColor: Color?
    public var string: String
    public var molecule: MoleculeModelProtocol
    public var stringColor: Color

    //--------------------------------------------------
    // MARK: - Init
    //--------------------------------------------------
    
    public init(string: String, molecule: MoleculeModelProtocol, stringColor: Color = Color(uiColor: .mvmBlack)) {
        self.string = string
        self.molecule = molecule
        self.stringColor = stringColor
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case string
        case molecule
        case stringColor
    }

    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    public required init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        string = try typeContainer.decode(String.self, forKey: .string)
        molecule = try typeContainer.decodeModel(codingKey: .molecule)
        stringColor = try typeContainer.decodeIfPresent(Color.self, forKey: .stringColor) ?? Color(uiColor: .mvmBlack)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encode(string, forKey: .string)
        try container.encodeModel(molecule, forKey: .molecule)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(stringColor, forKey: .stringColor)
    }
}
