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


public class EyebrowHeadlineBodyLinkModel: MoleculeModelProtocol, ParentMoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "eyebrowHeadlineBodyLink"
    public var moleculeName: String = EyebrowHeadlineBodyLinkModel.identifier
    public var backgroundColor: Color?
    public var eyebrow: LabelModel?
    public var headline: LabelModel?
    public var body: LabelModel?
    public var link: LinkModel?
    
    public var children: [MoleculeModelProtocol] {
        [eyebrow, headline, body, link].compactMap { (molecule: MoleculeModelProtocol?) in molecule }
    }
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(eyebrow: LabelModel? = nil, headline: LabelModel? = nil, body: LabelModel? = nil, link: LinkModel? = nil) throws {
        // TODO: This class initializers should ensure that atleast one item is set.
        /*guard eyebrow != nil || headline != nil || body != nil || link != nil else {
         throw
         }*/
        self.eyebrow = eyebrow
        self.headline = headline
        self.body = body
        self.link = link
        setDefaults()
    }
    
    //--------------------------------------------------
    // MARK: - Method
    //--------------------------------------------------
    
    /// Defaults to set
    public func setDefaults() {
        if let headline = headline {
            headline.hero = 0
        }
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case eyebrow
        case headline
        case body
        case link
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        eyebrow = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .eyebrow)
        headline = try typeContainer.decodeMoleculeIfPresent(codingKey: .headline)
        body = try typeContainer.decodeMoleculeIfPresent(codingKey: .body)
        link = try typeContainer.decodeIfPresent(LinkModel.self, forKey: .link)
        setDefaults()
        // TODO: This class initializers should ensure that atleast one item is set.
        /*guard eyebrow != nil || headline != nil || body != nil || link != nil else {
         throw
         }*/
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(eyebrow, forKey: .eyebrow)
        try container.encodeModelIfPresent(headline, forKey: .headline)
        try container.encodeIfPresent(body, forKey: .body)
        try container.encodeIfPresent(link, forKey: .link)
    }
}
