/***************************************************************************
 *   Copyright (C) 2009 by Nicholas Sinlock   *
 *   nsinlock@linux-1k4n   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <QString>
#include <QChar>
#include <QStringList>
#include <iostream>
#include <QtCore>

QString channelList[] = { "This Podcast", "Podcast Title", "The funnycast", "::Title with ascii art::" };
QString episodeList[] = { "This Podcast: January 14, 2009", "Podcast Title #001", "Funnycast 042: The Answer", "::Title with ascii art:: for January 14, 2009" };
QString correctAnswerList[] = { "January 14, 2009", "#001", "042: The Answer", "January 14, 2009" };

//QStringList badPredicateList;

QString decapitateString( const QString &input, const QString &ref )
{
    QString clean;
    int length;
    if( input.startsWith( ref, Qt::CaseInsensitive ) )   // if the episode title begins with the channel title...
    {
        clean = input.right( input.length() - ( ref.length() ) ).trimmed();   // ...remove the channel title from the episode title
    }
    else // otherwise search for any phrases which are common among the channel title and episode title and remove them from the episode title
    {
        for( length = 0; length < ref.length(); length++ )
        {
            if( ref[length].isSpace() && input.startsWith( ref.right( ref.length() - (length + 1)), Qt::CaseInsensitive ))
            {
	        clean = input.right( input.length() - (ref.length() - length) ).trimmed();
                break;
	    }
        }
    }

    for( length = 0; length < clean.length(); length++ ) // remove any extraneous characters from the beginning of the episode title such as extra punctuation symbols
    {
        if( clean[length].isLetterOrNumber() || clean[length] == '#')
	{
	    clean = clean.right( clean.length() - length );
	    //std::cout << "Clean Version: " << clean.toStdString() << std::endl;
            break;
        }
    }
    std::cout << "Clean Version: " << clean.toStdString() << std::endl;
    return clean.trimmed();
}

int main(int argc, char *argv[])
{
    QString cleanString;
    for( int i = 0; i < 5; i++ )
    {
        cleanString = decapitateString( episodeList[i], channelList[i] );
        if( cleanString.toUpper() != correctAnswerList[i].toUpper() )
        {
            std::cout << "Wrong Answer" << std::endl;
            std::cout << "Correct Answer: " << correctAnswerList[i].toStdString() << std::endl << std::endl;
        }
        else
            std::cout << "Right Answer\n" << std::endl;
    }
    return 0;
}

