#ifndef MUDLET_XMLIMPORT_H
#define MUDLET_XMLIMPORT_H

/***************************************************************************
 *   Copyright (C) 2008-2012 by Heiko Koehn - KoehnHeiko@googlemail.com    *
 *   Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com            *
 *   Copyright (C) 2016-2017, 2020, 2022 by Stephen Lyons                  *
 *                                               - slysven@virginmedia.com *
 *   Copyright (C) 2017 by Ian Adkins - ieadkins@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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "dlgTriggerEditor.h"

#include <QApplication>
#include <QFile>
#include <QMap>
#include <QMultiHash>
#include <QPointer>
#include <QXmlStreamReader>
#include <QClipboard>

class Host;
class TAction;
class TAlias;
class TKey;
class TScript;
class TTimer;
class TTrigger;
class TVar;
class TRoom;

class XMLimport : public QXmlStreamReader
{
    Q_DECLARE_TR_FUNCTIONS(XMLimport);

public:
    explicit XMLimport(Host*);
    virtual ~XMLimport() {}
    std::pair<bool, QString> importPackage(QFile*, QString packageName = QString(), int moduleFlag = 0, QString* pVersionString = nullptr);
    std::pair<EditorViewType, int> importFromClipboard();

private:
    const QString YES = qsl("yes");

    std::pair<EditorViewType, int> readPackage();

    void readHostPackage();
    int readTriggerPackage();
    int readTimerPackage();
    int readAliasPackage();
    int readActionPackage();
    int readScriptPackage();
    int readKeyPackage();
    void readVariablePackage();
    void readUnknownMapElement();
    void readMap();
    void readRoom(QMultiHash<int, int>&, unsigned int*);
    void readRooms(QMultiHash<int, int>&);
    void readRoomFeature(TRoom*);
    void readRoomFeatures(TRoom*);
    void readEnvColor();
    void readEnvColors();
    void readArea();
    void readAreas();
    void readHelpPackage();

    void readUnknownElement(const QString&);

    void readHost(Host*);
    bool readHostColorElement(Host*, QStringView elementName);
    bool readHostBorderElement(QMargins&, QStringView elementName);
    void readLegacyMapInfoContributors();
    void readMapInfoContributor();
    void readProfileShortcut();
    void readStopWatchMap();
    void readMMCPOptions();
    int readTrigger(TTrigger*);
    int readTimer(TTimer*);
    int readAlias(TAlias*);
    int readAction(TAction*);
    int readScript(TScript*);
    int readKey(TKey*);
    void readVariable(TVar*);
    void readHiddenVariables();

    void readStringList(QStringList&, const QString&);
    void readIntegerList(QList<int>&, const QString& parentName, const QString &whatIsParent);
    void readModulesDetailsMap(QMap<QString, QStringList>&);
    void getVersionString(QString&);
    QString readScriptElement();

    void remapColorsToAnsiNumber(QStringList&, const QList<int>&);

    bool readDefaultTrueBool(QString name);

    QPointer<Host> mpHost;
    QString mPackageName;
    TTrigger* mpTrigger = nullptr;
    TTimer* mpTimer = nullptr;
    TAlias* mpAlias = nullptr;
    TKey* mpKey = nullptr;
    TAction* mpAction = nullptr;
    TScript* mpScript = nullptr;
    TVar* mpVar = nullptr;
    bool gotTrigger = false;
    bool gotTimer = false;
    bool gotAlias = false;
    bool gotKey = false;
    bool gotAction = false;
    bool gotScript = false;
    int module = 0;
    int mMaxRoomId = 0;
    quint8 mVersionMajor = 1; // 0 to 255
    quint16 mVersionMinor = 0; // 0 to 999 for 3 digit decimal value. Cannot be a quint8 as that only allows x.255 for the decimal
};

#endif // MUDLET_XMLEXPORT_H
