//
//  BGImageMoleculeModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 4/28/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


open class BGImageMoleculeModel: MoleculeContainerModel {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    open override class var identifier: String { "bgImageContainer" }
    
    public var image: ImageViewModel
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    open override func setDefaults() {
        if useHorizontalMargins == nil {
            useHorizontalMargins = true
        }
        if useVerticalMargins == nil {
            useVerticalMargins = true
        }
        if topPadding == nil {
            topPadding = PaddingDefaultVerticalSpacing3
        }
        if bottomPadding == nil {
            bottomPadding = PaddingDefaultVerticalSpacing3
        }
        if image.contentMode == nil {
            image.contentMode = .scaleAspectFill
        }
        if image.imageFormat == nil {
            image.imageFormat = "jpg"
        }
    }

    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case image
    }
    
    //--------------------------------------------------
    // MARK: - Initialization
    //--------------------------------------------------

    public init(_ image: ImageViewModel, molecule: MoleculeModelProtocol) {
        self.image = image
        super.init(with: molecule)
    }

    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        image = try typeContainer.decode(ImageViewModel.self, forKey:.image)
        try super.init(from: decoder)
    }

    open override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(image, forKey: .image)
    }
}
