//
//  HeadersH2LinkModel.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 24/06/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

public class HeadersH2LinkModel: HeaderModel, MoleculeModelProtocol, ParentMoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "headerH2Link"
    public var titleLockup: TitleLockupModel
    public var link: LinkModel
    private var helper = DeprecatedHeadlineBodyHelper()
    
    public var children: [MoleculeModelProtocol] {
        [titleLockup, link]
    }
    
    public func replaceChildMolecule(with molecule: MoleculeModelProtocol) throws -> Bool  {
        return try replaceChildMolecule(at: &titleLockup, with: molecule)
        || replaceChildMolecule(at: &link, with: molecule)
    }
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(titleLockup: TitleLockupModel, link: LinkModel) {
        self.titleLockup = titleLockup
        self.link = link
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    public override func setDefaults() {
        if topPadding == nil {
            topPadding = Padding.Component.VerticalMarginSpacing
        }
        if bottomPadding == nil {
            bottomPadding = Padding.Component.VerticalMarginSpacing
        }
        if titleLockup.title.fontStyle == nil {
            titleLockup.title.fontStyle = Styler.Font.RegularTitleXLarge
        }
        if titleLockup.subTitle?.fontStyle == nil {
            titleLockup.subTitle?.fontStyle = Styler.Font.RegularTitleMedium
        }
        super.setDefaults()
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case titleLockup
        case link
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        titleLockup = try typeContainer.decodeMolecule(codingKey: .titleLockup)
        link = try typeContainer.decode(LinkModel.self, forKey: .link)
        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(link, forKey: .link)
        guard try !helper.deprecatedEncode(to: encoder) else { return }
        try container.encode(titleLockup, forKey: .titleLockup)
    }
}
