#include <QtGui/QApplication>
#include "k9assistant.h"
#include <QMap>
#include "k9play.h"
#include <QTimer>
#include <QTranslator>
#include <QIcon>
#include "config.h"
#include "k9copy.h"
QMap<QString,QString> m_args;

void NameValue(const QString &argc,QString &name,QString &value){
   value="";name="";
   int pos=argc.indexOf("=");
   if (pos==-1) {
       name=argc;
   } else {
       name=argc.left(pos);
       value=argc.mid(pos+1);
   }

   if (name.startsWith("-"))
      name=name.mid(1);
   if (name.startsWith("-"))
      name=name.mid(1);
}

bool isSet(const QString _name) {
    return m_args.contains(_name);
}
QString getOption(const QString _name) {
    if (isSet(_name))
        return m_args[_name];
    else
        return "";
}



int main(int argc, char *argv[])
{

    QApplication a(argc, argv);
    a.setApplicationVersion(VERSION);
    a.setApplicationName("backlite");
    a.setWindowIcon(QIcon(":/icons/icon-app"));


    QString translatorFileName = QString("qt_%1").arg(QLocale::system().name());
    QTranslator qttranslator;

    if (qttranslator.load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
        a.installTranslator(&qttranslator);


    QTranslator translator;
    translator.load(":/tr/translation");
    a.installTranslator(&translator);


    QString name,value;
    for (int i=0; i< argc;i++) {
        QString arg=QString::fromUtf8(argv[i]);
        if (arg.startsWith("-"))
            NameValue(arg,name,value);
        else
            value=arg;
        m_args[name]=value;
    }

    if (isSet("play")) {
        QObject obj;
        k9play player(&obj);
        player.setinitStatus( isSet("initstatus"));
        player.setcontinue( isSet("continue"));
        player.setfirstPass(isSet("firstpass"));
        player.setuseCache(isSet("usecache"));
        player.setDevice(getOption("input"));
        player.setTitle(getOption("dvdtitle").toInt());
        player.setstartSector(getOption("startsector"));
        player.setendSector(getOption("endsector") );
        player.setaudioFilter( getOption("audiofilter"));
        player.setsubpictureFilter( getOption("subpicturefilter"));
        player.setvampsFactor( getOption("vampsfactor"));
        player.setinputSize( getOption("inputsize"));
        player.setchapter(getOption("chapter"));
        player.setcell(getOption("cell"));
        player.setinject(getOption("inject"));
        player.settotalSize(getOption("totalsize"));
        player.setdvdSize(getOption("dvdsize"));
        player.setchapterSize( getOption("chapterSize"));
        player.setforcedFactor(isSet("ffactor"));
        player.setchapterList( getOption("chapterlist"));
        player.setexitOnEnd(true);
        player.setMainThread(true);

        QTimer::singleShot (100, &player, SLOT (execute ()));
        return a.exec();

    } else {
        if (!isSet("assistant")){
            k9Copy *k9=new k9Copy();
            if (isSet("input")) {
                k9->setInput(getOption("input"));
                k9->fileOpen();
            }

            k9->show();
            return a.exec();
        } else {
            k9Assistant w(0);
            w.run();
            return w.exec();
        }
    }
}
