#include "tablealgorithm_interface.h"
#include <QFile>
#include <QDebug>

TableAlgorithmInterface::TableAlgorithmInterface()
{
    m_pCurrentTable = NULL;
}

TableAlgorithmInterface::~TableAlgorithmInterface()
{
    m_pCurrentTable = NULL;
}


void TableAlgorithmInterface::setSource(QWidget *qwidget)
{
    m_pCurrentTable = (STable *)qwidget;
    reset();
}

bool TableAlgorithmInterface::ReloadColumn()
{
    m_vecColumn.clear();
    qDebug() << "file open ready";

    QFile file("column.txt");
    if (file.open(QIODevice::ReadOnly | QIODevice::Text) == false) return false;
    {
        qDebug() << "file open starting";
        while (!file.atEnd())
        {
            QString str = QString(file.readLine());
            if (str.at(0) == QChar('#')) continue;
            if (str.trimmed().isEmpty()) continue;
            m_vecColumn.push_back(str.split(","));
        }
    }
    if (m_vecColumn.size() <= 0) return false;
    setColumnIndex();
    return true;
}

void TableAlgorithmInterface::setColumnIndex()
{
    for(int i = 0; i < E_DATA_MAX; i++)
        m_anData[i] = -1;

    for(int i = 0; i < m_vecColumn.size(); i++)
    {
        if (m_vecColumn.at(i).at(0).trimmed() == QString("platform_title")) m_anData[E_DATA_platform_title] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("article_form"))  m_anData[E_DATA_article_form] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("article_parent"))  m_anData[E_DATA_article_parent] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("article_id"))  m_anData[E_DATA_article_id] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("article_nickname"))  m_anData[E_DATA_article_nickname] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("article_title"))  m_anData[E_DATA_article_title] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("article_data"))  m_anData[E_DATA_article_data] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("article_url"))  m_anData[E_DATA_article_url] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("article_hit"))  m_anData[E_DATA_article_hit] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("article_date"))  m_anData[E_DATA_article_date] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("article_order"))  m_anData[E_DATA_article_order] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("platform_id"))  m_anData[E_DATA_platform_id] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("platform_name"))  m_anData[E_DATA_platform_name] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("platform_form"))  m_anData[E_DATA_platform_form] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("reply_url"))  m_anData[E_DATA_reply_url] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("keyword_id"))  m_anData[E_DATA_keyword_id] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("article_profileurl")) m_anData[E_DATA_article_profileurl] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("article_profile")) m_anData[E_DATA_article_profile] = i;
        if (m_vecColumn.at(i).at(0).trimmed() == QString("etc")) m_anData[E_DATA_etc] = i;
    }
}


QString TableAlgorithmInterface::getArticleID()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_article_id])->text();
}
QString TableAlgorithmInterface::getKeywordID()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_keyword_id])->text();
}
QString TableAlgorithmInterface::getPlatformName()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_platform_name])->text();
}
QString TableAlgorithmInterface::getPlatformForm()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_platform_form])->text();
}
QString TableAlgorithmInterface::getArticleForm()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_article_form])->text();
}
QString TableAlgorithmInterface::getArticleNickname()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_article_nickname])->text();
}
QString TableAlgorithmInterface::getArticleDate()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_article_date])->text();
}
QString TableAlgorithmInterface::getArticleTitle()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_article_title])->text();
}

QString TableAlgorithmInterface::getArticleData()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_article_data])->text();
}
QString TableAlgorithmInterface::getArticleUrl()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_article_url])->text();
}
QString TableAlgorithmInterface::getArticleParent()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_article_parent])->text();
}
QString TableAlgorithmInterface::getArticleOrder()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_article_order])->text();
}
QString TableAlgorithmInterface::getReplyUrl()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_reply_url])->text();
}
QString TableAlgorithmInterface::getArticleHit()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_article_hit])->text();
}

QString TableAlgorithmInterface::getArticleProfileUrl()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_article_profileurl])->text();
}

QString TableAlgorithmInterface::getArticleProfile()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_article_profile])->text();
}
QString TableAlgorithmInterface::getEtc()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_etc])->text();
}
QString TableAlgorithmInterface::getPlatformTitle()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_platform_title])->text();
}
QString TableAlgorithmInterface::getPlatformID()
{
    return m_pCurrentTable->item(m_nIndex, m_anData[E_DATA_platform_id])->text();
}

void TableAlgorithmInterface::next()
{
    m_nIndex++;
}

bool TableAlgorithmInterface::hasNext()
{
    if(m_nIndex < m_pCurrentTable->rowCount())
        return true;
    else
        return false;
}

int TableAlgorithmInterface::currentIndex()
{
    return m_nIndex;
}

void TableAlgorithmInterface::seek(int index)
{
    if(index >= length())
        m_nIndex = length() - 1;
    else
        m_nIndex = index;
}

int TableAlgorithmInterface::length()
{
    return m_pCurrentTable->rowCount();
}
void TableAlgorithmInterface::reset()
{
    m_nIndex = 0;
}

void TableAlgorithmInterface::moveFirst()
{
    reset();
}

void TableAlgorithmInterface::moveLast()
{
    m_nIndex = m_pCurrentTable->rowCount() - 1;
}

