#include "sdatadlg.h"
#include "stable.h"
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QInputDialog>
#include <QTextCodec>
#include <QFile>

#define D_NOT_SELECT -1

SDataDlg::SDataDlg()
{
    setWidgets();
}

SDataDlg::~SDataDlg()
{

}

void SDataDlg::setWidgets()
{
    setWindowTitle("Data");
    m_ptab = new QTabWidget;
    QVBoxLayout *vlayout = new QVBoxLayout;
    vlayout->addWidget(m_ptab);
    setLayout(vlayout);

    m_ptab->setTabsClosable(true);
    connect(m_ptab,SIGNAL(tabCloseRequested(int)),this,SLOT(CloseTab(int)));
    connect(m_ptab,SIGNAL(tabBarDoubleClicked(int)),this,SLOT(DoubleClickTab(int)));
}

QTableWidget *SDataDlg::AddTable(QString _str)
{
    STable *pTable = new STable;
    m_ptab->addTab(pTable,_str);
    return (QTableWidget *)pTable;
}

void SDataDlg::CloseTab(int index)
{
    ((STable*)(m_ptab->widget(index)))->clear();
    m_ptab->removeTab(index);
}

void SDataDlg::DoubleClickTab(int index)
{
    bool ok;
    if (index < 0) return;
    QString text = QInputDialog::getText(this,"Tab name change","Name : ", QLineEdit::Normal,m_ptab->tabText(index), &ok);
    if (ok)
    {
        m_ptab->setTabText(index,text);
    }
}

void SDataDlg::DataReload(QString _strTableName,int _nSelect)
{
    QString strSelect;
    strSelect = "select ";
    QVector <QStringList> column = GetColumn().data();
    foreach(QStringList strlist,column)
        strSelect += "CONVERT(" + strlist.at(SColumn::E_DATABASE) + " USING utf8),";
    strSelect = strSelect.left(strSelect.size()-1);
    strSelect += " from ";
    strSelect += _strTableName;

    if (_nSelect == STable::E_ARTICLE_BODY)
        strSelect += " WHERE article_form = 'body'";
    if (_nSelect == STable::E_ARTICLE_REPLY)
        strSelect += " WHERE article_form = 'reply'";

    QSqlQuery query(strSelect);
    STable *pTable = (STable *)m_ptab->currentWidget();
    if (pTable == NULL)
        pTable = (STable*)AddTable("<first>");
    pTable->setArticleSelect(_nSelect);
    pTable->clear();
    pTable->SetHeaderList(&column,SColumn::E_NAME);
    pTable->setRowCount(query.size());
    GetMainWindow()->m_progress.setRange(0,query.size());
    int nCount = 0;
    while (query.next())
    {
        for (int i = 0; i < pTable->columnCount() ; i++)
        {
            QString str = query.value(i).toString().replace("\n","     ");
            if((i == GetColumn().getColumnIndex("platform_form")) && (str.trimmed() == "post" || str.trimmed() == "channel" || str.trimmed() == "story"))
                str = "sns";
            if((i == GetColumn().getColumnIndex("platform_form")) && (str.trimmed() == "group"))
                str = "community";
            pTable->setItem(nCount,i,new QTableWidgetItem(" "+str.replace("\t"," ")+" "));
        }
        nCount++;
        GetMainWindow()->m_progress.setValue(nCount);
        GetMainWindow()->m_progress.repaint();
    }    
}

void SDataDlg::InsertCopyRow(int _nRow,QTableWidget *_pCurrent,QTableWidget *_pNew)
{
    _pNew->setRowCount(_pNew->rowCount()+1);
    for (int nCount = 0;nCount < _pCurrent->columnCount() ;nCount++ )
        _pNew->setItem(_pNew->rowCount()-1,nCount,new QTableWidgetItem(*_pCurrent->item(_nRow,nCount)));
}

void SDataDlg::SearchDate(QDate _dtStart,QDate _dtEnd)
{    
    STable *pCurrent = (STable *)m_ptab->currentWidget();
    if (pCurrent == NULL) return;
    STable *pNew = new STable;
    pNew->setArticleSelect(pCurrent->getArticleSelect());
    QVector <QStringList> column = GetColumn().data();
    pNew->SetHeaderList(&column,SColumn::E_NAME);
    GetMainWindow()->m_progress.setRange(0,pCurrent->rowCount()-1);

    for (int nCount = 0 ; nCount < pCurrent->rowCount(); nCount++ )
    {
        bool bFlag = false;
        QString strTime = pCurrent->item(nCount,GetColumn().getDateColumn())->text().trimmed();
        if (strTime.size() >= 10)
        {
            QChar ch = strTime.at(4);
            QString strFormat = QString("yyyy")+ch+QString("MM")+ch+QString("dd");
            QDate date = QDate::fromString(strTime.left(10),strFormat);
            if (_dtStart <= date && _dtEnd >= date)
                bFlag = true;
        }
        if (bFlag)
            InsertCopyRow(nCount,pCurrent,pNew);
        GetMainWindow()->m_progress.setValue(nCount);
        GetMainWindow()->m_progress.repaint();
    }
    m_ptab->addTab(pNew,STable::GetArticleType(pCurrent->getArticleSelect()));
    m_ptab->setCurrentIndex(m_ptab->count()-1);
}

void SDataDlg::SearchCounter(int _nCatalogIndex,int _nCompIndex,int _nInsDelIndex,int _nCounter)
{
    STable *pCurrent = (STable *)m_ptab->currentWidget();    
    if (pCurrent == NULL) return;
    QMap<QString, int> mapData,mapDataResult;
    GetMainWindow()->m_progress.setRange(0,pCurrent->rowCount()-1);

    for (int nCount = 0 ; nCount < pCurrent->rowCount(); nCount++ )
    {
        QString str;
        if (_nCatalogIndex == GetColumn().getDateColumn())
            str = QDateTime::fromString(pCurrent->item(nCount,_nCatalogIndex)->text().trimmed(),"yyyy-MM-dd hh:mm:ss").date().toString("yyyy-MM-dd");
        else
            str = pCurrent->item(nCount,_nCatalogIndex)->text().trimmed();

        if (str.isEmpty() == false)
        {
            if(mapData.contains(str))
                mapData[str]++;
            else
                mapData.insert(str,1);
        }
        GetMainWindow()->m_progress.setValue(nCount);
        GetMainWindow()->m_progress.repaint();
    }

    for(QMap <QString, int>::iterator iterPos = mapData.begin(); iterPos != mapData.end(); iterPos++)
    {
        bool bFlag = false;
        switch(_nCompIndex)
        {
        case E_LENGTH_COMP_GREATER:
            if (iterPos.value() > _nCounter)
                bFlag = true;
            break;
        case E_LENGTH_COMP_LESS:
            if (iterPos.value() < _nCounter)
                bFlag = true;
            break;
        case E_LENGTH_COMP_EQUAL:
            if (iterPos.value() == _nCounter)
                bFlag = true;
            break;
        }        
        if (bFlag)
            mapDataResult.insert(iterPos.key(), iterPos.value());
    }

    {     
        STable *pNew = new STable;
        pNew->setArticleSelect(pCurrent->getArticleSelect());
        QVector <QStringList> column = GetColumn().data();
        pNew->SetHeaderList(&column,SColumn::E_NAME);
        GetMainWindow()->m_progress.setRange(0,pCurrent->rowCount()-1);

        for (int nCount = 0 ; nCount < pCurrent->rowCount(); nCount++ )
        {
            bool bFlag = false;
            QString strCurrent = pCurrent->item(nCount,_nCatalogIndex)->text().trimmed();
            if(mapDataResult.contains(strCurrent))
                bFlag = true;
            if (_nInsDelIndex == 1) bFlag = !bFlag;
            if (bFlag) InsertCopyRow(nCount,pCurrent,pNew);
            GetMainWindow()->m_progress.setValue(nCount);
            GetMainWindow()->m_progress.repaint();
        }
        m_ptab->addTab(pNew,STable::GetArticleType(pCurrent->getArticleSelect()));
        m_ptab->setCurrentIndex(m_ptab->count()-1);
    }    
}

void SDataDlg::SearchLength(int _nCatalogIndex,int _nCompIndex,int _nInsDelIndex,int _nLength)
{
    STable *pCurrent = (STable *)m_ptab->currentWidget();
    if (pCurrent == NULL) return;
    GetMainWindow()->m_progress.setRange(0,pCurrent->rowCount()-1);
    STable *pNew = new STable;
    pNew->setArticleSelect(pCurrent->getArticleSelect());
    QVector <QStringList> column = GetColumn().data();
    pNew->SetHeaderList(&column,SColumn::E_NAME);
    GetMainWindow()->m_progress.setRange(0,pCurrent->rowCount()-1);

    for (int nCount = 0 ; nCount < pCurrent->rowCount(); nCount++ )
    {
        bool bFlag = false;
        int nCurrentLength = pCurrent->item(nCount,_nCatalogIndex)->text().trimmed().length();
        switch(_nCompIndex)
        {
        case E_LENGTH_COMP_GREATER:
            if (nCurrentLength > _nLength)
                bFlag = true;
            break;
        case E_LENGTH_COMP_LESS:
            if (nCurrentLength < _nLength)
                bFlag = true;
            break;
        case E_LENGTH_COMP_EQUAL:
            if (nCurrentLength == _nLength)
                bFlag = true;
            break;
        }
        if (_nInsDelIndex == 1) bFlag = !bFlag;
        if (bFlag) InsertCopyRow(nCount,pCurrent,pNew);
        GetMainWindow()->m_progress.setValue(nCount);
        GetMainWindow()->m_progress.repaint();
    }
    m_ptab->addTab(pNew,STable::GetArticleType(pCurrent->getArticleSelect()));
    m_ptab->setCurrentIndex(m_ptab->count()-1);
}

void SDataDlg::SearchReplace(int _nCatalogIndex,int _nReplaceFindIndex,int _nReplaceIndex,QString _strFind,QString _strReplace)
{    
    STable *pCurrent = (STable *)m_ptab->currentWidget();
    if (pCurrent == NULL) return;
    STable *pNew = new STable;
    pNew->setArticleSelect(pCurrent->getArticleSelect());
    QVector <QStringList> column = GetColumn().data();
    pNew->SetHeaderList(&column,SColumn::E_NAME);
    GetMainWindow()->m_progress.setRange(0,pCurrent->rowCount()-1);
    //int nCatalog = m_pcbReplaceCatalog->currentIndex();
    QStringList strListKeyword;
    if (_nReplaceFindIndex == E_REPLACE_SPACE)
        strListKeyword = _strFind.split(" ");
    else
        strListKeyword.push_back(_strFind);

    pNew->setRowCount(pCurrent->rowCount());
    for (int nCount = 0 ; nCount < pCurrent->rowCount(); nCount++ )
    {
        for (int nColumnCount = 0;nColumnCount < pCurrent->columnCount() ;nColumnCount++ )
        {
            if (_nCatalogIndex == nColumnCount)
            {
                QString strOut = pCurrent->item(nCount,nColumnCount)->text();
                switch(_nReplaceIndex)
                {
                case 0:
                {
                    foreach(QString str,strListKeyword)
                        strOut = strOut.replace(str,_strReplace);
                    break;
                }
                case 1:
                {
                    QString strMiddle;
                    foreach(QString strLine,strOut.split("\n"))
                    {
                        foreach(QString strWord,strLine.split(" "))
                        {
                            bool bFlag = false;
                            foreach(QString str,strListKeyword)
                            {
                                if (strWord == str)
                                {
                                    strMiddle += _strReplace + " ";
                                    bFlag = true;
                                    break;
                                }
                            }
                            if (bFlag == false)
                                strMiddle += strWord + " ";
                        }
                        strMiddle += "\n";
                    }
                    strOut = strMiddle;
                    break;
                }
                case 2:
                    QString strMiddle;
                    foreach(QString strLine,strOut.split("\n"))
                    {
                        foreach(QString strWord,strLine.split(" "))
                        {
                            bool bFlag = false;
                            foreach(QString str,strListKeyword)
                            {
                                if (strWord.contains(str))
                                {
                                    strMiddle += _strReplace + " ";
                                    bFlag = true;
                                    break;
                                }
                            }
                            if (bFlag == false)
                                strMiddle += strWord + " ";
                        }
                        strMiddle += "\n";
                    }
                    strOut = strMiddle;
                    break;
                }
                pNew->setItem(nCount,nColumnCount,new QTableWidgetItem(strOut));
            }
            else
                pNew->setItem(nCount,nColumnCount,new QTableWidgetItem(*pCurrent->item(nCount,nColumnCount)));
        }
        GetMainWindow()->m_progress.setValue(nCount);
        GetMainWindow()->m_progress.repaint();
    }
    //m_ptwData->addTab(pNew,m_ptwData->tabText(m_ptwData->currentIndex())+" r");
    m_ptab->addTab(pNew,STable::GetArticleType(pCurrent->getArticleSelect()));
    m_ptab->setCurrentIndex(m_ptab->count()-1);
}

int SDataDlg::GetCurrentArticle()
{
    if (m_ptab->count() == 0) return E_ARTICLE_NONE;
    return ((STable *)m_ptab->currentWidget())->getArticleSelect();
}

void SDataDlg::SearchKeyword(QVector <SKeyword> _vecKeyword)
{    
    STable *pCurrent = (STable *)m_ptab->currentWidget();
    if (pCurrent == NULL) return;
    STable *pNew = new STable;
    pNew->setArticleSelect(pCurrent->getArticleSelect());
    QVector <QStringList> column = GetColumn().data();
    pNew->SetHeaderList(&column,SColumn::E_NAME);    

    GetMainWindow()->m_progress.setRange(0,pCurrent->rowCount()-1);

    for (int nCount = 0 ; nCount < pCurrent->rowCount(); nCount++ )
    {
        bool bFlag = false;        
        foreach(SKeyword stKeyword,_vecKeyword)
        {
            QString strData = pCurrent->item(nCount,stKeyword.m_nCatalog)->text();
            switch(stKeyword.m_nKeyword)
            {
                case E_SEARCH_KEYWORD_OR:
                {
                    foreach(QString strKey , stKeyword.m_strListKeyword)
                        if (strData.contains(strKey)){bFlag = true;break;}
                    break;
                }
                case E_SEARCH_KEYWORD_AND:
                {
                    int nKeyCount = 0;
                    foreach(QString strKey , stKeyword.m_strListKeyword)
                        if (strData.contains(strKey)) nKeyCount++;
                    if (nKeyCount == stKeyword.m_strListKeyword.size())
                        bFlag = true;
                    break;
                }
                case E_SEARCH_KEYWORD_CELL_DELETE_OR:
                {
                    bFlag = true;
                    foreach(QString strKey , stKeyword.m_strListKeyword)
                        if (strData.contains(strKey)){bFlag = false;break;}
                    break;
                }
                case E_SEARCH_KEYWORD_CELL_DELETE_AND:
                {
                    bFlag = true;
                    int nCount = 0;
                    foreach(QString strKey , stKeyword.m_strListKeyword)
                        if (strData.contains(strKey)){nCount++;}
                    if (nCount == stKeyword.m_strListKeyword.size())
                        bFlag = false;
                    break;
                }
            }
        }
        if (bFlag) InsertCopyRow(nCount,pCurrent,pNew);
        GetMainWindow()->m_progress.setValue(nCount);
        GetMainWindow()->m_progress.repaint();
    }
    m_ptab->addTab(pNew,STable::GetArticleType(pCurrent->getArticleSelect()));
    m_ptab->setCurrentIndex(m_ptab->count()-1);
}

void SDataDlg::Import(QVector <QByteArray> _vecImport )
{
    STable *pNew = new STable;
    QTextCodec *codec = QTextCodec::codecForName("eucKR");
    GetMainWindow()->m_progress.setRange(0,_vecImport.size());
    QVector <QStringList> column = GetColumn().data();
    int ncRow = 0;
    bool bFirst = true;
    pNew->setRowCount(_vecImport.size());
    foreach (QByteArray byte,_vecImport)
    {
        QString strLine = codec->toUnicode(byte);
        if (bFirst)
        {
            QStringList strings = strLine.split(",");
            if (strings.at(0) == QString("#Head#"))
            {
                pNew->setColumnCount(strings.size()-1);
                for (int i = 1; i < strings.size();i++)
                {
                    for(int j = 0; j < column.size(); j++)
                    {
                        if(column.at(j).at(SColumn::E_DATABASE).compare(strings.at(i)) == 0)
                            strings[i] = column.at(j).at(SColumn::E_NAME);
                    }
                    pNew->setHorizontalHeaderItem(i-1,new QTableWidgetItem(strings.at(i)));
                }
                bFirst = false;
                pNew->setRowCount(_vecImport.size()-1);
                continue;
            }
            else
                pNew->setColumnCount(strings.size());
        }
        bFirst = false;
        strLine = strLine.replace("\"","");
        QStringList strings = strLine.split(",");
        int ncCol=0;
        foreach(QString str,strings)
            pNew->setItem(ncRow,ncCol++,new QTableWidgetItem(QString(" " + str + " ")));
        GetMainWindow()->m_progress.setValue(ncRow++);
        GetMainWindow()->m_progress.repaint();
    }
    m_ptab->addTab(pNew,"import");
    m_ptab->setCurrentIndex(m_ptab->count()-1);
}

void SDataDlg::Export(QString _strFilename)
{
    QFile file(_strFilename);
    if(!file.open(QFile::WriteOnly | QFile::Text)) return;
    QTextStream out(&file);
    QTableWidget *pCurrent = (QTableWidget *)m_ptab->currentWidget();
    out << "#Head#,";
    QVector <QStringList> column = GetColumn().data();
    foreach(QStringList strList,column)
        out << strList.at(SColumn::E_DATABASE) << ",";
    out << endl;
    GetMainWindow()->m_progress.setRange(0,pCurrent->rowCount()-1);
    for (int nCount = 0 ; nCount < pCurrent->rowCount(); nCount++ )
    {
        for (int nColumnCount = 0;nColumnCount < pCurrent->columnCount() ;nColumnCount++ )
        {
            QString str = pCurrent->item(nCount,nColumnCount)->text();
            str = str.replace(",",".");
            str = str.replace("\n","");
            out << "\"" << str << "\"" << ",";
        }
        out << "\"\"" << endl;
        GetMainWindow()->m_progress.setValue(nCount);
        GetMainWindow()->m_progress.repaint();
    }
    file.close();
}
