//
//  ListRightVariablePaymentsModel.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 10/02/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


public class ListRightVariablePaymentsModel: ListItemModel, MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "listRVImg"
    public var image: ImageViewModel
    public var leftLabel: LabelModel
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(image: ImageViewModel, leftLabel: LabelModel) {
        self.leftLabel = leftLabel
        self.image = image
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    override public func setDefaults() {
        super.setDefaults()
        leftLabel.hero = 0
    }
    
    static func createPayPalImage() -> ImageViewModel {
        let image = ImageViewModel(image: "imageName_PayPal_logo")
        image.localBundle = MVMCoreUIUtility.bundleForMVMCoreUI()
        image.width = 70.0
        image.height = 18.0
        image.contentMode = .scaleAspectFit
        return image
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case leftLabel
        case image
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        leftLabel = try typeContainer.decode(LabelModel.self, forKey: .leftLabel)
        image = try typeContainer.decodeIfPresent(ImageViewModel.self, forKey: .image) ?? ListRightVariablePaymentsModel.createPayPalImage()
        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.encode(leftLabel, forKey: .leftLabel)
        try container.encode(image, forKey: .image)
    }
}
