//
//  BGImageHeadlineBodyButtonModel.swift
//  MVMCoreUI
//
//  Created by Khan, Arshad on 02/04/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


public class BGImageHeadlineBodyButtonModel: ContainerModel, MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "bgImageHeadlineBodyButton"
    public var backgroundColor: Color?
    public var button: ButtonModel?
    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
    //--------------------------------------------------
    
    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 = BGImageHeadlineBodyButton.heightConstant
        }
        
        button?.size = .tiny
        button?.style = .secondary
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case headlineBody
        case image
        case button
    }
    
    //--------------------------------------------------
    // 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)
        button = try typeContainer.decodeIfPresent(ButtonModel.self, forKey: .button)
        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(button, forKey: .button)
    }
}
