#ifndef SFILTERPROCESS_H
#define SFILTERPROCESS_H

#include <QObject>
#include <QObject>
#include <QThread>
#include <QSqlDatabase>
#include <QMap>
#include <QListWidget>
#include <QSqlDatabase>
#include <QVector>
#include <QDateTime>
#include <iostream>

#include "./../common.h"

using namespace std;

struct SReply
{
    QStringList m_strlist;
    QDate m_date;
    bool m_bSelect;
    int m_nOrder;
    QVector <int> m_vecCategory;
};

struct SBody
{
    int m_nNum;
    QStringList m_strlist;
    bool m_bSelect;
    bool m_bReplySelect;
    QDateTime m_dtLast;
    QDate m_date;
    QMap <int,SReply> m_vecReply;
    QVector <int> m_vecCategory;
};

struct SMap
{
    int m_nBody;
    int m_nCategory;
    int m_nRealIn;
    QString m_strURL;
};

struct SInfluenceCount
{
    int m_ncBody;
    int m_ncReply;
    double m_dinfulrence;
    int m_nID;
    int m_nNickname;
    int m_nCommunity;
    QString m_strID;
    QString m_strOrder;
    QStringList m_strlistData;
    SInfluenceCount()
    {
        m_nNickname = 0;
        m_nCommunity = 0;
        m_nID = 0;
    }
};

struct SPowerCafeCount
{
    QString m_strCafeID;
    QString m_strCommunityURL;
    QStringList m_strlistData;
    int m_nCommunityID;
    int m_ncBodyAll;
    int m_ncReplyAll;
    int m_ncAll;
    int m_ncBodyReal;
    int m_ncReplyReal;
    int m_ncAllReal;
    int m_ncUserReal;
    int m_ncUserAll;
    QMap <QString,bool> m_mapUser;
    SPowerCafeCount()
    {
        m_ncBodyAll = 0;
        m_ncReplyAll = 0;
        m_ncAll = 0;
        m_ncBodyReal = 0;
        m_ncReplyReal = 0;
        m_ncAllReal = 0;
        m_ncUserReal = 0;
        m_ncUserAll = 0;
    }
};

struct SBuzzyArticle
{
    QMap <QDate,int> m_mapCount;
};

#define D_ARTICLE_MAX 3

struct SBuzzyCount
{
    int m_nCategory;
    int m_anCount[D_ARTICLE_MAX];
};

struct SId
{
    int     m_nNum;
    int     m_nPlatfromName;
    int     m_nCommunity;
    QString m_strID;
    SId()
    {
        m_nNum = 0;
        m_nCommunity = 0;
        m_nPlatfromName = 1;
        m_strID = "(none)";
    }
};

struct SCommunity
{
    int     m_nNum;
    int     m_nPlatformname;
    int     m_nPlatformform;
    QString m_strTitle;
    QString m_strUrl;
    QString m_strID;
    QString m_strExid;

    SCommunity()
    {
        m_nNum = 0;
        m_nPlatformname = 1;
        m_nPlatformform = 1;
        m_strTitle = "(none)";
        m_strUrl = " ";
        m_strID = " ";
        m_strExid = " ";
    }
};

struct SNickname
{
    int     m_nNum;
    int     m_nID;
    QString m_strNickname;
    SNickname()
    {
        m_nID = 0;
    }
};

struct SNaverBlogProfile
{
    QDateTime m_date;
    QString m_strURL;
};

class SFilterProcess : public QObject
{
    Q_OBJECT
public:
    enum E_COLUMN
    {
        E_COLUMN_DATABASE=0,
        E_COLUMN_NAME,
        E_COLUMN_DATE,
        E_COLUMN_COUNT,
    };

    enum E_ARTICLE
    {
        E_ARTICLE_BODY = 0,
        E_ARTICLE_REPLY,
        E_ARTICLE_MAX,
    };

    enum E_DATA
    {
        E_DATA_platform_title = 0,
        E_DATA_article_form,
        E_DATA_article_parent,
        E_DATA_article_id,
        E_DATA_article_nickname,
        E_DATA_article_title,
        E_DATA_article_data,
        E_DATA_article_url,
        E_DATA_article_hit,
        E_DATA_article_date,
        E_DATA_article_order,
        E_DATA_platform_id,
        E_DATA_platform_name,
        E_DATA_platform_form,
        E_DATA_reply_url,
        E_DATA_keyword_id,
        E_DATA_article_profileurl,
        E_DATA_article_profile,
        E_DATA_etc,
        E_DATA_MAX,
    };

    enum E_SPAMER
    {
        E_SPAMER_BODY_COUNT_CUT = 0,
        E_SPAMER_BODY_COUNT_RATIO,
        E_SPAMER_REPLY_COUNT_CUT,
        E_SPAMER_REPLY_COUNT_RATIO,
        E_SPAMER_NICK_COUNT_CUT,
        E_SPAMER_NICK_COUNT_RATIO,
        E_SPAMER_MAX
    };

    struct SBuzzy
    {
        SBuzzyArticle m_astArticle[E_ARTICLE_MAX];
        int m_nArticle;
        int m_nCategory;
    };
    SFilterProcess();
    ~SFilterProcess();
    void run(QString _strFindID,QString _strFilterGroupName);
    bool ReloadSetup();
private:
    QSqlDatabase m_db;
    int m_anData[E_DATA_MAX];
    int m_nDateColumn;
    int m_nUrlColumn;
    int m_nOrderColumn;
    QVector <QStringList> m_data[E_ARTICLE_MAX],m_dataAll[E_ARTICLE_MAX];
    bool m_bDataFlag[E_ARTICLE_MAX],m_bDataFlagAll[E_ARTICLE_MAX];
    QSet <QString> m_setDataAll[E_ARTICLE_MAX];
    QStringList m_listDataBaseColumn;
    QMap <QString,SBody> m_mapData;
    QVector <SMap> m_vecMap;
    QStringList m_strlistOrder;
    QVector <SInfluenceCount> m_vecInfluence;
    QVector <SBuzzy> m_vecBuzzy;
    QMap <QString,SId> m_mapID;
    QMap <QString,SCommunity> m_mapCommunity;
    QMap <QString,SNickname> m_mapNickname;
    QMap <QString,int> m_mapPlatformform;
    QMap <QString,int> m_mapPlatformname;
    QMap <QString,SNaverBlogProfile> m_mapNaverBlogProfile;
    QString m_strFilterGroupName;

    const int C_MAP_REAL_BODY;
    const int C_MAP_REAL_REPLY;

    QString m_strWebDataBaseHost;
    QString m_strWebDataBaseUser;
    QString m_strWebDataBasePass;
    QString m_strWebDataBaseName;

    double m_adSpammer[E_SPAMER_MAX];

    int m_nCutDays;
    QString m_strStartDay;
    QString m_strEndDay;

    QVector <SKeyword> m_vecKeyword;
    QMap <QString,int> m_mapReply;

private:
    bool LockTable(QString _strTable);
    void UnLockTables();

    void DataFilter(QString _strFilterGroupID);
    void DataCategoryMake(QString _strFilter,int _nCategory);
    void DataPut(QString _strCompanyNum);
    void DataLoad(QString _strDataNum);
    void DataInfluenceBuzzCount(QString _strCompanyNum);
    void DataInfluenceCount(QString _strFilter,QString _strCompanyNum);
    void DataSpammersCount(QString _strFilter,QString _strCompanyNum);
    void DataPowerCafeCount(QString _strFilter,QString _strCompanyNum);
    void DataConsumerSupervisionCount(QString _strCompanyNum);    
    QString GetVectorToString(QVector <int> _vecCategory,QString _strDelimiter);
    void DataMake(int _nCategory,QVector <QStringList> *_data, bool *_bDataFlag);

    void MakeBuzzy(int _nCategory,QVector <QStringList> *_data, bool *_bDataFlag);
    void MakeBuzzyAll();

    void DataAdd();
    void DataCategoryAllMake(QString _strCompany);

    QString SqlString(QString _str);
    bool RenameTable(QString _strOld, QString _strNew , QString _strTmp);
    void FilterDate(QString strJson);
    void FilterSerach(QString strJson);
    void FilterLength(QString strJson);
    void FilterReplace(QString strJson);
    void FilterCounter(QString _strJson);

    bool ReloadColumn();    
    void ReloadData();
    QString SeqDelString(QString _strData,QChar _chChar,int _nMax);
    QString MakeInfluencerrankQuery(const SInfluenceCount &_stIn,int _nRank,QString _strSubject,QString _strCompanyNum);
    QVariant GetWebData(QString _strTable,QString _strColumn,int _nNum);
    QVariant GetWebData(QString _strTable,QString _strColumn,QString _strWhere);
    inline QString GetCommunityKey(const QStringList _strList);
    void InsertLog(QString _strMsg);
    void UpdateDBInfoState(QSqlDatabase &_db,QString _strCompanyNum,QString _strStatus);
public slots:
signals:
    void finished();
};

#endif // SFILTERPROCESS_H
