#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QCalendarWidget>
#include <QDateEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QComboBox>
#include <QLineEdit>
#include <QGroupBox>
#include <QListWidget>
#include <QTabWidget>
#include <QRadioButton>
#include <QButtonGroup>
#include <QTableWidget>
#include <QProgressBar>
#include <QMenuBar>
#include <QSqlDatabase>
#include "stable.h"
struct SCount
{
    SCount()
    {
        m_str.clear();
        m_nCount = 0;
    }

    SCount(QString _str)
    {
        m_str = _str;
        m_nCount = 1;
    }
    QString m_str;
    int m_nCount;
};
class Widget : public QWidget
{
    enum E_COLUMN
    {
        E_COLUMN_DATABASE=0,
        E_COLUMN_NAME,
        E_COLUMN_DATE,
        E_COLUMN_COUNT,
    };

    enum E_LENGTH_COMP
    {
        E_LENGTH_COMP_GREATER = 0,
        E_LENGTH_COMP_LESS,
        E_LENGTH_COMP_EQUAL,
    };

    enum E_DATABASE_COMMAND
    {
        E_DATABASE_COMMAND_INSERT = 0,
        E_DATABASE_COMMAND_UPDATE,
    };

    Q_OBJECT
public:
    Widget(QWidget *parent = 0);
    ~Widget();    
    QTabWidget *GetTabWidget()
    {
        return m_ptwData;
    }
    int getDatePosition()
    {
        return m_nColumn;
    }
    int getTitlePosition()
    {
        return m_nTitle;
    }
    int getBodyPosition()
    {
        return m_nBody;
    }
    int getPlatformTitlePosition()
    {
        return m_nPlatformTitle;
    }

private:
    // Data
    QListWidget *m_plwData;
    // Date
    QCalendarWidget *m_pcw;
    QDateEdit *m_pdeStart;
    QDateEdit *m_pdeEnd;
    //QPushButton *m_ppbInsertCalc;
    //QComboBox *m_pcbDateCatalog;
    int m_nColumn;
    int m_nTitle;
    int m_nBody;
    int m_nPlatformTitle;
    // Keyword
    QComboBox *m_pcbCatalog;
    QComboBox *m_pcbKeyword;
    QComboBox *m_pcbMethod;
    QLineEdit *m_pleString;
    //QPushButton *m_ppbInsertSearch;
    // Length
    QComboBox *m_pcbLengthCatalog;
    QComboBox *m_pcbLengthComp;
    QComboBox *m_pcbLengthInsDel;
    QLineEdit *m_pleLength;
    // Counter
    QComboBox *m_pcbCounterCatalog;
    QComboBox *m_pcbCounterComp;
    QComboBox *m_pcbCounterInsDel;
    QComboBox *m_pcbCounterCntApl;
    QLineEdit *m_pleCounter;

    // Filter
    QListWidget *m_plwFilterGroup;
    QListWidget *m_plwFilter;
    QLineEdit *m_pleFilterGroup;
    QGroupBox *m_pgbFilter;
    // Replace
    QComboBox *m_pcbReplaceCatalog;
    QComboBox *m_pcbReplaceFind;
    QLineEdit *m_pleReplaceFind;
    QLineEdit *m_pleReplace;
    QComboBox *m_pcbReplace;

    // Count
    QComboBox *m_pcbCountCatalog;

    //
    QTabWidget *m_ptwData;
    //
    QProgressBar *m_pProgress;
    // Column
    QVector <QStringList> m_vecColumn;
    QVector <SCount> vecData;
    QMap <QString, QMap <QString, int> > m_mapData;

private:
    QMenuBar *setMenuWidget();
    QGroupBox *setDataWidgets();
    QGroupBox *setDateWidgets();
    QGroupBox *setCountWidgets();
    QGroupBox *setSearchWidgets();
    QGroupBox *setLengthWidgets();
    QGroupBox *setCounterWidgets();
    QGroupBox *setFilterWidgets();
    QGroupBox *setReplaceWidgets();

    QTableWidget *AddTable(QString _str);
    void SetTableHead();
    void InsertCopyRow(int _nRow,QTableWidget *_pCurrent,QTableWidget *_pNew);    
    void DataReload(QString _strTableName,int _nSelect);
    void DataBaseFilter(int _nType,QString _strJson,int _nGroup,int _nCommand,QString _strFilterId ="");
    void DataBaseTimeFilter(int _nTimeCategory ,QDate _dateStart ,QDate _dateEnd ,int _nGroup,int _nCommand,QString _strFilterId="");
    void DataBaseSearchFilter(int _nArticle,int _nCategory,int _nMethod,int _nKeyword, QString _str,int _nGroup,int _nCommand,QString _strFilterId ="");
    void DataBaseLengthFilter(int _nArticle,int _nCategory,int _nComp,int _nInsDel,QString _str,int _nGroup,int _nCommand,QString _strFilterId ="");
    void DataBaseReplaceFilter(int _nArticle,int _nCategory,int _nFind,QString _strFind,QString _strReplace,int _nGroup,int _nCommand,QString _strFilterId ="");
    void RefreshFilter(int _nGroup);
    void DataBaseCounterFilter(int _nArticle,int _nCategory,int _nComp, int _nInsDel,QString _str,int _nGroup,int _nCommand,QString _strFilterId ="");
    bool ReloadColumn();    
    QString GetArticleType(int _nSelect);
public slots:
    void CloseTab(int index);
    void DoubleClickTab(int index);    
    void CountSave();
    void SearchDate();
    void SearchKeyword();
    void SearchLengthInsert();
    void SearchReplaceInsert();

    void SearchDateUpdate();
    void SearchKeywordUpdate();
    void SearchLengthUpdate();
    void SearchReplaceUpdate();
    void SearchCounterUpdate();
    void SearchCounterInsert();

    static void Debug(QString _strMsg);
    void DataGroupRefresh();
    void DataGroupItemChanged ( QListWidgetItem * item );
    void FilterGroupInsert();
    void FilterGroupDelete();
    void FilterGroupModify();
    void FilterGroupRefresh();
    void FilterGroupCopyPaste();
    void currentGroupItemChanged(QListWidgetItem *_pCurrent, QListWidgetItem *_pPrev=0);
    void FilterDelete();
    void currentFilterItemChanged(QListWidgetItem *_pCurrent, QListWidgetItem *_pPrev=0);
    void FileNew();
    void FileImport();
    void FileExport();
    void FileExit();
};

#endif // WIDGET_H
