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


public class HeadlineBodyCaretLinkImageModel: ContainerModel, MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "headlineBodyCaretLinkImage"
    public var backgroundColor: Color?
    public var caretLink: CaretLinkModel?
    public var headlineBody: HeadlineBodyModel
    public var image: ImageViewModel
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    init(headlineBody: HeadlineBodyModel, image: ImageViewModel) {
        self.headlineBody = headlineBody
        self.image = image
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Defaults
    //--------------------------------------------------
    
    /// Defaults to set
    public override func setDefaults() {
        
        if useHorizontalMargins == nil {
            useHorizontalMargins = true
        }
        
        if useVerticalMargins == nil {
            useVerticalMargins = true
        }
        
        if topPadding == nil {
            topPadding = PaddingDefault
        }
        
        if bottomPadding == nil {
            bottomPadding = PaddingDefault
        }
        
        if image.height == nil {
            image.height = HeadLineBodyCaretLinkImage.heightConstant
        }
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case headlineBody
        case image
        case caretLink
    }
    
    //--------------------------------------------------
    // 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)
        image = try typeContainer.decode(ImageViewModel.self, forKey: .image)
        caretLink = try typeContainer.decodeIfPresent(CaretLinkModel.self, forKey: .caretLink)
        try super.init(from: decoder)
    }
    
    public override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        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(image, forKey: .image)
        try container.encodeIfPresent(caretLink, forKey: .caretLink)
    }
}
