#include "ymfilter.h"
#include "QMapIterator"
#include <QDebug>
YMFilter::YMFilter()
{

}

YMFilter::~YMFilter()
{

}

void YMFilter::setParam(QMap<QString, QString> mapParam)
{
    m_mapParam = mapParam;
}

void YMFilter::clearParam()
{
    m_mapParam.clear();
}

QMap<QString, int> YMFilter::RankFilterALL(QMap<QString, int> mapResult)
{
    QMap<QString, QMap<int, QString> > Result;
    //QMap<int, QString> Result;

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        QString key = slkey.at(0).trimmed();
        int nLength = slkey.length();

        for(int i = 1; i < nLength - 2 ; i++)
        {
            key += "~!@";
            key += slkey.at(i).trimmed();
        }



        //qDebug() << slkey.at(0);
        if(Result.contains(key))
        {
            Result[key].insertMulti(iterPos.value(), iterPos.key());
        }
        else
        {
            QMap<int, QString> qLast;
            qLast.insert(iterPos.value(), iterPos.key());
            Result.insert(key, qLast);
        }

    }

    int nRank = m_mapParam.value("RankFilterAll").toInt();

    QMap <QString, int> totalResult;
    //qDebug() << "rankfilter:" << nRank;
    for(QMap<QString, QMap<int, QString> >::iterator iterPos = Result.begin(); iterPos != Result.end(); iterPos++)
    {
        QMapIterator <int, QString> iter(Result[iterPos.key()]);
        iter.toBack();
        int i = 0;
        while(iter.hasPrevious())
        {
            if(i >= nRank)
                break;

            iter.previous();
            totalResult.insertMulti(iter.value(), iter.key());

            i++;
        }
    }

    return totalResult;
}

QMap<QString, int> YMFilter::CounterFilter(QMap<QString, int> mapResult)
{
    QMap<int, QString> Result;

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        Result.insertMulti(iterPos.value(), iterPos.key());
    }

    int nCount = m_mapParam.value("CounterFilter").toInt();

    QMapIterator <int, QString> iter(Result);
    QMap<QString, int> totalResult;
    iter.toBack();
    while(iter.hasPrevious())
    {
        if(iter.key() > nCount)
            break;

        iter.previous();
        totalResult.insertMulti(iter.value(), iter.key());
    }

    return totalResult;
}

void YMFilter::AppendMap(QMap<QString, int> &Dest, QMap<QString, int> &Source)
{
    for(QMap<QString, int>::iterator iterPos = Source.begin(); iterPos != Source.end(); iterPos++)
    {
        if(!Dest.contains(iterPos.key()))
        {
            Dest.insert(iterPos.key(), iterPos.value());
        }
    }
}

QMap<QString, int> YMFilter::Exec(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;
    if(!m_mapParam.value("Extractor").isEmpty())
    {
        AppendMap(totalResult, Extractor(mapResult));
    }
    if(!m_mapParam.value("KeywordFilter").isEmpty())
    {
        mapResult = KeywordFilter(mapResult);
        //AppendMap(totalResult, KeywordFilter(mapResult));
    }
    if(!m_mapParam.value("KeywordLengthFilter").isEmpty())
    {
        mapResult = KeywordLengthFilter(mapResult);
        //totalResult = KeywordLengthFilter(totalResult);
    }
    AppendMap(totalResult, mapResult);
    if(!m_mapParam.value("RankFilterAll").isEmpty())
    {
        totalResult = RankFilterALL(totalResult);
    }
    else if(!m_mapParam.value("RankFilterSeparated").isEmpty())
    {
        totalResult = RankFilterSeparated(totalResult);
    }
    if(!m_mapParam.value("CounterFilter").isEmpty())
    {
        totalResult = RankFilterSeparated(totalResult);
    }

    return totalResult;
}


