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

import UIKit

public class CornerLabelsModel: ParentMoleculeModelProtocol {
    public static var identifier: String = "cornerLabels"
    public var backgroundColor: Color?
    public var topLeftLabel: LabelModel?
    public var topRightLabel: LabelModel?
    public var bottomLeftLabel: LabelModel?
    public var bottomRightLabel: LabelModel?
    public var molecule: MoleculeModelProtocol?
    public var children: [MoleculeModelProtocol] {
        [molecule, topLeftLabel, topRightLabel, bottomLeftLabel, bottomRightLabel].compactMap { $0 }
    }
    
    init(with molecule: MoleculeModelProtocol?) {
        self.molecule = molecule
    }
    
    private enum CodingKeys: String, CodingKey {
        case backgroundColor
        case topLeftLabel
        case topRightLabel
        case bottomLeftLabel
        case bottomRightLabel
        case molecule
        case moleculeName
    }

    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        molecule = try typeContainer.decodeModelIfPresent(codingKey: .molecule)
        topLeftLabel = try typeContainer.decodeMoleculeIfPresent(codingKey: .topLeftLabel)
        topRightLabel = try typeContainer.decodeMoleculeIfPresent(codingKey: .topRightLabel)
        bottomLeftLabel = try typeContainer.decodeMoleculeIfPresent(codingKey: .bottomLeftLabel)
        bottomRightLabel = try typeContainer.decodeMoleculeIfPresent(codingKey: .bottomRightLabel)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeModelIfPresent(molecule, forKey: .molecule)
        try container.encodeModelIfPresent(topLeftLabel, forKey: .topLeftLabel)
        try container.encodeModelIfPresent(topRightLabel, forKey: .topRightLabel)
        try container.encodeModelIfPresent(bottomLeftLabel, forKey: .bottomLeftLabel)
        try container.encodeModelIfPresent(bottomRightLabel, forKey: .bottomRightLabel)
        try container.encode(moleculeName, forKey: .moleculeName)
    }
}
