//
//  DropDownListItemModel.swift
//  MVMCoreUI
//
//  Created by Suresh, Kamlesh on 12/12/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import Foundation


@objcMembers public class DropDownListItemModel: ListItemModel, MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "dropDownListItem"
    public var molecules: [[ListItemModelProtocol & MoleculeModelProtocol]]
    public var dropDown: ItemDropdownEntryFieldModel
    private var addedMolecules: [ListItemModelProtocol & MoleculeModelProtocol]?

    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    /// Defaults to set
    public override func setDefaults() {
        style = .sectionFooter
        super.setDefaults()
        hideArrow = true
        line = LineModel(type: .none)
    }
    
    //--------------------------------------------------
    // MARK: - Functions
    //--------------------------------------------------
    
    public class func verify(dropdown: ItemDropdownEntryFieldModel, molecules: [[ListItemModelProtocol & MoleculeModelProtocol]]) throws {
        guard dropdown.options.count == molecules.count else {
            throw MolecularError.countImbalance("dropdown.options.count is not equal to molecules.count")
        }
    }

    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(molecules: [[ListItemModelProtocol & MoleculeModelProtocol]], dropDown: ItemDropdownEntryFieldModel) throws {
        self.molecules = molecules
        self.dropDown = dropDown
        try Self.verify(dropdown: dropDown, molecules: molecules)
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------

    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case molecules
        case dropDown
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------

    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        molecules = try typeContainer.decodeModels2DIfPresent(codingKey: .molecules) ?? [[]]
        dropDown = try typeContainer.decode(ItemDropdownEntryFieldModel.self, forKey: .dropDown)
        try Self.verify(dropdown: dropDown, molecules: 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.encodeModels2D(molecules, forKey: .molecules) 
        try container.encode(dropDown, forKey: .dropDown)
    }
}

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

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

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