//
//  HeadersH1ButtonModel.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 04/03/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


public class HeadersH1ButtonModel: HeaderModel, MoleculeModelProtocol, ParentMoleculeModelProtocol {
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    public static var identifier: String = "headerH1Btn"
    public var titleLockup: TitleLockupModel
    public var buttons: TwoButtonViewModel
    private var helper = DeprecatedHeadlineBodyHelper()

    public var children: [MoleculeModelProtocol] {
        [titleLockup, buttons]
    }
    
    public func replaceChildMolecule(with molecule: MoleculeModelProtocol) throws -> Bool {
        return try replaceChildMolecule(at: &titleLockup, with: molecule)
        || replaceChildMolecule(at: &buttons, with: molecule)
    }
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(titleLockup: TitleLockupModel, buttons: TwoButtonViewModel) {
        self.titleLockup = titleLockup
        self.buttons = buttons
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Subclass
    //--------------------------------------------------

    public override func setDefaults() {
        super.setDefaults()
        if titleLockup.title.fontStyle == nil {
            titleLockup.title.fontStyle = Styler.Font.RegularTitle2XLarge
        }
        if titleLockup.subTitle?.fontStyle == nil {
            titleLockup.subTitle?.fontStyle = Styler.Font.RegularTitleMedium
        }
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case titleLockup
        case buttons
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        titleLockup = try helper.deprecatedCreate(from: decoder) ?? typeContainer.decodeMolecule(codingKey: .titleLockup)
        buttons = try typeContainer.decode(TwoButtonViewModel.self, forKey: .buttons)
        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(buttons, forKey: .buttons)
        guard try !helper.deprecatedEncode(to: encoder) else { return }
        try container.encode(titleLockup, forKey: .titleLockup)
    }
}
