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


public class HeadlineBodyButtonModel: MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "headlineBodyButton"
    public var moleculeName: String = HeadlineBodyButtonModel.identifier
    public var backgroundColor: Color?
    
    public var headlineBody: HeadlineBodyModel
    public var button: ButtonModel
    public var buttonHeadlinePadding: CGFloat
    
    //--------------------------------------------------
    // MARK: - Method
    //--------------------------------------------------
    
    /// Defaults to set
    public func setDefaults() {
        button.size = .tiny
        button.style = .secondary
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case headlineBody
        case button
        case buttonHeadlinePadding
    }
    
    //--------------------------------------------------
    // 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)
        headlineBody = try typeContainer.decode(HeadlineBodyModel.self, forKey: .headlineBody)
        button = try typeContainer.decode(ButtonModel.self, forKey: .button)
        buttonHeadlinePadding = try typeContainer.decode(CGFloat.self, forKey: .buttonHeadlinePadding)
        setDefaults()
    }
    
    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.encode(headlineBody, forKey: .headlineBody)
        try container.encode(button, forKey: .button)
        try container.encode(buttonHeadlinePadding, forKey: .buttonHeadlinePadding)
    }
}
