#include "configcache.h"

#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/copy.hpp>

#define HEADER_READOK   0
#define HEADER_READERR  1
#define HEADER_READLAST 2

struct configcache::iobuffers
{
  boost::iostreams::filtering_ostreambuf *out;
};

configcache::configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize, const int& cachemode,
			 ostream *_log){
  log = _log;

  dataReader = new datread(configMemSize, log);
  
  NEQUI = nequi;
  NSKIP = nskip;
  DATADIR = datadir;
  CACHEID = cacheid;

  if ( cacheid.find("_") != -1 ) {
    if(log) *log << "CCACHE: Invalid cacheid \"" << cacheid << "\" given. Cacheids must not contain underscores!" << endl << flush;
    exit(1);
  }

  configMem = (char*)malloc(configMemSize);
  tmpConfig = (char*)malloc(configMemSize);

  *configmem = configMem;
  configSize = configMemSize;

  ioBuffers = new iobuffers;
  ioBuffers->out = NULL;

  MODE = cachemode;

  refetchDataFiles = false;
}

string configcache::paraString() {
  stringstream parastring;

  for(int ipara=0; ipara<Paras.size(); ipara++)
    parastring << "_" << Paras[ipara].id << Paras[ipara].val;
  
  return parastring.str();
}

string configcache::getFileId(int actnequi, const bool& superextended, const bool& shortid)
{
  stringstream fileid;

  if(!shortid) fileid << CACHEID << "_" << actnequi << "_" << NSKIP;

  if( superextended )
    fileid << "_" << hash( paraString() );
  else
    fileid << paraString();

  return fileid.str();
}

void configcache::fetchDataFiles()
{
  struct dirent *de=NULL;
  DIR *d=NULL;
  static infiledesc filedesc;
  
  d=opendir(DATADIR.c_str());
  if(d != NULL){
    while(de = readdir(d)){
      string filename = de->d_name;
      if(isValidInFile(filename, &filedesc)) 
	{
	  inFiles.push_back(filedesc);
	}
    }
  }
}

bool configcache::isValidInFile(const string& infile, infiledesc *filedesc)
{
  char *inchar, *inParts;
  string truncIn, truncOut;

  filedesc->filename = infile;

  if( infile.size() < 4 ) return false;

  if( infile.substr(infile.size()-4) == ".dat" ) {
    filedesc->extended = false;
    filedesc->superextended = false;
  }
  else if( infile.substr(infile.size()-4) == "edat" ) {
    filedesc->extended = true;
    filedesc->superextended = false;
  }
  else if( infile.substr(infile.size()-4) == "sdat" ) {
    filedesc->extended = true;
    filedesc->superextended = true;
  }
  else
    return false;

  inchar = new char [infile.size()+1];
  strcpy (inchar, infile.c_str());

  inParts = strtok( inchar, "_" );
  for(int iPart=0; inParts!=NULL; iPart++)
    {
      if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }

      switch(iPart)
	{
	case 1: if(inParts != CACHEID)
	    return false;
	  break;
	case 2:
	  filedesc->nequi = atoi(inParts);
	  break;
	case 3: 
	  if(atoi(inParts) != NSKIP) 
	    return false;
	  filedesc->nskip = atoi(inParts);
	  break;
	}
      inParts = strtok( NULL, "_");
    }
  truncIn = truncIn.substr(0, truncIn.size()-4);

  delete[] inchar;

  if( truncIn.find( getFileId(NEQUI, filedesc->superextended, true) + "_" ) == string::npos ) return false;

  return true;
}

void * configcache::getHeader(const string& headerid) {
  return dataReader->getHeader(headerid);
}

void configcache::readConfig(bool *readnewconfig, int *nequileft, vector<unsigned long> *excludeFileHashes)
{
  *readnewconfig = false;

  if( DATADIR == "" || !(MODE==CACHE_MODE_RO||MODE==CACHE_MODE_RW) ) return;

  if(refetchDataFiles){
    refetchDataFiles = false;
    fetchDataFiles();
  }

  while(true)
    {
      vector<infiledesc>::iterator inFileIt = getNextInfile(excludeFileHashes);
      int iDidVirtualSkips;

      if( (!dataReader->fisopen()) && inFileIt == inFiles.end() ) {
	if (*readnewconfig)
	  *nequileft = nequileft_internal;
	return;
      }

      while( (!dataReader->fisopen()) && inFiles.size() > 0 ) {
	string inFileParaString;

	openFileDesc = *inFileIt;

	if (openFileDesc.nequi < NEQUI)
	  doVirtualEquilibration = true;
	else
	  doVirtualEquilibration = false;

	firstUsedConfig = true;

	if(log) *log << "CCACHE: Opening dat-file: " << inFileIt->filename << endl << flush;

	dataReader->openFile( DATADIR + "/" + inFileIt->filename );
		
	if( openFileDesc.superextended ) {
	  if( dataReader->getParaString() != paraString() ) {
	    if(log) *log << "CCACHE: Parastring does not match. Closing dat-file..." << endl << flush;
	    dataReader->closeFile();
	  }
	}

	inFiles.erase(inFileIt);
	
	if( !dataReader->fisopen() ) continue;
      }

      if( dataReader->fisopen() ) 
	{
	  if (doVirtualEquilibration) {
	    if(log) *log << "CCACHE: Trying virtual equilibration." << endl << flush;
	    doVirtualEquilibration = false;
	    for (iDidVirtualSkips=0; iDidVirtualSkips < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iDidVirtualSkips++) {
	      if( dataReader->readFullBlock(tmpConfig) < 0 )
		break;
	      else if ( (NEQUI-openFileDesc.nequi) - (iDidVirtualSkips+1)*openFileDesc.nskip < nequileft_internal ) {
		memcpy(configMem, tmpConfig, configSize);
		nequileft_internal = NEQUI - openFileDesc.nequi - (iDidVirtualSkips+1)*openFileDesc.nskip;
		*readnewconfig = true;
		firstUsedConfig = false;
	      }
	    }
	  }

	  if( dataReader->readFullBlock(tmpConfig) >= 0 )
	    {
	      memcpy(configMem, tmpConfig, configSize);
	      *readnewconfig = true;
	      if (firstUsedConfig) {
		firstUsedConfig = false;
		if (openFileDesc.nequi < NEQUI)
		  nequileft_internal = NEQUI - openFileDesc.nequi - iDidVirtualSkips*openFileDesc.nskip;
		else
		  nequileft_internal = NEQUI - openFileDesc.nequi;
	      }
	      nequileft_internal -= openFileDesc.nskip;
	      *nequileft = nequileft_internal;
	      return;
	    }
	}
    }
}

void configcache::openOutFile(int actnequi)
{ 
  time_t secstamp = time(NULL);
  int iseq=0;
  
  while (true) {
    outFileName.str("");
    outFileName << DATADIR << "/" << secstamp << "." << iseq << "_" << getFileId(actnequi, true, false) << "_.sdat.tmp";

    int tmpfd = open(outFileName.str().c_str(), O_CREAT | O_EXCL, 0644);

    if ( tmpfd != -1 ) {
      close(tmpfd);
      break;
    }
    else if ( errno != EEXIST ) {
      if(log) *log << "CCACHE: Could not create cachefile!" << endl << flush;
      exit(1);
    }

    iseq++;
  }
  
  outFile.open( outFileName.str().c_str(), std::ios::binary );

  outFile << paraString() << endl;

  ioBuffers->out = new boost::iostreams::filtering_ostreambuf;
  ioBuffers->out->push(boost::iostreams::bzip2_compressor());
  ioBuffers->out->push(outFile);
}

void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size, int actnequi) {
  unsigned long headeridhash;

  if( DATADIR == "" || !(MODE==CACHE_MODE_WO||MODE==CACHE_MODE_RW) ) return;

  if(!outFile.is_open())
    openOutFile(actnequi);

  headeridhash = hash(headerid);

  boost::iostreams::write(*ioBuffers->out, (char*)&size, sizeof(long unsigned int));
  boost::iostreams::write(*ioBuffers->out, (char*)&headeridhash, sizeof(unsigned long));
  boost::iostreams::write(*ioBuffers->out, header, size);
}

void configcache::writeConfig(int actnequi)
{
  long unsigned int zeroheader=0;

  if ( DATADIR == "" || !(MODE==CACHE_MODE_WO||MODE==CACHE_MODE_RW) ) return;

  if ( ! outFile.is_open() )
    openOutFile(actnequi);
  
  boost::iostreams::write(*ioBuffers->out, (char*)&zeroheader, sizeof(long unsigned int));

  boost::iostreams::write(*ioBuffers->out, configMem, configSize);
}

void configcache::addPara(const string& parid, const double& val){
  parameter newPara;
  newPara.id = parid;
  newPara.val = val;
  Paras.push_back(newPara);
}

int configcache::getParIndex(const string& parid){
  for(int ipara=0; ipara<Paras.size(); ipara++)
    if(Paras[ipara].id == parid) return ipara;
}

void configcache::setPara(const string& parid, const double& value){
  Paras[getParIndex(parid)].val = value;

  finishOutFile();
  dataReader->closeFile();
  inFiles.clear();

  refetchDataFiles = true;
  nequileft_internal = NEQUI;
}

configcache::~configcache()
{
  finishOutFile();
  delete dataReader;
}

void configcache::finishOutFile()
{
  if( ioBuffers->out != NULL )
    {
      delete ioBuffers->out;
      ioBuffers->out = NULL;
    }

  if( outFile.is_open() )
    {
      outFile.close();
      rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
    }
}

unsigned long configcache::hash(const string& str)
{
  unsigned long hash = 5381;

  for(string::const_iterator it=str.begin();it!=str.end();it++) 
    hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */

  return hash;
}

vector<infiledesc>::iterator configcache::getNextInfile(vector<unsigned long> *excludeFileHashes) {
  for (vector<infiledesc>::iterator init = inFiles.begin(); init != inFiles.end(); ++init) {
    if (excludeFileHashes != NULL) {
      bool excludethisfile = false;

      for (vector<unsigned long>::iterator exit = excludeFileHashes->begin(); exit != excludeFileHashes->end(); ++exit)
	if ( *exit == hash(init->filename) ) {
	  excludethisfile = true;
	  break;
	}

      if (excludethisfile)
	continue;
    }
    return init;
  }
  return inFiles.end();
}
