/***************************************************************************
 *   Copyright (C) 2009 by Eric MAEKER                                     *
 *   eric.maeker@free.fr                                                   *
 *   All rights reserved.                                                  *
 *                                                                         *
 *   Redistribution and use in source and binary forms, with or without    *
 *   modification, are permitted provided that the following conditions    *
 *   are met:                                                              *
 *   - Redistributions of source code must retain the above copyright      *
 *   notice, this list of conditions and the following disclaimer.         *
 *   - Redistributions in binary form must reproduce the above copyright   *
 *   notice, this list of conditions and the following disclaimer in the   *
 *   documentation and/or other materials provided with the distribution.  *
 *   - Neither the name of the FreeMedForms' organization nor the names of *
 *   its contributors may be used to endorse or promote products derived   *
 *   from this software without specific prior written permission.         *
 *                                                                         *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   *
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     *
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS     *
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE        *
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,  *
 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  *
 *   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      *
 *   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER      *
 *   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT    *
 *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN     *
 *   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE       *
 *   POSSIBILITY OF SUCH DAMAGE.                                           *
 ***************************************************************************/
#ifndef COMMON_COMMANDLINEPARSER_H
#define COMMON_COMMANDLINEPARSER_H
#include <common/xmlparser.h>
#include <QHash>
#include <QString>
#include <QApplication>
#include <QStringList>

namespace Common {

/**
  \brief Small command line parser. User the \e Param enum to get values of command line params.
*/
class CommandLine
{
public:

    enum Param {
        CL_ProgramName = 0,
        CL_ExchangeFile,
        CL_PatientName,
        CL_PatientFirstName,
        CL_DateOfBirth,
        CL_PatientUUID,
        CL_Price,
        CL_PriceCode,
        CL_CallingProgramUser,
        CL_MaxParam,
        CL_Version
    };

    CommandLine()
    {
        ref.insert(CL_ProgramName, "--ProgramName");
        ref.insert(CL_ExchangeFile, "--exchange");
        ref.insert(CL_PatientName, "--patientname");
        ref.insert(CL_PatientFirstName, "--patientfirstname");
        ref.insert(CL_DateOfBirth, "--dateofbirth");
        ref.insert(CL_PatientUUID, "--patientuuid");
        ref.insert(CL_Price,"--price");
        ref.insert(CL_PriceCode,"--pricecode");
        ref.insert(CL_CallingProgramUser,"--user");
        ref.insert(CL_MaxParam,"--params");
        ref.insert(CL_Version,"--version");
        QStringList args = qApp->arguments();
        int countArgs = args.size();
        qDebug() << __FILE__ << QString::number(__LINE__) << " arguments number = " << QString::number(countArgs);
        if(countArgs == 14){
            qWarning() << __FILE__ << QString::number(__LINE__) << " in countArgs = 14" ;
            qDebug() << __FILE__ << QString::number(__LINE__) << " arguments 4 = " << qApp->arguments()[4];
            qDebug() << __FILE__ << QString::number(__LINE__) << " arguments 5 = " << qApp->arguments()[5];
            qDebug() << __FILE__ << QString::number(__LINE__) << " arguments 6 = " << qApp->arguments()[6];
            qDebug() << __FILE__ << QString::number(__LINE__) << " arguments 7 = " << qApp->arguments()[7];
            qDebug() << __FILE__ << QString::number(__LINE__) << " arguments 8 = " << qApp->arguments()[8];
            qDebug() << __FILE__ << QString::number(__LINE__) << " arguments 9 = " << qApp->arguments()[9];
            qDebug() << __FILE__ << QString::number(__LINE__) << " arguments 10 = " << qApp->arguments()[10];
            qDebug() << __FILE__ << QString::number(__LINE__) << " arguments 11 = " << qApp->arguments()[11];
            qDebug() << __FILE__ << QString::number(__LINE__) << " arguments 12 = " << qApp->arguments()[12];
            qDebug() << __FILE__ << QString::number(__LINE__) << " arguments 12 = " << qApp->arguments()[13];
            QString GUID_Patient = qApp->arguments()[4];
            QString argsOfMedintux = qApp->arguments()[13];
            QStringList argsOfMedintuxList = argsOfMedintux.split(";");
            QStringList list ;
            list.append( argsOfMedintuxList[0]);
            list.append( argsOfMedintuxList[1]);
            list.append( argsOfMedintuxList[2]);
            list.append( argsOfMedintuxList[3]);
            list.append( "--patientuuid="+GUID_Patient);

            args = list;

        }

        if(countArgs < 2){
            qWarning() << __FILE__ << QString::number(__LINE__) << " in countArgs < 2" ;
            QHash<QString,QString> hashList;
            XmlParser *xml = new XmlParser;
            hashList       = xml->readXmlFile();
            params.insert(CL_ProgramName, hashList.value("programName"));//calling program, key = xml tag
            params.insert(CL_PatientName, hashList.value("name")); //name, key = xml tag
            params.insert(CL_PatientFirstName,hashList.value("firstname")); //firstname, key = xml tag
            params.insert(CL_DateOfBirth,hashList.value("dateofbirth"));
            params.insert(CL_PatientUUID, hashList.value("uid"));
            params.insert(CL_Price, hashList.value("price")); //price , from EMR, key = xml tag
            params.insert(CL_PriceCode, hashList.value("priceCode")); //price code, from EMR, key = xml tag
            params.insert(CL_CallingProgramUser, hashList.value("user"));//user of calling program
            params.insert(CL_MaxParam, hashList.value("otherparams"));//a string of other params if necessary
            params.insert(CL_Version, hashList.value("version"));//version of accountancy program
       //qDebug() << programName+"\n"+nomRecupere+"\n"+ prenomRecupere +"\n"+guidEntre+"\n"+ tarif +"\n"+codeTarif+"\n"+user;
            xml->purgeXmlFile();
            delete xml;
        }
        else{
        foreach(const QString a, args) {
            QString k = a;
            qDebug() << __FILE__ << QString::number(__LINE__) << " k = "+k;
            if (k.contains(" "))
                k = k.left(k.indexOf(" "));
            if (a.contains("="))
                k = k.left(k.indexOf("="));
            QString strForInsert = a.mid(a.indexOf("=")+1).remove("\"").trimmed();
            if(a.contains(" ")){
                strForInsert.replace(" ","_");
                }
            qDebug() << __FILE__ << QString::number(__LINE__) << " strForInsert = "+strForInsert;
            switch (ref.key(k,-1))
            {
                case CL_ProgramName : params.insert(CL_ProgramName,strForInsert ); break;
                case CL_ExchangeFile : params.insert(CL_ExchangeFile, strForInsert); break;
                case CL_PatientName : params.insert(CL_PatientName, strForInsert); break;
                case CL_PatientFirstName : params.insert(CL_PatientFirstName, strForInsert); break;
                case CL_DateOfBirth : params.insert(CL_DateOfBirth,strForInsert); break;
                case CL_PatientUUID : params.insert(CL_PatientUUID, strForInsert); break;
                case CL_Price : params.insert(CL_Price, strForInsert); break;
                case CL_PriceCode : params.insert(CL_PriceCode, strForInsert); break;
                case CL_CallingProgramUser : params.insert(CL_CallingProgramUser, strForInsert); break;
                case CL_MaxParam : params.insert(CL_MaxParam, strForInsert); break;
                case CL_Version : params.insert(CL_Version, strForInsert); break;
                default : break;
            }
        }
        }
    }

    QVariant value(int param, const QVariant &defaultValue = QVariant()) const
    {
        return params.value(param, defaultValue);
    }



    QString paramName(int param) const
    {
        if (ref.keys().contains(param))
            return ref.value(param);
        else
            return QString::number(param);
    }

    void setValue(int param,QVariant newValue){
        params.insert(param,newValue);

    }


private:
    QHash<int,QVariant> params;
    QHash<int, QString> ref;
};

}  // End namespace Common

#endif // COMMON_COMMANDLINEPARSER_H
