#include "widget.h"
#include <QHBoxLayout>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QDateEdit>
#include <QFileDialog>
#include "snaverblogmanage.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(Update()));
    m_pcb = new QComboBox;
    m_pcb->setInsertPolicy(QComboBox::InsertAtCurrent);

    QPushButton *pbtRefresh = new QPushButton("Refresh",this);
    QPushButton *pbtStart = new QPushButton("Start",this);
    QPushButton *pbtStop = new QPushButton("Stop",this);
    {
        QObject::connect(pbtStart,SIGNAL(clicked()),this,SLOT(StartButton()));
        QObject::connect(pbtStop,SIGNAL(clicked()),this,SLOT(StopButton()));
        QObject::connect(pbtRefresh,SIGNAL(clicked()),this,SLOT(RefreshButton()));
    }

    QVBoxLayout *vlayout = new QVBoxLayout;
    {
        QHBoxLayout *hlayout = new QHBoxLayout;
        hlayout->addWidget(m_pcb,Qt::AlignLeft);
        hlayout->addWidget(pbtRefresh);
        vlayout->addLayout(hlayout);
    }

    m_pedTime = new QLineEdit(this);
    m_pedTime->setText(QString("500"));

    {
        QHBoxLayout *hlayout = new QHBoxLayout;
        hlayout->addWidget(m_pedTime);
        hlayout->addWidget(pbtStart);
        hlayout->addWidget(pbtStop);
        vlayout->addLayout(hlayout);
    }

    {
        QHBoxLayout *hlayout = new QHBoxLayout;
        for (int i = 0; i < C_CRAWLER_MAX; i++ )
            hlayout->addWidget(&m_aResultList[i]);
        vlayout->addLayout(hlayout);
    }

    setLayout(vlayout);
    m_pNaverBlog = new SNaverBlogManage(this);
    m_pManage[0] = m_pNaverBlog;

    m_db = QSqlDatabase::addDatabase("QMYSQL");
    m_db.setHostName("bigbird.iptime.org");
    m_db.setUserName("admin");
    m_db.setPassword("admin123");
    m_db.setDatabaseName("concepters");
    if (!m_db.open())
    {
        InsertLog(0,"MySql Error...");
        return;
    }

    for (int i = 0 ; i < C_CRAWLER_MAX ; i++)
        m_pManage[i]->SetParent(this);

    m_pNaverBlog->MakeTables();
    RefreshButton();
}

Widget::~Widget()
{
    m_pNaverBlog->DropTables();
    m_db.close();
    m_pNaverBlog->SetParent(0);
}

void Widget::InsertLog(int _nSelect,QString str)
{
    if (_nSelect >= C_CRAWLER_MAX) return;

    QTime time = QTime::currentTime();
    QString strOut = time.toString("[hh:mm:ss] ") + str;
    m_aResultList[_nSelect].addItem(strOut);
    QDate date = QDate::currentDate();

    QFile file(date.toString(Qt::ISODate)+"_"+QString::number(_nSelect)+".log");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
        return;

    QTextStream out(&file);
    out << strOut << "\n";
    file.close();

    if (m_aResultList[_nSelect].count() > 1024)
    {
        m_aResultList[_nSelect].removeItemWidget(m_aResultList[_nSelect].item(0));
        QListWidgetItem* item = m_aResultList[_nSelect].takeItem(0);
        delete item;
    }
    m_aResultList[_nSelect].setCurrentRow( m_aResultList[_nSelect].count() - 1 );
    m_aResultList[_nSelect].repaint();
}

void Widget::StartButton()
{
    m_timer.stop();
    QSqlQuery query;
    query.exec("UPDATE crawling set state = '" + QString::number(m_pNaverBlog->GetTableNumber()) + "' where id = '" + m_pcb->currentData().toString() + "'");
    query.exec("SELECT _keyword.start,_keyword.end, _keyword.searches,_keyword.id,_datagroup.id "
                    "FROM crawling _crawling INNER JOIN keyword _keyword ON _crawling.keyword_id = _keyword.id "
                    "inner join datagroup _datagroup on _crawling.datagroup_id  = _datagroup.id "
                    "where _crawling.id = '" + m_pcb->currentData().toString() + "'");
    query.next();
    QString str =  + " , ";
        str += query.value(1).toString() + " ~ ";
        str += query.value(2).toString() + " -> ";
        str += query.value(3).toString();
    m_timer.start(m_pedTime->text().trimmed().toInt());
    m_pManage[0]->Start(QDate::fromString(query.value(0).toString(),"yyyy-MM-dd"),
                        QDate::fromString(query.value(1).toString(),"yyyy-MM-dd"),
                        query.value(2).toString().trimmed(),// keyword
                        query.value(3).toString().trimmed(),// keyword_id
                        query.value(4).toString().trimmed(),
                        1,
                        m_pedTime->text().trimmed().toInt());
}

void Widget::StopButton()
{
    m_timer.stop();
}

void Widget::Update()
{
    if (m_db.isOpen() == false)
    {
        if (m_db.open())
        {
            InsertLog(0,"MySql Open Error...");
            m_timer.stop();
            return;
        }
    }

    int nCount = 0;
    for (int i = 0 ; i < C_CRAWLER_MAX ; i++)
        nCount += m_pManage[i]->Update();
    if (nCount == C_CRAWLER_MAX)
    {
        m_timer.stop();
        m_pNaverBlog->Join();
    }
}

void Widget::RefreshButton()
{
    m_pcb->clear();
    QSqlQuery query("SELECT _keyword.searches,_keyword.start,_keyword.end, _datagroup.name , _crawling.id "
                    "FROM crawling _crawling INNER JOIN keyword _keyword ON _crawling.keyword_id = _keyword.id "
                    "inner join datagroup _datagroup on _crawling.datagroup_id  = _datagroup.id where _crawling.state is null");
    while (query.next())
    {
        QString str;
        str += query.value(0).toString() + " , ";
        str += query.value(1).toString() + " ~ ";
        str += query.value(2).toString() + " -> ";
        str += query.value(3).toString();
        m_pcb->addItem(str,query.value(4));
    }
}
