/*  **************************************************************************

    source::worx xtree
    Copyright © 2024-2025 c.holzheuer
    christoph.holzheuer@gmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


#ifndef XQITEMFACTORY_H
#define XQITEMFACTORY_H

#include <xsingleton.h>
#include <xqitem.h>
#include <xqitemtype.h>
#include <functional>

class XQViewModel;

// erzeugt items aus XQNodes

class XQItemFactory : public xsingleton<XQItemFactory>
{

public:

  void initItemFactory(const QString& modelSheetFileName );

  XQNodePtr  findModelSheet( const QString& modelName ) const;

  // __fix auf nicht vorhandenen content felder prüfen! vereinheitlichen!
  XQItemList  makeRow( const XQNodePtr& sheetNode, const XQNodePtr& contentNode );
  XQItemList  makeChildRow( QStandardItem* parent, const XQNodePtr& sheetNode, const XQNodePtr& contentNode );
  XQItem*     makeSingleItem( const XQNodePtr& sheetNode, const QString& caption );

  void        setItemTypeDataFromString( XQItem& item, const QString& roleKey, const QString& source ) const;

  XQItemType* makeItemType(const XQNodePtr& sheetEntry );
  XQItemType* findItemTypeTemplate(const QString& key ) const;


protected:

  bool isValid();

  XQItem*    makeItem(const XQNodePtr& sheetNode, const XQNodePtr& contentNode );

  // shortcuts
  using ItemConfigFunc = std::function<void( XQItem* item, const QString& attrValue, XQNodePtr contentNode, XQNodePtr sheetNode )>;
  using ItemConfigMap  = QMap<QString,ItemConfigFunc>;

  XQItemTypeMap s_ItemTypeTemplates;

  // Beschreibung des XQModels
  XQNodePtr  _modelSheet{};
  // Beschreibung der ItemTypes
  XQNodePtr  _typesSheet{};

};

#endif // XQITEMFACTORY_H
