/*   -*- coding: utf-8 -*-  */
#include <stdlib.h> 
#include <string.h>
#include <ctype.h>
#include "Buffer.h"
#include "Wicri.h"
#include "StrDict.h"
#include "Utf8Converter.h"

StrDict        *WicriTableUnivKey2Fr=NULL;
StrDict        *WicriTableUnivLcKey2Fr=NULL;


SxmlNode *WicriUnivFrFromKey(char *enStr)
{
  char *orgStr;
  if(!WicriTableUnivKey2Fr)
    {
      char *DILIB;
      Buffer* bufTableName;
      DILIB=getenv("DILIB");
      bufTableName=NewBuffer();
      BufferStrcpy(bufTableName, DILIB);
      /*      BufferStrcat(bufTableName, "/data/Wicri/BigUnivKey.fr.tab"); */
      BufferStrcat(bufTableName, "/data/Wicri/Metadata/BigUnivToPays.tab");
      WicriTableUnivKey2Fr=StrDictFromFile(BufferString(bufTableName));
    }
  if (!enStr)return NULL;
  orgStr= StrDictSearch(WicriTableUnivKey2Fr, enStr);
  if (!orgStr)return NULL;
  return SxmlFromString(orgStr);
}

SxmlNode *WicriUnivFrFromLcKey(char *enStr)
{
  char *orgStr;
  if(!WicriTableUnivLcKey2Fr)
    {
      char *DILIB;
      Buffer* bufTableName;
      DILIB=getenv("DILIB");
      bufTableName=NewBuffer();
      BufferStrcpy(bufTableName, DILIB);
      /*      BufferStrcat(bufTableName, "/data/Wicri/BigUnivKey.fr.tab"); */
      BufferStrcat(bufTableName, "/data/Wicri/Metadata/BigUnivLcToPays.tab");
      WicriTableUnivLcKey2Fr=StrDictFromFile(BufferString(bufTableName));
    }
  if (!enStr)return NULL;
  orgStr= StrDictSearch(WicriTableUnivLcKey2Fr, enStr);
  if (!orgStr)return NULL;
  return SxmlFromString(orgStr);
}

SxmlNode *WicriUnivFrFromSubstr(char *str)
{
  SxmlNode *univNode;
  char *posPar;
  char *lowStr;
  static Buffer *bUniv=NULL;
  char *posUniv;

  if (!str)return NULL;
  univNode=WicriUnivFrFromKey(str);
  if (univNode)return univNode;
  lowStr=Utf8StringToSort(str);
  univNode=WicriUnivFrFromLcKey(lowStr);
  if (univNode)return univNode;
  if (strncmp(lowStr, "the ",4)==0)
    {
      univNode=WicriUnivFrFromKey(str+4);
      if (univNode)return univNode;
    }
  posUniv=strstr(str, "Univ");
  if (posUniv>str)
    {
      univNode=WicriUnivFrFromKey(posUniv);
      if (univNode)return univNode;
    }
  posPar=strchr(str,'(');
  if (!posPar) return NULL;
  if (!bUniv) bUniv=NewBuffer();
  if (posPar < (str+2)) return NULL;
  BufferStrncpy(bUniv, str, posPar-str-1);
  while (BufferTailCmp(bUniv," ")==0) BufferTailCut(bUniv,1);
  univNode=WicriUnivFrFromKey(BufferString(bUniv));
  if (univNode)return univNode;
  lowStr=Utf8StringToSort(BufferString(bUniv));
  return WicriUnivFrFromLcKey(lowStr);
}

char *WicriUnivFindSubstrInField(char *str)
{
  static Buffer *bufStr=NULL;
  char *posComma;
  char *posUniv;
  int offsetUniv;
  int offsetBack;
  int offsetCommaBefore;
  if (!bufStr) bufStr=NewBuffer();
  posUniv=strstr(str, "Univ");
  if (!posUniv) posUniv=strstr(str, "univ");
  if (!posUniv) return NULL;
  offsetUniv=posUniv-str;
  if (offsetUniv==0)
    {
      posComma=strchr(posUniv,',');
      if (!posComma) BufferStrcpy(bufStr, posUniv);
      else
	{BufferStrncpy(bufStr, posUniv, posComma-posUniv);}
      return BufferString(bufStr);
    }
  offsetCommaBefore=offsetUniv;
  while ((--offsetCommaBefore)>=0)
    {
      if (str[offsetCommaBefore]==',') break;
    }
  if (offsetCommaBefore<=0)
    {
      posComma=strchr(posUniv,',');
      if (!posComma) return str;
      else
	{BufferStrncpy(bufStr, str, posComma-str);}
      return BufferString(bufStr);
    }  
  posComma=strchr(posUniv,',');
  if (!posComma) return str+offsetCommaBefore+2;
  BufferStrncpy(bufStr, str+offsetCommaBefore+2, posComma-str-offsetCommaBefore-2);
  return BufferString(bufStr);
}

SxmlNode *WicriAffiliationSetUnivFromField(SxmlNode *affiliation, char *field)
{
  char *level;
  char *subStr;
  SxmlNode *orgNode;
  SxmlNode *orgNameNode;
  SxmlNode *placeNameInDict;
  SxmlNode *placeNameInAff;
  SxmlNode *countryInDict;
  SxmlNode *countryInAff;
  SxmlNode *settlement;

  level=SxmlGetAttribute(affiliation, "wicri:level");
  if (level && (level[0]>'3')) return NULL;
  if (!level)level="0";
  subStr=WicriUnivFindSubstrInField(field);
  if (!subStr) return NULL;
  orgNode=WicriUnivFrFromSubstr(subStr);
  if (!orgNode)return NULL;
  orgNameNode=SxmlGetFirstChildByTagName(orgNode,"orgName");
  if (!orgNameNode)return NULL;
  SxmlAppendChild(affiliation, SxmlClone(orgNameNode));
  SxmlSetAttribute (affiliation, "wicri:level", "4");
  if (level[0]=='3') return orgNameNode;
  if (level[0]=='0')
    {
      countryInDict=SxmlGetFirstChildByTagName(orgNode,"country");
      countryInAff=SxmlGetFirstChildByTagName(affiliation,"country");
      if ((!countryInAff)&&countryInDict) SxmlAppendChild(affiliation, SxmlClone(countryInDict));
      level="1";
    }
  placeNameInAff=SxmlGetFirstChildByTagName(affiliation,"placeName");
  placeNameInDict=SxmlGetFirstChildByTagName(orgNode,"placeName");
  if (! placeNameInDict) return orgNameNode;
  if ((level[0]=='1')||(! placeNameInAff))
    {
      if (placeNameInDict) SxmlAppendChild(affiliation, SxmlClone(placeNameInDict));
      return orgNameNode;
    }
  settlement=SxmlGetFirstChildByTagName(placeNameInDict, "settlement");
  if (!settlement)return orgNameNode;
  SxmlAppendChild(placeNameInAff, SxmlClone(settlement));
  return orgNameNode;
}
