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

import UIKit


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

    public var children: [MoleculeModelProtocol] {
        return molecules.flatMap { $0 }
    }
    
    public func replaceChildMolecule(with replacementMolecule: MoleculeModelProtocol) throws -> Bool {
        guard let replacementMolecule = replacementMolecule as? ListItemModelProtocol & MoleculeModelProtocol else { return false }
        for (tabIndex, _) in molecules.enumerated() {
            for (elementIndex, _) in molecules[tabIndex].enumerated() {
                if molecules[tabIndex][elementIndex].id == replacementMolecule.id {
                    molecules[tabIndex][elementIndex] = replacementMolecule
                    return true
                }
            }
        }
        return false
    }
    
    //--------------------------------------------------
    // 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
        if line == nil {
            line = LineModel(type: .none)
        }
    }
    
    //--------------------------------------------------
    // 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 index = tabs.selectedIndex
        guard molecules.count >= index else { return nil }
        let addedMolecules = molecules[index]
        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
    }
}
