/*
 * This file is part of libtqtvd_astronav
 *
 * Copyright (C) 2008-2017 TQTVD Software
 * Copyright (C) 2017 Mirakulo Software (mirakulo@mirakulo.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */


#include "IAstroNavPlayer.h"
#include <string>
#include <pthread.h>
#include <map>

using namespace AstroNav;
using namespace std;


#if 0
#define DEBUGF(format, args...) printf ( format, args )
#else
#define DEBUGF(format, args...)
#endif


// for syncronization...
static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
#define _LOCK_    do { DEBUGF("%lu lock   %s\n", pthread_self(), __FUNCTION__); pthread_mutex_lock(&g_mutex);   } while(0);
#define _UNLOCK_  do { DEBUGF("%lu unlock %s\n", pthread_self(), __FUNCTION__); pthread_mutex_unlock(&g_mutex); } while(0);


extern "C" DLLEXPORT unsigned long astroOpen(void* fileServer, const char *filePath)
{
    DEBUGF("<astroOpen file='%s' server=0x%08lx >\n", filePath, fileServer);

    _LOCK_;

    AstroNavPlayerFileServerWrapper* serverWrapper = (AstroNavPlayerFileServerWrapper*)(fileServer);
    IAstroNavPlayerFileServerPtr server = serverWrapper->fileserver().lock();
    unsigned long rc = (unsigned long)-1;

    if (server)
        rc = server->open(filePath, false);

    _UNLOCK_;

    DEBUGF("</astroOpen rc=%lu>\n", rc);

    return rc;
}


extern "C" DLLEXPORT unsigned long astroRead(void* fileServer, unsigned long fd, unsigned char *buff, unsigned long len)
{
    _LOCK_;

    unsigned long rc = 0;
    AstroNavPlayerFileServerWrapper* serverWrapper = (AstroNavPlayerFileServerWrapper*)(fileServer);
    IAstroNavPlayerFileServerPtr server = serverWrapper->fileserver().lock();

    if (server)
       rc = server->read(fd, buff,len, false);

    DEBUGF("<astroRead fd=%lu rc=%lu />\n", fd, rc);

    _UNLOCK_;

    return rc;
}


extern "C" DLLEXPORT void astroClose(void* fileServer, unsigned long fd)
{

    _LOCK_;

    AstroNavPlayerFileServerWrapper* serverWrapper = (AstroNavPlayerFileServerWrapper*)(fileServer);
    IAstroNavPlayerFileServerPtr server = serverWrapper->fileserver().lock();

    if (server)
      server->close(fd);

    _UNLOCK_;

}


extern "C" DLLEXPORT unsigned long astroSize(void* fileServer, unsigned long fd)
{
    _LOCK_;

    unsigned long rc = 0;
    AstroNavPlayerFileServerWrapper* serverWrapper = (AstroNavPlayerFileServerWrapper*)(fileServer);
    IAstroNavPlayerFileServerPtr server = serverWrapper->fileserver().lock();

    if (server)
       rc = server->size(fd);

    DEBUGF("<astroSize fd=%lu rc=%lu />\n", fd, rc);

    _UNLOCK_;

    return rc;


}

extern "C" DLLEXPORT unsigned long astroSetFilePos(void* fileServer, unsigned long fd, unsigned long pos)
{
    _LOCK_;

    unsigned long rc = 0;
    AstroNavPlayerFileServerWrapper* serverWrapper = (AstroNavPlayerFileServerWrapper*)(fileServer);
    IAstroNavPlayerFileServerPtr server = serverWrapper->fileserver().lock();

    if (server)
       rc = server->setFilePos(fd, pos);

    DEBUGF("<astroSetFilePos fd=%lu pos=%lu rc=%lu />\n", fd, pos, rc);

    _UNLOCK_;

    return rc;

}


