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

StrDict *WicriAmeriqueUsaCodeState2Fr=NULL;
StrDict *WicriAmeriqueUsaState2Fr=NULL;
static Buffer *bufName=NULL;

SxmlNode *WicriAmeriqueGetUsaStateNodeFrFromCode(char *code)
{
  char *strState;
  SxmlNode *retNode;
  if (!WicriAmeriqueUsaCodeState2Fr)
    {
      if (!bufName)  bufName=NewBuffer();
      BufferStrcpy(bufName,WicriGetDataRoot());
      BufferStrcat(bufName,"/Amerique/UsaCode2StateFr.tab");
      WicriAmeriqueUsaCodeState2Fr=StrDictFromFile(BufferString(bufName));
    }
  strState=StrDictSearch(WicriAmeriqueUsaCodeState2Fr, code);
  if (!strState) return NULL;
  retNode=SxmlFromString(strState);
  return retNode;
}

SxmlNode *WicriAmeriqueGetUsaStateNodeFrFromState(char *code)
{
  char *strState;
  SxmlNode *retNode;
  if (!WicriAmeriqueUsaState2Fr)
    {
      if (!bufName)  bufName=NewBuffer();
      BufferStrcpy(bufName,WicriGetDataRoot());
      BufferStrcat(bufName,"/Amerique/UsaStateToFr.dict");
      WicriAmeriqueUsaState2Fr=StrDictFromFile(BufferString(bufName));
    }
  strState=StrDictSearch(WicriAmeriqueUsaState2Fr, code);
  if (!strState) return NULL;
  retNode=SxmlFromString(strState);
  return retNode;
}

SxmlNode *WicriAmeriqueSetRegionFromCodeState(SxmlNode *affiliation, char *code)
{
  SxmlNode *placeNode;
  SxmlNode *affCountry;
  SxmlNode *placeName;
  SxmlNode *retNode;

  placeNode=WicriAmeriqueGetUsaStateNodeFrFromCode(code);
  if (!placeNode)return NULL;
  affCountry=SxmlGetFirstChildByTagName(affiliation, "country");
  if (!affCountry) SxmlAppendChild(affiliation, SxmlLeafCreate("country","États-Unis"));
  placeName=SxmlGetFirstChildByTagName (placeNode, "placeName");
  SxmlAppendChild(affiliation, retNode=SxmlClone(placeName));
  return retNode;
}

SxmlNode *WicriAmeriqueSetRegionFromState(SxmlNode *affiliation, char *field)
{
  SxmlNode *placeNode;
  SxmlNode *affCountry;
  SxmlNode *placeName;
  SxmlNode *retNode;

  placeNode=WicriAmeriqueGetUsaStateNodeFrFromState(field);
  if (!placeNode)return NULL;
  affCountry=SxmlGetFirstChildByTagName(affiliation, "country");
  if (!affCountry) SxmlAppendChild(affiliation, SxmlLeafCreate("country","États-Unis"));
  placeName=SxmlGetFirstChildByTagName (placeNode, "placeName");
  SxmlAppendChild(affiliation, retNode=SxmlClone(placeName));
  return retNode;
}

SxmlNode *WicriAmeriqueGetRegionFromField(SxmlNode *affiliation, char *strRegion, char *regionArea)
{
  SxmlNode *retNode;
  static char *codeCountry=NULL;
  if(!codeCountry) codeCountry=malloc(3);
  codeCountry=strncpy(codeCountry, strRegion,2);
  retNode=WicriAmeriqueSetRegionFromCodeState(affiliation, codeCountry);
  if (retNode) 
    {
      SxmlSetAttribute(affiliation, "wicri:level", "2");
      return retNode;
    }
  retNode=WicriAmeriqueSetRegionFromState(affiliation, strRegion);
  if (retNode) 
    {
      SxmlSetAttribute(affiliation, "wicri:level", "2");
      return retNode;
    }
  return NULL;
}

char *WicriAmeriqueGetUsaStateNameFrFromCode(char *code)
{
  SxmlNode *stateNode;
  static Buffer* bufState;

  stateNode=WicriAmeriqueGetUsaStateNodeFrFromCode(code);
  if (!stateNode)return NULL;
  if (!bufState) bufState=NewBuffer();
  BufferStrcpy(bufState, 
	       SxmlLeafText
	       (SxmlGetFirstChildByTagName(SxmlGetFirstChildByTagName(stateNode, "placeName"),"region")));
  SxmlFree(stateNode);
  return BufferString(bufState);
}
