//
//  ListDeviceRightVariableCaretModel.swift
//  MobileFirstFramework
//
//  Created by Lekshmi S on 09/12/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//
import MVMCoreUI

public class ListDeviceRightVariableCaretModel: ListItemModel, MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "listDvcRvCaret"
    public var eyebrow: LabelModel?
    public var headline: LabelModel?
    public var body1: LabelModel?
    public var body2: LabelModel?
    public var body3: LabelModel?
    public var image: ImageViewModel
    public var rightLabel: LabelModel
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(eyebrow: LabelModel? = nil, headline: LabelModel? = nil, body1: LabelModel? = nil, body2: LabelModel? = nil, body3: LabelModel? = nil, image: ImageViewModel, rightLabel: LabelModel) {
        self.eyebrow = eyebrow
        self.headline = headline
        self.body1 = body1
        self.body2 = body2
        self.body3 = body3
        self.image = image
        self.rightLabel = rightLabel
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Method
    //--------------------------------------------------
    
    override public func setDefaults() {
        super.setDefaults()
        rightLabel.hero = 0
        if image.width == nil, image.height == nil {
            image.width = 36
            image.height = 72
        }
    }
    
    /// The left stack requires content.
    func validateContent() throws {
        if eyebrow == nil,
           headline == nil,
           body1 == nil,
           body2 == nil,
           body3 == nil {
            throw ModelRegistry.Error.decoderOther(message: "\(self) requires one left label")
        }
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case eyebrow
        case headline
        case body1
        case body2
        case body3
        case image
        case rightLabel
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        eyebrow = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .eyebrow)
        headline = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .headline)
        body1 = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .body1)
        body2 = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .body2)
        body3 = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .body3)
        image = try typeContainer.decode(ImageViewModel.self, forKey: .image)
        rightLabel = try typeContainer.decode(LabelModel.self, forKey: .rightLabel)
        try super.init(from: decoder)
        try validateContent()
    }
    
    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(eyebrow, forKey: .eyebrow)
        try container.encodeIfPresent(headline, forKey: .headline)
        try container.encodeIfPresent(body1, forKey: .body1)
        try container.encodeIfPresent(body2, forKey: .body2)
        try container.encodeIfPresent(body3, forKey: .body3)
        try container.encode(image, forKey: .image)
        try container.encode(rightLabel, forKey: .rightLabel)
    }
}
