#ifndef LOGICBURNDATA_H
#define LOGICBURNDATA_H

#include "src/view/burndata.h"
#include <QAbstractItemModel>
#include <QIcon>
#include <kysdk/applications/kmessagebox.h>
#include <QMutex>

class DataFile;
class LogicBurnData : public QAbstractItemModel
{
    Q_OBJECT

public:
    explicit LogicBurnData(QObject *parent = nullptr);

    // Header
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
    bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;

    // Basic
    QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex parent(const QModelIndex &index) const override;

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

    // Editable
    bool setData(const QModelIndex &index, const QVariant &value,int role = Qt::EditRole) override;
    Qt::ItemFlags flags(const QModelIndex& index) const override;
    Qt::DropActions supportedDropActions() const override;

    QStringList mimeTypes() const;
    QMimeData *mimeData(const QModelIndexList &indexes) const;
    bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);

public:
    bool isUp(QModelIndex);
    bool isFolder(QModelIndex);
    bool checkAdd(QModelIndex);
    bool checkAdd(QModelIndexList);
    bool checkDel(QModelIndexList);
    bool checkCls(QModelIndex);
    bool checkDefaultAddpage(QModelIndex);
    bool checkOpen(QModelIndexList);
    bool checkOpenLocal(QModelIndexList);
    bool checkProperties(QModelIndexList);
    bool checkSelectModelItem(QModelIndexList);
    bool checkSize();

    QString stringSize(unsigned long);
    int index() const { return m_iIndex; }
    unsigned int level(QModelIndex idx = QModelIndex());
    int filter(QModelIndex idx = QModelIndex());
    int doFilterHidden(QModelIndex idx = QModelIndex());
    int doRecoveryHidden(QList<void *>, QModelIndex);
    int doFilterBroken(QModelIndex idx = QModelIndex());
    int doRecoveryBroken(QList<void *>, QModelIndex);
    int doFilterRepleace(QModelIndex idx = QModelIndex());
    int doRecoveryRepleace(QList<void *>,QModelIndex);
    void fixFileDstPathByIndex(QString newPath, QModelIndex idx); /* 修改文件(夹)的目标路径(拷贝到刻录的指定路径) */

    int findTheRootIndexofFile(DataFile* inputFile);

    int recoverHiddenFilesByDeviceIndex(QList<void*> dataList,int deviceIndex);
    int recoverBrokenFilesByDeviceIndex(QList<void*> dataList,int deviceIndex);
    int recoverReplaceLinkFilesByDeviceIndex(QList<void*> dataList,int deviceIndex);
    int getDeviceCount();
    bool updateListOrder();  // 保持m_oBurners列表顺序与MainWindow的顺序一致

public slots:
    void changeIndex(int);


    int addDatasToIndex(QStringList, QModelIndex);
    int deleteDatasFromIndex(QModelIndexList, QModelIndex);


    int clearDatasUnderIndex(QModelIndex);
    int clearDatasUnderIndexd(QModelIndex);
    bool duplicatedName(QString, QModelIndex);
    void addEmptyFolder(QString, QModelIndex);
    void deleteRoot(int);
    void slotRestoreAllFiles(QList<void *> hiddens, QList<void *> brokens, QList<void *> replaces);
    void stopLoadingData();

private slots:
    void slotsSetting();
    void slotsCreateISO(void *inputCDrom);
    void slotsCreateISO();
    void slotsBurnData();
    void slotBurnISO9660Data();
    void slotsBurnUDFData();
    void slotsEmptyMedium(void *);
    void slotsUpdateCDROM();
    void slotsDoFilterHidden(void *);
    void slotsDoFilterBroken(void *);
    void slotsDoRepleace(void *);
    void slotsUpdateSize(unsigned long);

private:
    int addDiskData(QStringList, void *);

    int calcFileCount(QStringList intputList);
    int calcFileCountInDir(QString inputDirPath);

    int addDirectoy(QString, void *, bool disk = false);
    int addFile(QString, void *, bool isDisk = false);
    bool checkTempPathAvailabeSize();

private:
    int           m_iIndex;
    bool          m_bCancel;
    bool          m_bAppRunning;
    QStringList   m_oHHeaders;
    QStringList   m_oVHeaders;
    QList<void *> m_oRoots;
    QList<void *> m_oBurners;
    bool          m_isContinue;
    QList<QMutex*> m_mutex_list;

Q_SIGNALS:
    void setting(void *);
    void rootIndex(QModelIndex);
    void sizeChanged(unsigned long);
    void rootIndexChanged(QModelIndex);

    // progress
    void title(QString);
    void setRange(int, int);
    void setValue(int, QIcon icon = QIcon(), QString str = QString());
    void addValue(int, QIcon icon = QIcon(), QString str = QString());

    void defaultAddPageEnabled(bool);
    void addEnabled(bool);
    void delEnabled(bool);
    void clsEnabled(bool);
    void folderEnabled(bool);

    void updateDisk(int, void *);

    // filter
    void newFilterNode(QIcon, QString);

    void hiddenFileFiltered(int, void *);
    void brokenLinkfileFiltered(int, void *);
    void replaceLinkFileFiltered(int, void *);
};

#endif // LOGICBURNDATA_H
