/*   -*- coding: utf-8 -*-  */
#include "MediaWiki.h"
#include "Explor.h"
#include "Buffer.h"
#include "SxmlNode.h"

char *ExplorTemplateName;
SxmlNode *ExplorTemplateDataTree;
char *ExplorTemplateCurrentStream;
char *ExplorTemplateCurrentStep;
char *ExplorTemplateCurrentIndex;

void ExplorGenerTemplateInitExplor()
{
  ExplorInit('v', NULL);
  ExplorAreaIsMulti=0;
  ExplorTemplateDataTree=SxmlClone(ExplorAreaData);
}

SxmlDocumentReader *ExplorGenerTemplateOpenReaderSort(char *stream, char *step, char *index)
{
    static Buffer *bufPath=NULL;
    if (!bufPath)bufPath=NewBuffer();
    if(!ExplorSortReader)ExplorSortReader=SxmlRecordReaderCreate();
  BufferStrcpy(bufPath,ExplorAreaDir);
  BufferStrcat(bufPath, "/Data/");
  BufferStrcat(bufPath, stream);
  BufferStrcat(bufPath, "/");
  BufferStrcat(bufPath, step);
  BufferStrcat(bufPath, "/");
  BufferStrcat(bufPath, index);
  BufferStrcat(bufPath, ".i.sort");
  SxmlRecordReaderOpen(ExplorSortReader, BufferString(bufPath));
  return ExplorSortReader;

}

void ExplorGenerTemplateInit(char *endName)
{
  static Buffer *bufPath=NULL;
  if (!bufPath)bufPath=NewBuffer();
  BufferStrcpy(bufPath, "Modèle:");
  BufferStrcat(bufPath, ExplorAreaCode);
  BufferStrcat(bufPath, ", ");
  BufferStrcat(bufPath, endName);
  ExplorTemplateName=strdup(BufferString(bufPath));
}


void ExplorGenerTemplateBeginInclude(int pageMode)
{
  if (pageMode==0) MediaWikiExportFileBegin();
  ExplorGenerTemplateInit("include");
  MediaWikiExportPageBegin(ExplorTemplateName, getenv("USER"), NULL);
  printf ("{{#switch:{{{section}}}");
}

void ExplorGenerTemplateEndInclude(int pageMode)
{
  printf("   }}</text></revision></page>\n");
  if (pageMode==0)   MediaWikiExportFileEnd();
}

void ExplorGenerTemplateGenerListSort(char *stream, char *step, char *index, StrDict *table, int length)
{
  SxmlNode *sortNode;
  static Buffer *bufKey=NULL;
  static Buffer *bufPrint=NULL;
  if (!bufKey)bufKey=NewBuffer();
  if (!bufPrint)bufPrint=NewBuffer();
  ExplorGenerTemplateOpenReaderSort(stream, step, index );
  int i;
  i=length;
  while((i--)&& (sortNode=SxmlRecordReaderNextDocumentElement(ExplorSortReader)))
    {
      SxmlNode *keyNode;
      SxmlNode *sizeNode;
      char *printStr;
      char *keyStr;
      char *dictPrintStr;

      keyNode=SxmlGetFirstChildByTagName(sortNode,"kw");
      keyStr=SxmlLeafText(keyNode);
      if(ExplorIncludeCategoryLang &&  (strcmp(index,"KwdFr")==0) &&  (strcmp(ExplorIncludeCategoryLang,"fr")==0))
	{
	  BufferStrcpy(bufPrint,"[[:Catégorie:");
	  BufferStrcat(bufPrint, keyStr);
	  BufferStrcat(bufPrint,"|");
	  BufferStrcat(bufPrint, keyStr);
	  BufferStrcat(bufPrint,"]]");
	}
      else
	{
	  BufferStrcpy(bufPrint,"[[");
	  BufferStrcat(bufPrint, keyStr);
	  BufferStrcat(bufPrint,"]]");
	}
      printStr=BufferString(bufPrint);
      if(table)
	{
	  BufferStrcpy(bufKey, index);
	  BufferStrcat(bufKey, ":");
	  BufferStrcat(bufKey, keyStr);
	  dictPrintStr=StrDictSearch(table, BufferString(bufKey));
	  if (dictPrintStr)
	    {
	      if (strncmp(dictPrintStr,"#r",2)==0)
		{
		  i++;
		  continue;
		}
	      printStr=dictPrintStr;
	    }
	}
      sizeNode=SxmlGetFirstChildByTagName(sortNode,"f");
      printf ("\n# %s ({{Explor lien| ", printStr);
      if (ExplorAreaWebSite)  printf ("|site =%s ", ExplorAreaWebSite);
      printf ("|wiki =%s ", ExplorWikiCode);
      printf ("|area =%s ", ExplorAreaCode);
      printf ("|flux =%s ", stream);
      printf ("|étape =%s ",step);
      if (strcmp(index,"AutAff")==0)
	{
	  printf ("|type =auteurAffiliation ");
	}
      else printf ("|type = indexItem|index=%s.i ", index);
      printf ("|clé =%s ", keyStr);
      printf ("|texte =%s" ,SxmlLeafText(sizeNode));
      printf (")}}");
    }
  SxmlRecordReaderClose(ExplorSortReader);
}
