//
//  HeadersH1NoButtonsBodyTextModel.swift
//  MVMCoreUI
//
//  Created by Subhankar Acharya on 13/07/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

public class HeadersH1NoButtonsBodyTextModel: HeaderModel, MoleculeModelProtocol, ParentMoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "headerH1"
    public var titleLockup: TitleLockupModel
    private var helper = DeprecatedHeadlineBodyHelper()
    
    public var children: [MoleculeModelProtocol] {
        [titleLockup]
    }
    
    public func replaceChildMolecule(with molecule: MoleculeModelProtocol) throws -> Bool  {
        return try replaceChildMolecule(at: &titleLockup, with: molecule)
    }
    
    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: - Initializer
    //--------------------------------------------------
    
    public init(titleLockup: TitleLockupModel) {
        self.titleLockup = titleLockup
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case titleLockup
    }
    
    //--------------------------------------------------
    // 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)
        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)
        guard try !helper.deprecatedEncode(to: encoder) else { return }
        try container.encode(titleLockup, forKey: .titleLockup)
    }
}

public struct DeprecatedHeadlineBodyHelper {
    var headlineBody: HeadlineBodyModel?
    
    private enum DeprecatedCodingKeys: String, CodingKey {
        case headlineBody
    }
    
    public mutating func deprecatedCreate(from decoder: Decoder) throws -> TitleLockupModel? {
        let typeContainer = try decoder.container(keyedBy: DeprecatedCodingKeys.self)
        headlineBody = try typeContainer.decodeIfPresent(HeadlineBodyModel.self, forKey: .headlineBody)
        guard let headlineBody = headlineBody else { return nil }
        return try createTitleLockupModel(headlineBody: headlineBody)
    }
    
    public func deprecatedEncode(to encoder: Encoder) throws -> Bool {
        guard let headlineBody = headlineBody else { return false }
        var container = encoder.container(keyedBy: DeprecatedCodingKeys.self)
        try container.encode(headlineBody, forKey: DeprecatedCodingKeys.headlineBody)
        return true
    }
    
    public func createTitleLockupModel(defaultStyle: HeadlineBodyModel.Style = .header, headlineBody: HeadlineBodyModel) throws -> TitleLockupModel {
        guard let headline = headlineBody.headline else { throw ModelRegistry.Error.decoderOther(message: "headline is required for this use case.") }
        var body = headlineBody.body
        switch headlineBody.style ?? defaultStyle {
        case .landingHeader:
            headline.fontStyle = Styler.Font.RegularTitle2XLarge
            body?.fontStyle = Styler.Font.RegularTitleMedium
        case .itemHeader:
            headline.fontStyle = Styler.Font.BoldTitleLarge
            body?.fontStyle = Styler.Font.RegularBodyLarge
        default:
            headline.fontStyle = Styler.Font.RegularTitleXLarge
            body?.fontStyle = Styler.Font.RegularTitleMedium
        }
        let model = try TitleLockupModel(title: headline, subTitle: body)
        model.id = headlineBody.id
        return model
    }
    
    public func createHeadlineBodyModel(titleLockup: TitleLockupModel) -> HeadlineBodyModel {
        var headlineBody = HeadlineBodyModel(headline: titleLockup.title)
        headlineBody.body = titleLockup.subTitle
        headlineBody.id = titleLockup.id
        return headlineBody
    }
}
