#include <iostream>

#include "core/basics/StringTools.h"
#include "vislearning/baselib/Globals.h"
#include "core/basics/ossettings.h"
#include "core/basics/Exception.h"

#include <sys/types.h>
#include <sys/stat.h>
#ifdef WIN32
#ifdef NICE_USELIB_BOOST
	#include "boost/filesystem.hpp"
#endif
#else
	#include <unistd.h>
#endif

using namespace OBJREC;
using namespace NICE;
using namespace std;



std::string Globals::currentImgFN = "";

void Globals::setCurrentImgFN ( const std::string & imgfn )
{
    currentImgFN = imgfn;
}

std::string Globals::getCurrentImgFN ( )
{
    return currentImgFN;
}

std::string Globals::getCacheFilename ( const std::string & root, int cache_mode )
{
  std::string filename = Globals::getCurrentImgFN();

  if ( filename.length() == 0 ) {
    fthrow(Exception, "Globals::getCacheFilename: current image filename was not set, please use Globals::setCurrentImgFN()");
  }

  vector<string> mylistdir;
  StringTools::split ( filename, FILESEP, mylistdir );

	int dirpart = 2;
	if ( cache_mode == SORT_CATEGORIES_SECONDPART )
	{
		cache_mode = SORT_CATEGORIES;
		dirpart = 3;
	}

    if ( cache_mode == SORT_CATEGORIES ) 
    {
      if ( mylistdir.size() < 2 )
      {
        cerr << "Globals::getCacheFilename: unable to parse filename " << filename << endl;
        exit(-1);
      }
      std::string name = mylistdir[mylistdir.size()-1];
      
      vector<string> mylist;
      StringTools::split ( name, '.', mylist );
      
      name = "";
      for ( uint k = 0 ; k < mylist.size()-1 ; k++ )
        if ( k == 0 )
          name = name + mylist[k];
        else 
          name = name + "." + mylist[k];

      std::string dir = root + FILESEPSTRING + mylistdir[mylistdir.size()-dirpart];

      struct stat info;

      if ( stat (dir.c_str(), &info ) < 0 ) {
#ifdef WIN32
#ifdef NICE_USELIB_BOOST
		  boost::filesystem::path t_path(dir.c_str());
		  if ( boost::filesystem::create_directory(t_path) < 0 )
        {
          cerr << "Globals::getCacheFilename: unable to create directory: " << dir << endl;
          exit(-1);
        }
#else
		  fthrow(Exception,"mkdir function not defined on system. try using boost lib and rebuild library");
#endif
#else
		  if ( mkdir(dir.c_str(), 0755) < 0 )
        {
          cerr << "Globals::getCacheFilename: unable to create directory: " << dir << endl;
          exit(-1);
        }
#endif
      }
      
      std::string id = dir + FILESEPSTRING + name;
      return id;

    } else if ( cache_mode == SORT_NONE ) {

      vector<string> mylist;
      std::string name = mylistdir[ mylistdir.size() - 1 ];

      StringTools::split ( name, '.', mylist );
      if ( mylist.size() != 2 )
      {
        cerr << "Globals::getCacheFilename: unable to parse filename " << name << endl;
        exit(-1);
      }
      name = mylist[0];

      std::string id = root + FILESEPSTRING + name;

      return id;
    } else {
      fprintf (stderr, "Globals::getCacheFilename: unknown mode\n");
    }

    return "";
}

int Globals::getCacheMode ( const std::string & desc )
{
    if ( desc == "cat" ) {
		return SORT_CATEGORIES;
    } else if ( desc == "cat2" ) {
		return SORT_CATEGORIES_SECONDPART;
    } else if ( desc == "none" ) {
		return SORT_NONE;
    } else {
		fprintf (stderr, "Globals::getCacheMode: mode unknown >%s<\n", desc.c_str() );
		exit(-1);
    }
}