/*
General Header
ChiefSoft July 2017
 */
#include "clog.h"
#include "cgi.h"
#include "input.h"
#include "mermsemr_api.h"
#include "safestring.h"
#include <string>
#include "pgsql.h"
#include "pgsql_wrapper.h"
#include "cfg.h"
#include <curl/curl.h>
#include "medTEmails.h"
/*
Other Headers
 */
#include "medTrTransporter.h"
#include "function_backoffice.h"
#include "function_members.h"
#include "function_agents.h"
#include "function_driver.h"

long trasnporter_call(CVars in, CVars &out) {
    logfmt(logINFO, "trasnporter_call()");

    long action = REQ_LONG(in, "action", 0, -1);
    CVars x;
    long ret = 0;
    const char * loc = getenv("REMOTE_ADDR");

    switch (action) {

        case MEDTRANS_TRANSP_LOGIN:
            return TranspLoginAdmin(in, out);
            break;

        case MEDTRANS_TRANSP_CREATE:
            return medTrCreateAgent(in, out);
            break;

        case MEDTRANS_LOAD_AGENT:
            REQ_LONG(in, "agent_id", 0, -1);
            return load_db_record(out, "SELECT *,id AS agent_id FROM agents WHERE id=%lu ", in["agent_id"].Long());

            break;
        case MEDTRANS_TRANSP_ADDLOCATION:
            return medTrCreateTransporter(in, out);
            break;

        case MEDTRANS_TRANSP_GET_TRANSLIST:
            return medTrTransporterTransportList(in, out);
            break;

        case MEDTRANS_TRANSP_VERIFYSESSION:
            REQ_LONG(in, "agent_id", 0, -1);
            REQ_STRING(in, "sessionid", 2, 100, "(.*)");
            ret = PHP_INVALID_SESSION;
            out["status"] = "INVALID_SESSION";
            if (TransporterSessionCheck(in["agent_id"].Long(), in["sessionid"].c_str(), 0) == 1L) {
                out = in;
                out["status"] = "VALID_SESSION";
                ret = PHP_VALID_SESSION;
            }
            break;

        case MEDTRANS_TRANSP_GET_TRANSLATOR:
            return medTrLoadIntepreter(in, out);
            break;
        case MEDTRANS_TRANSP_GET_TRANSPORTER:
            return medTrLoadTransporter(in, out);
            break;

        case MEDTRANS_TRANSP_ADD_TRANSLATOR:
            return medTrCreateIntepreter(in, out);
            break;

        case MEDTRANS_TRANSP_LIST_TRANSLATOR:
            return medTrTransporterTranslatortList(in, out);
            break;

        case MEDTRANS_TRANSP_LISTLOCATION:
            return medTrTransporterLocationtList(in, out); // list locations for a particular agent
            break;

        case MEDTRANS_TRANSP_EDIT_TRANSLATOR:
            return medTrUpdateItepreter(in, out);
            break;
        case MEDTRANS_TRANSP_EDITLOCATION:
            return medTrUpdateTransporter(in, out);
            break;

        case MEDTRANS_TRANSP_MANAGELANGUAGE:
            return medTrInterpreterLanguage(in, out);
            break;

        case MEDTRANS_TRANSP_LANGUAGELIST:
            return medTrInterpreterLangList(in, out);
            break;

        case MEDTRANS_TRANSP_AGENTLANGUAGE:
            return medTrAgentLangList(in, out);
            break;


        case MEDTRANS_TRANSP_DRIVERSCALL:
            return medTrDriverFunctions(in, out);
            break;

        case MEDTRANS_TRANSP_ASSIGNTRANSPORT:
            return medAssignTranspoter(in, out);
            break;

        case MEDTRANS_TRANSP_DEVICELIST:
            return medTrDeviceList(in, out);
            break;

            
    }
    logfmt(logINFO, "/trasnporter_call()");
    return ret;
}
