#ifndef MAP_TYPE_HPP_INCLUDED
#define MAP_TYPE_HPP_INCLUDED

#include <iostream>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <vector>
#include <NBO/NBO/ENBOType.h>
#include <NBO/NBOGlobal.h>
#include <NBO/Shared/Base/NBOBaseClass.h>


namespace Inscoper::NBO {

/*!
* \brief Represents the XML complex type Map.
*
* This is a Mapping of Key/Value pairs.
*/
class NBO_EXPORT Map_Type : public NBOBaseClass {

public:
	/*!
	* \brief Represents the XML element M.
	*
	* This is a Key/Value pair used to build up a Mapping. The Element and Attribute Name are kept to single letters to minimize the length at the expense of readability as they are likely to occur many times.
	*/
	class NBO_EXPORT InlineM : public NBOBaseClass {
	
	public:
		
		InlineM();
		~InlineM();
	
		/*!
		* \brief Cast a NBOBaseClass to a InlineM.
		*
		* \param base The base class to cast
		* \return The casted element, or nullptr if the cast fails
		*/
		static std::shared_ptr<InlineM> fromBase(NBOBaseClassPtr base);
	
		/*!
		* \brief Returns the class type name.
		*
		* \return The class type name
		*/
		std::string getTypeName() const override;
	
		/*!
		* \brief Returns the XML element name corresponding to this class.
		*
		* \return The XML element name
		*/
		std::string getXMLName() const override;
	
		/*!
		* \brief Returns the NBO Type corresponding to this class.
		*
		* \return The NBO Type value
		*/
		ENBOType getNBOType() const override;
	
		/*!
		* \brief Returns a list of all classes derived from this class.
		*
		* \return The list of derived classes
		*/
		std::vector<NBOBaseClassPtr> getChildren() const override;
	
		/*!
		* \brief Returns a list of all child elements contained in this class.
		*
		* \return The list of child classes
		*/
		std::vector<NBOBaseClassPtr> getSubElements() const override;
	
		/*!
		* \brief Populates this object from a JSON file.
		*
		* \param path The path to the JSON file
		* \param validate If true, validates constraints on value. Default is true.
		*/
		void fromJsonFile(const std::string& path, bool validate = true);
	
		/*!
		* \brief Populates this object from a JSON string.
		*
		* \param json_string The JSON content as a string
		* \param validate If true, validates constraints on value. Default is true.
		*/
		void fromJsonString(const std::string& json_string, bool validate = true);
	
		/*!
		* \brief Populates this object from a XML file.
		*
		* \param path The path to the XML file
		* \param validate If true, validates constraints on value. Default is true.
		*/
		void fromXmlFile(const std::string& path, bool validate = true);
	
		/*!
		* \brief Populates this object from a XML string.
		*
		* \param xml_string The XML content as a string
		* \param validate If true, validates constraints on value. Default is true.
		*/
		void fromXmlString(const std::string& xml_string, bool validate = true);
	
		/*!
		* \brief Serializes this object to an XML file.
		*
		* \param filename The path to the output XML file
		* \param validate If true, validates constraints on value. Default is true.
		*/
		void toXmlFile(const std::string& filename, bool validate = true) const;
	
		/*!
		* \brief Serializes this object to an XML string.
		*
		* \param validate If true, validates constraints on value. Default is true.
		* \return A string containing the XML representation of this object
		*/
		std::string toXmlString(bool validate = true) const;
	
		/*!
		* \brief Sets the value of the K attribute.
		*
		* This is the key of this Key/Value pair.
		*
		* \param value The value to set
		* \param validate If true, validates constraints on value. Default is true.
		*/
		void setK(const std::string& value, bool validate = true);
	
		/*!
		* \brief Returns the value of the K attribute.
		*
		* This is the key of this Key/Value pair.
		*
		* \return The value of the attribute
		*/
		std::string getK() const;
	
		/*!
		* \brief Resets the K attribute to an unset state.
		*
		* This is the key of this Key/Value pair.
		*/
		void resetK();
	
		/*!
		* \brief Checks whether the K attribute is present.
		*
		* This is the key of this Key/Value pair.
		*
		* \return true if the attribute has been set, false otherwise
		*/
		bool hasK() const;
	
		/*!
		* \brief Returns the allowed values for this attribut.
		*
		* This is the key of this Key/Value pair.
		*
		* \return The list of allowed values
		*/
		static const std::set<std::string>& getKAllowedValues();
	
		/*!
		* \brief Assigns a new value to this object (= operator operator).
		*
		* \param v The value to set
		*/
		InlineM& operator=(const std::string& v);
		/*!
		* \brief Returns the value of this object  (generic conversion operator overload).
		*
		* \return The value of the object
		*/
		operator std::string() const;
		/*!
		* \brief Assigns a new value to this object.
		*
		* \param v The value to set
		* \param validate If true, validates constraints on value. Default is true.
		*/
		void set(const std::string& v, bool validate = true);
		/*!
		* \brief Returns the value of this object.
		*
		* \return The value of the object
		*/
		std::string get() const;
	private:
		std::string value;
	
		friend std::ostream& operator<<(std::ostream& os, const InlineM& u);
	
	private:
		class Impl;
		std::unique_ptr<Impl> pImpl;
	
	    std::optional<std::string> K; // Optional attribute
	    static const std::set<std::string> KAllowedValues;
	};
	
	/*! Shared pointer for InlineM*/
	typedef std::shared_ptr<InlineM> InlineMPtr;
	
	
	Map_Type();
	~Map_Type();

	/*!
	* \brief Cast a NBOBaseClass to a Map_Type.
	*
	* \param base The base class to cast
	* \return The casted element, or nullptr if the cast fails
	*/
	static std::shared_ptr<Map_Type> fromBase(NBOBaseClassPtr base);

	/*!
	* \brief Returns the class type name.
	*
	* \return The class type name
	*/
	std::string getTypeName() const override;

	/*!
	* \brief Returns the XML element name corresponding to this class.
	*
	* \return The XML element name
	*/
	std::string getXMLName() const override;

	/*!
	* \brief Returns the NBO Type corresponding to this class.
	*
	* \return The NBO Type value
	*/
	ENBOType getNBOType() const override;

	/*!
	* \brief Returns a list of all classes derived from this class.
	*
	* \return The list of derived classes
	*/
	std::vector<NBOBaseClassPtr> getChildren() const override;

	/*!
	* \brief Returns a list of all child elements contained in this class.
	*
	* \return The list of child classes
	*/
	std::vector<NBOBaseClassPtr> getSubElements() const override;

	/*!
	* \brief Populates this object from a JSON file.
	*
	* \param path The path to the JSON file
	* \param validate If true, validates constraints on value. Default is true.
	*/
	void fromJsonFile(const std::string& path, bool validate = true);

	/*!
	* \brief Populates this object from a JSON string.
	*
	* \param json_string The JSON content as a string
	* \param validate If true, validates constraints on value. Default is true.
	*/
	void fromJsonString(const std::string& json_string, bool validate = true);

	/*!
	* \brief Populates this object from a XML file.
	*
	* \param path The path to the XML file
	* \param validate If true, validates constraints on value. Default is true.
	*/
	void fromXmlFile(const std::string& path, bool validate = true);

	/*!
	* \brief Populates this object from a XML string.
	*
	* \param xml_string The XML content as a string
	* \param validate If true, validates constraints on value. Default is true.
	*/
	void fromXmlString(const std::string& xml_string, bool validate = true);

	/*!
	* \brief Serializes this object to an XML file.
	*
	* \param filename The path to the output XML file
	* \param validate If true, validates constraints on value. Default is true.
	*/
	void toXmlFile(const std::string& filename, bool validate = true) const;

	/*!
	* \brief Serializes this object to an XML string.
	*
	* \param validate If true, validates constraints on value. Default is true.
	* \return A string containing the XML representation of this object
	*/
	std::string toXmlString(bool validate = true) const;

	/*!
	* \brief Sets the list of M elements.
	*
	* This is a Key/Value pair used to build up a Mapping. The Element and Attribute Name are kept to single letters to minimize the length at the expense of readability as they are likely to occur many times.
	*
	* \param value The new list of M elements
	* \param validate If true, validates constraints on value. Default is true.
	*/
	void setM_List(const std::vector<Inscoper::NBO::Map_Type::InlineMPtr>& value, bool validate = true);

	/*!
	* \brief Returns the list of M elements.
	*
	* This is a Key/Value pair used to build up a Mapping. The Element and Attribute Name are kept to single letters to minimize the length at the expense of readability as they are likely to occur many times.
	*
	* \return The list of M elements
	*/
	std::vector<Inscoper::NBO::Map_Type::InlineMPtr> getM_List() const;

	/*!
	* \brief Adds a new M element to this object.
	*
	* This is a Key/Value pair used to build up a Mapping. The Element and Attribute Name are kept to single letters to minimize the length at the expense of readability as they are likely to occur many times.
	*
	* \param value The element to add
	* \param validate If true, validates constraints on value. Default is true.
	*/
	void addToM_List(const Inscoper::NBO::Map_Type::InlineMPtr& value, bool validate = true);

	/*!
	* \brief Clear the list of M.
	*
	* This is a Key/Value pair used to build up a Mapping. The Element and Attribute Name are kept to single letters to minimize the length at the expense of readability as they are likely to occur many times.
	*/
	void clearM_List();


private:
	class Impl;
	std::unique_ptr<Impl> pImpl;

    std::vector<Inscoper::NBO::Map_Type::InlineMPtr> M_List;
};

/*! Shared pointer for Map_Type*/
typedef std::shared_ptr<Map_Type> Map_TypePtr;

} // namespace Inscoper::NBO

#endif // MAP_TYPE_HPP_INCLUDED