/*   -*- coding: utf-8 -*-  */

/***********************************************************************
*
*        Module   : Tei
*        Fichier  : TeiDoubles.c
*        Auteur   : J. DUCLOY
*        Date     : decembre 2015
*
************************************************************************/

#include "TeiHandler.h"
#include "Utf8Text.h"
#include <ctype.h>

char *TeiBuildDoubleKey(SxmlNode *teiRoot, SxmlNode *publicationStmt)
{
  SxmlNode *issn;
  SxmlNode *date;
  SxmlNode *firstAuthor;
  SxmlNode *title;
  char *sortTitle;
  static SxPathResult *pathTeiISSN=NULL;
  static SxPathResult *pathTeiFirstAuthor=NULL;
  static SxPathResult *pathTeiFirstTitle=NULL;
  static Buffer *bufKey=NULL;
  char *posSpace;
  char *textTitle;
  char *firstWordTitle;
  char *w2;
  char *w3;
  static Utf8TextSplitEngine *textEngine=NULL;

  if (!bufKey)bufKey=NewBuffer();  
  if(!pathTeiISSN)pathTeiISSN=SxPathFirstCompile("teiHeader/fileDesc/sourceDesc/biblStruct/series/idno@type=ISSN");
  if(!textEngine)
    {
      textEngine=Utf8TextSplitEngineCreate();
      Utf8SplitEngineSetTransMode(textEngine, 's');
      /*
      Utf8SplitEngineSetMinLenght(textEngine, 3);
      Utf8SplitEngineSetStopWordDict(textEngine, "DILIB", "Data/Eng/StopWordTit.dict");
      */
    }
  issn=SxPathFirstResultNode(pathTeiISSN, teiRoot);
  if (!issn)return NULL;  
  if(!SxmlIsLeaf(issn))return NULL;
  BufferStrcpy(bufKey, SxmlLeafText(issn));

  date=SxmlGetFirstChildByTagName(publicationStmt, "date");
  if (!date) return NULL;
  BufferStrcat(bufKey,":");
  BufferStrncat(bufKey, SxmlLeafText(date),4);

  if(!pathTeiFirstAuthor) pathTeiFirstAuthor=SxPathFirstCompile("teiHeader/fileDesc/sourceDesc/biblStruct/analytic/author/name");
  firstAuthor=SxPathFirstResultNode(pathTeiFirstAuthor, teiRoot);
  if (!firstAuthor) return NULL;
  BufferStrcat(bufKey,":");
  BufferStrcat(bufKey, SxmlGetAttribute(firstAuthor, "uniqKey"));

  if(!pathTeiFirstTitle) pathTeiFirstTitle=SxPathFirstCompile("teiHeader/fileDesc/titleStmt/title");
  title=SxPathFirstResultNode(pathTeiFirstTitle, teiRoot);
  if (!title) return NULL;
  textTitle=SxmlGetFirstText(title);
  if (!textTitle)return NULL;
  Utf8SplitEngineSetText(textEngine, textTitle);
  firstWordTitle=Utf8SplitGetStringItem(textEngine);
  if (!firstWordTitle)return NULL;
  BufferStrcat(bufKey,":");
  BufferStrcat(bufKey, firstWordTitle);

  w2=Utf8SplitGetStringItem(textEngine);
  if (!w2)return NULL;
  BufferStrcat(bufKey,":");
  BufferStrcat(bufKey, w2);

  w3=Utf8SplitGetStringItem(textEngine);
  if (!w3)return NULL;
  BufferStrcat(bufKey,":");
  BufferStrcat(bufKey, w3);

  /*
  sortTitle=Utf8StringToSort(textTitle);
  posSpace=strchr(sortTitle,' ');
  if (!posSpace) BufferStrcat(bufKey, sortTitle);
  else  BufferStrncat(bufKey, sortTitle, posSpace-sortTitle);
  */

  return BufferString(bufKey);
}

SxmlNode *TeiSetDoubleKey(SxmlNode *teiRoot)
{
  SxmlNode *publicationStmt;
  SxmlNode *newIdno;
  char *doubleKey;
  publicationStmt=TeiFindPublicationStmt(teiRoot);
  if (!publicationStmt)return NULL;
  doubleKey=TeiBuildDoubleKey(teiRoot, publicationStmt);
  if (!doubleKey)return NULL;
  newIdno=SxmlLeafCreate("idno", doubleKey);
  SxmlSetAttribute(newIdno, "type", "wicri:doubleKey");
  SxmlAppendChild(publicationStmt, newIdno);
  return newIdno;
}

int TeiDoublesGetLevel(char *stream)
{
  if (strcmp(stream,"ISTEX")==0) return 9;
  if (strcmp(stream,"INIST")==0) return 8;
  if (strcmp(stream,"PMC")==0) return 7;
  if (strcmp(stream,"PubMed")==0) return 6;
  if (strcmp(stream,"HAL")==0) return 5;
  return 0;
}

void TeiDoublesCompleteMaster(SxmlNode *masterTeiRoot, SxmlNode *container)
{
  SxmlNode *teiContainer;
  SxmlNode *masterPublicationStmt;
  SxmlNode *streamPublicationStmt;
  SxmlNode *idnoNode;
  SxmlNode *masterTextClass;
  SxmlNode *streamTextClass;
  static SxPathResult *teiPathTextClass=NULL;
  if (!teiPathTextClass)teiPathTextClass=SxPathFirstCompile("teiHeader/profileDesc/textClass");

  teiContainer=SxmlFirstChild(container);
  masterPublicationStmt=TeiFindPublicationStmt(masterTeiRoot);  /* to be deprecated ??? */
  streamPublicationStmt=TeiFindPublicationStmt(teiContainer);
  SxmlReset(streamPublicationStmt);
  while ((idnoNode=SxmlNextNode(streamPublicationStmt)))
    {
      if (SxmlNodeHasName(idnoNode,"idno"))
	{
	  char *type;
	  type=SxmlGetAttribute(idnoNode, "type");
	  if (strcmp(type,"url")==0) 
	    {
	      SxmlAppendChild(masterPublicationStmt, SxmlClone(idnoNode));
	      continue;
	    }
	  if (strcmp(type,"RBID")==0) 
	    {
	      SxmlAppendChild(masterPublicationStmt, SxmlClone(idnoNode));
	      continue;
	    }
	  if (strncmp(type,"wicri:",6)==0) 
	    {
	      SxmlAppendChild(masterPublicationStmt, SxmlClone(idnoNode));
	      continue;
	    }
	}
    }
  masterTextClass=SxPathFirstResultNode(teiPathTextClass, masterTeiRoot);
  streamTextClass=SxPathFirstResultNode(teiPathTextClass, teiContainer);
  if(streamTextClass)
    {
      SxmlNode *streamKeywords;
      SxmlReset(streamTextClass);
      while((streamKeywords=SxmlNextNode(streamTextClass)))
	{
	  /* SxmlAppendChild(masterTextClass, SxmlClone(streamKeywords)); */
	  TeiKeywordsPutTeiKeyword(streamKeywords);
	}
    }
} 

SxmlNode *TeiDoublesBuildRecordFromList(SxmlNode *listDoubles)
{
  SxmlNode *record;
  SxmlNode *masterNode;
  SxmlNode *masterTei;
  SxmlNode *doubleNode;

  record=SxmlElementCreate("record");
  SxmlReset(listDoubles);
  masterNode=NULL;
  while((doubleNode=SxmlNextNode(listDoubles)))
    {
      if(!masterNode)
	{
	  masterNode=doubleNode;
	  continue;
	}
      if  (TeiDoublesGetLevel(SxmlNodeName(masterNode)) < TeiDoublesGetLevel(SxmlNodeName(doubleNode))) 
	{
	  masterNode=doubleNode;
	  continue;
	}
    }
  /* SxmlRemoveChild(masterNode); */
  /* SxmlAppendChild(record, masterTei=SxmlRemoveChild(SxmlFirstChild(masterNode))); */
  SxmlAppendChild(record, masterTei=SxmlClone(SxmlFirstChild(masterNode)));
  TeiHandlerInit(record);
  /* SxmlFree(masterNode); */

  SxmlAppendChild(record, listDoubles);

  SxmlReset(listDoubles);
  while((doubleNode=SxmlNextNode(listDoubles)))
    {
      if(doubleNode==masterNode) continue;
      TeiDoublesCompleteMaster(masterTei, doubleNode);
    }

  return record;
}
