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

public class ListStoreLocatorModel: ListItemModel, MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier = "listStoreLocator"
    public var heart: HeartModel?
    public var leftHeadline: LabelModel
    public var leftBody: LabelModel
    public var leftSubBody: LabelModel
    public var rightLabel: LabelModel
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(heart: HeartModel?, leftHeadline: LabelModel, leftBody: LabelModel, leftSubBody: LabelModel, rightLabel: LabelModel) {
        self.heart = heart
        self.leftHeadline = leftHeadline
        self.leftBody = leftBody
        self.leftSubBody = leftSubBody
        self.rightLabel = rightLabel
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Method
    //--------------------------------------------------
    
    override public func setDefaults() {
        super.setDefaults()
        leftHeadline.hero = 0
        if rightLabel.numberOfLines == nil {
            rightLabel.numberOfLines = 1
        }
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case heart
        case leftHeadline
        case leftBody
        case leftSubBody
        case rightLabel
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    public required init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        heart = try typeContainer.decodeIfPresent(HeartModel.self, forKey:.heart)
        leftHeadline = try typeContainer.decode(LabelModel.self, forKey: .leftHeadline)
        leftBody = try typeContainer.decode(LabelModel.self, forKey: .leftBody)
        leftSubBody = try typeContainer.decode(LabelModel.self, forKey: .leftSubBody)
        rightLabel = try typeContainer.decode(LabelModel.self, forKey: .rightLabel)
        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(heart, forKey: .heart)
        try container.encode(leftHeadline, forKey: .leftHeadline)
        try container.encode(leftBody, forKey: .leftBody)
        try container.encode(leftSubBody, forKey: .leftSubBody)
        try container.encode(rightLabel, forKey: .rightLabel)
    }
}
