#ifndef SINITIALIZER
#define SINITIALIZER
#include "sdbmanager.h"
#include <QList>
#include <QMap>
#include <QVariant>
class QString;
class QStringList;
class QSqlDatabase;

const int REAL_BODY = 0x01;
const int REAL_REPLY = 0x02;
//const int DEBUG = 0;
//const int RELEASE = 1;
//const int MODE = RELEASE;

class SInitializer
{

private:
    bool initDays();
    bool initDBConnection();
    bool initFilterProcess();
    bool initCompanyNum();
    bool initColumn();
    bool initInfluencer();
    bool initSpammer();
    bool initPlatform();
    //bool initConsumerCategory();
    bool initCategory();


public:
    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
    };

    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_SERVICE
    {
        E_SERVICE_INFLUENCER = 1,
        E_SERVICE_CONSUMER,
        E_SERVICE_SPAMMER,
        E_SERVICE_POWERCAFE,
        E_SERVICE_MAX
    };

    struct stDataBaseInfo
    {
        QString m_strHostName;
        QString m_strUserName;
        QString m_strPassword;
        QString m_strDBName;
    };
    struct stFilterProcessGroup
    {
        int m_nFilterProcessGroupID;
        QString m_strFilterProcessGroupName;
    };
    struct stInfluencerParam
    {
        double m_dBody;
        double m_dReply;
        stInfluencerParam() : m_dBody(0.4), m_dReply(0.6) {}
        double operator[](const int nSelect) const
        {
            switch(nSelect)
            {
            case 0:
                return m_dBody;
            case 1:
                return m_dReply;
            default:
                return 0.0;
            }
        }
    };
    struct stColumn
    {
        QStringList m_slColumn;
        int m_anColumn[E_DATA_MAX];
        stColumn()
        {
            for (int i = 0; i < E_DATA_MAX; i++)
                m_anColumn[i] = -1;
        }
    };
    struct stFilterProcess
    {
        int m_nDatagroupID;
        int m_nFilterGroupID;
        int m_nCategoryNum;
        int m_nFilterProcessOrder;
        int m_nFilterProcessType;
        int m_nCompanyNum;
        int m_nServiceNum;
    };

    struct stFilter
    {
        int m_nType;
        int m_nFilterGroupID;
        QVariant m_vData;
        stFilter()
        {
            m_nType = -1;
            m_nFilterGroupID = -1;
        }
    };
    struct stPlatform
    {
        QMap<QString, int> m_mapPlatformName;
        QMap<QString, int> m_mapPlatformForm;
    };



private:
    SDBManager m_sDBManager;
    QString m_strStartDay;
    QString m_strEndDay;
    double m_adSpammerParam[E_SPAMER_MAX];
    stDataBaseInfo m_stWebDBInfo;
    int m_nCompanyNum;
    stFilterProcessGroup m_stFilterProcessGroup;
    stInfluencerParam m_stInfluencerParam;
    stColumn m_stColumn;
    QList<stFilterProcess> m_listFilterProcess;
    QMap<int, QList<stFilter> > m_mapFilterGroup; // key : filtergroupid
    int m_nDataGroupNumber;
    stPlatform m_stPlatform;
    QMap<int, int> m_mapConsumerCategory;
    int m_nConsumerAllCategory;
    int m_anAllCategory[E_SERVICE_MAX];

public:
    bool init(const QString& _strFilterProcessGroupID, const QString& _strFilterProcessGroupName);
    void insertLog(const QString& _strMsg) const;
    QSqlDatabase getDatabase(const QString& _dbName) const;
    QSqlDatabase getDatabase(const SDBManager::E_DATABASE& _eDatabase) const;
    int getDataGroupNumber() const;
    QStringList getColumnStringList() const;
    QString getStartDay() const;
    QString getEndDay() const;
    const int* getColumnIntArray() const;
    QList<stFilterProcess> getFilterProcess();
    QMap<int, QList<stFilter> > getFilterGroup();
    const double* getSpammerParam() const;
    QList<stFilter> getFilterGroup(int _nFilterGroupId);
    int getPlatformForm(const QString &_form);
    int getPlatformName(const QString &_name);
    QMap<int, int>* getConsumerCategory();
    int getConsumerAllCategory();
    void closeConnection(const SDBManager::E_DATABASE _eDatabase);
    void closeConnection(const QString& _str);
    bool openConnection(const SDBManager::E_DATABASE _eDatabase);
    bool openConnection(const QString& _str);
    double getInfluencerParam(int _BodyorReply);
    void updateWebDBInfoState(QSqlDatabase &_db, int _nCompany, QString _strStatus);
    void updateWebDBInfoComplete(QSqlDatabase _db, QString _mindate, QString _maxdate, QString _lastupdate, int _bodycount, int _replycount, int _nCompany);
    int getAllCategoryNum(int _nService);
    //bool disableIndex(QSqlDatabase& db, QString _tableName, QStringList _strIndexNames, int _nMode = MODE);
    //bool enableIndex(QSqlDatabase& db, QString _tableName, QStringList _strIndexNames, int _nMode = MODE);
    //bool disableSettings(QSqlDatabase& db, int _nMode = MODE);
    //bool enableSettings(QSqlDatabase& db, int _nMode = MODE);
};


#endif // SINITIALIZER

