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

import UIKit


public class TabsListItemModel: ListItemModel, MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "tabsListItem"
    var tabs: TabsModel
    var molecules: [[ListItemModelProtocol & MoleculeModelProtocol]]
    private var addedMolecules: [ListItemModelProtocol & MoleculeModelProtocol]?

    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case tabs
        case molecules
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    public override func setDefaults() {
        super.setDefaults()
        hideArrow = true
        action = nil
        style = nil
        topPadding = 8
        bottomPadding = 0
    }
    
    //--------------------------------------------------
    // MARK: - Init
    //--------------------------------------------------
    
    public init(with tabs: TabsModel, molecules: [[ListItemModelProtocol & MoleculeModelProtocol]]) {
        self.tabs = tabs
        self.molecules = molecules
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        tabs = try typeContainer.decode(TabsModel.self, forKey: .tabs)
        molecules = try typeContainer.decodeModels2D(codingKey: .molecules)
        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(tabs, forKey: .tabs)
        try container.encodeModels2D(molecules, forKey: .molecules)
    }
}

extension TabsListItemModel: PageBehaviorProtocolRequirer {
    public func getRequiredBehaviors() -> [PageBehaviorModelProtocol] {
        return [AddRemoveMoleculesBehaviorModel()]
    }
}

extension TabsListItemModel: AddMolecules {
    public func moleculesToAdd() -> AddMolecules.AddParameters? {
        guard addedMolecules == nil else { return nil }
        let addedMolecules = molecules[tabs.selectedIndex]
        self.addedMolecules = addedMolecules
        return (addedMolecules, .below)
    }
}

extension TabsListItemModel: RemoveMolecules {
    public func moleculesToRemove() -> [ListItemModelProtocol & MoleculeModelProtocol]? {
        guard let addedMolecules = addedMolecules else { return nil }
        self.addedMolecules = nil
        return addedMolecules
    }
}
