#include "imdb_movie.hpp"

#include "movie.hpp"

using std::string;

CIMDBMovie::CIMDBMovie(Entry e)
  : real_name(""), m_strDirector(""), m_strWritingCredits(""), m_strGenre(""),
    m_strTagLine(""), m_strPlotOutline(""), m_strPlot(""), 
    m_strPictureURL(""), m_strTitle(""), m_strRuntime(""), m_iYear(0),
    m_fRating(0.0), m_strVotes(""), m_strCast(""), m_iTop250(0)
{
  // entry
  filetype = e.filetype;
  filenames = e.filenames;
  // simplefile
  id = e.id;
  name = e.name;
  lowercase_name = e.lowercase_name;
  path = e.path;
  type = e.type;
}

void CIMDBMovie::Save(const string &strFileName)
{
  FILE* fd=fopen(strFileName.c_str(), "wb+");
  if (!fd) return;
  SaveString(m_strDirector,fd);
  SaveString(m_strGenre,fd);
  SaveString(m_strPictureURL,fd);
  SaveString(m_strPlot,fd);
  SaveString(m_strPlotOutline,fd);
  SaveString(m_strTagLine,fd);
  SaveString(m_strWritingCredits,fd);
  SaveString(m_strTitle,fd);
  SaveString(m_strVotes,fd);
  SaveString(m_strCast,fd);
  SaveString(m_strRuntime,fd);

  fwrite(&m_iYear,1,sizeof(int),fd);
  fwrite(&m_fRating,1,sizeof(float),fd);
  fwrite(&m_iTop250,1,sizeof(int),fd);
	
  fclose(fd);
}

bool CIMDBMovie::Load(const string& strFileName)
{
  FILE* fd=fopen(strFileName.c_str(), "rb+");
  if (!fd) return false;
  if(LoadString(m_strDirector,fd))
    {
      if(LoadString(m_strGenre,fd))
	{
	  if(LoadString(m_strPictureURL,fd))
	    {
	      if(LoadString(m_strPlot,fd))
		{
		  if(LoadString(m_strPlotOutline,fd))
		    {
		      if(LoadString(m_strTagLine,fd))
			{
			  if(LoadString(m_strWritingCredits,fd))
			    {
			      if(LoadString(m_strTitle,fd))
				{
				  if(LoadString(m_strVotes,fd))
				    {	
				      if(LoadString(m_strCast,fd))
					{	
					  if(LoadString(m_strRuntime,fd))
					    {
					      fread(&m_iYear,1,sizeof(int),fd);
					      fread(&m_fRating,1,sizeof(float),fd);
					      fread(&m_iTop250,1,sizeof(int),fd);
					      fclose(fd);
					      return true;
					    }
					}
				    }
				}
			    }
			}
		    }
		}
	    }
	}
    }
  fclose(fd);
  return false;
}

string CIMDBMovie::cover_path()
{
  Movie &movie = Movie::get_instance();

  // Try to find a cover in the movie folder to enable users to overwrite the
  // imdb cover 
  string cover = movie.get_cover_from_dir(this->path);

  if (!cover.empty())
    return cover;

  // cover not found in directory of movie, use the one from imdb
  return movie.imdb_dir + name + "_cover.jpg";
}

void CIMDBMovie::SaveString(const string &strTxt, FILE *fd)
{
  int iSize=strTxt.size();
  fwrite(&iSize,1,sizeof(int),fd);
  if (iSize > 0)
    {
      fwrite(&strTxt.c_str()[0],1,iSize,fd);
    }
}

bool CIMDBMovie::LoadString(string &strTxt, FILE *fd)
{
  strTxt="";
  int iSize;
  int iRead=fread(&iSize,1,sizeof(int),fd);
  if (iRead != sizeof(int) ) return false;
  if (feof(fd)) return false;
  if (iSize==0) return true;
  if (iSize > 0 && iSize < 16384)
    {
      char *szTmp = new char [iSize+2];
      iRead=fread(szTmp,1,iSize,fd);
      if (iRead != iSize)
	{
	  delete [] szTmp;
	  return false;
	}
      szTmp[iSize]=0;
      strTxt=szTmp;
      delete [] szTmp;
      return true;
    }
  return false;
}

Entry CIMDBMovie::to_entry()
{
  Entry e;

  // entry
  e.filetype = filetype;
  e.filenames = filenames;
  // simplefile
  e.id = id;
  e.name = name;
  e.path = path;
  e.type = type;
  
  return e;
}
