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


public class ListProgressBarThinModel: ListItemModel, MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier = "listPrgBarThin"
    public var progressBar: ProgressBarModel
    public var leftHeadline: LabelModel
    public var leftBody: LabelModel?
    public var rightBar: LineModel
    public var rightLabel: LabelModel
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(progressBar: ProgressBarModel, leftHeadline: LabelModel, leftBody: LabelModel? = nil, rightBar: LineModel, rightLabel: LabelModel) {
        self.progressBar = progressBar
        self.leftHeadline = leftHeadline
        self.leftBody = leftBody
        self.rightBar = rightBar
        self.rightLabel = rightLabel
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Method
    //--------------------------------------------------
    
    override public func setDefaults() {
        super.setDefaults()
        
        rightBar.type = .medium
        
        if rightBar.backgroundColor == nil {
            rightBar.backgroundColor = Color(uiColor: .gray)
        }
        
        if rightLabel.numberOfLines == nil {
            rightLabel.numberOfLines = 1
        }
        
        leftHeadline.hero = 0
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case progressBar
        case leftHeadline
        case leftBody
        case rightBar
        case rightLabel
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    public required init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        progressBar = try typeContainer.decode(ProgressBarModel.self, forKey:.progressBar)
        leftHeadline = try typeContainer.decode(LabelModel.self, forKey: .leftHeadline)
        leftBody = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .leftBody)
        rightBar = try typeContainer.decode(LineModel.self, forKey: .rightBar)
        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.encode(progressBar, forKey: .progressBar)
        try container.encode(leftHeadline, forKey: .leftHeadline)
        try container.encodeIfPresent(leftBody, forKey: .leftBody)
        try container.encode(rightBar, forKey: .rightBar)
        try container.encode(rightLabel, forKey: .rightLabel)
    }
}
