//
//  TemplateModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 3/13/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation


@objcMembers open class TemplateModel: MVMControllerModelProtocol, TabPageModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    open class var identifier: String { "" }
    
    public var pageType: String
    public var template: String {
        // Although this is done in the extension, it is needed for the encoding.
        Self.identifier
    }

    public var backgroundColor: Color?
    public var screenHeading: String?
    public var navigationBar: (NavigationItemModelProtocol & MoleculeModelProtocol)?
    public var formRules: [FormGroupRule]?
    public var behaviors: [PageBehaviorModelProtocol]?
    public var rootMolecules: [MoleculeModelProtocol] { [] }
    
    public var tabBarHidden: Bool = false
    public var tabBarIndex: Int?

    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(pageType: String) {
        self.pageType = pageType
    }

    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case pageType
        case template
        case screenHeading
        case backgroundColor
        case formRules
        case behaviors
        case navigationBar
        case tabBarHidden
        case tabBarIndex
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------

    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        pageType = try typeContainer.decode(String.self, forKey: .pageType)
        screenHeading = try typeContainer.decodeIfPresent(String.self, forKey: .screenHeading)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        formRules = try typeContainer.decodeIfPresent([FormGroupRule].self, forKey: .formRules)
        behaviors = try typeContainer.decodeModelsIfPresent(codingKey: .behaviors)
        navigationBar = try typeContainer.decodeModelIfPresent(codingKey: .navigationBar)
        if let tabBarHidden = try typeContainer.decodeIfPresent(Bool.self, forKey: .tabBarHidden) {
            self.tabBarHidden = tabBarHidden
        }
        tabBarIndex = try typeContainer.decodeIfPresent(Int.self, forKey: .tabBarIndex)
    }

    open func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(pageType, forKey: .pageType)
        try container.encode(template, forKey: .template)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(screenHeading, forKey: .screenHeading)
        try container.encodeIfPresent(formRules, forKey: .formRules)
        try container.encodeModelIfPresent(navigationBar, forKey: .navigationBar)
        try container.encode(tabBarHidden, forKey: .tabBarHidden)
        try container.encodeIfPresent(tabBarIndex, forKey: .tabBarIndex)
    }
}
