// standard headers
#if defined(GSDL_USE_STL_H)
#include <iostream.h>
#include <fstream.h>
#include <vector.h>
#else
#include <iostream>
#include <fstream>
#include <vector>
#endif

// protocol headers
#include "corbaiface.h"
using namespace gsdlInterface;

// greenstone headers
#include "corbaconv_text_t.h"

//local headers
#include "corbatext_t.h"

void corbatext_fillChar(corbatext_t *t, char *text)
{
  unsigned int i;

  t->text.length(0);
  if (text == NULL)
      {
	  return;
      }
  for (i = 0; i < strlen(text); i ++)
      {
	  t->text[i] = text[i];
      }  
  t->encoding = 0;
}

void corbatext_usvector(corbatext_t t, usvector& us)
{
  unsigned int i;

  for (i = 0; i < t.text.length(); i ++)
    {
      us.push_back(t.text[i]);
    }
}

char *corbatext_string(corbatext_t t)
{ 
  char *lcopy;
  unsigned int i;
    lcopy = (char *) malloc(t.text.length() + 1);
    for (i = 0; i < t.text.length(); i ++)
    {
      lcopy[i] = (char) t.text[i];
    }
    lcopy[i] = '\0';
    return lcopy;
}

corbatext_t_var corbatext_corbatext(text_t t)
{ corbatext_t_var ct;
// usString_var    us;

  // cout << "Making text\n";
  ct = new corbatext_t;
  //  ct->text.length(20);
  // us = new usString(20);
  //  cout << ct->text.length() << endl;
  //  cout << ct->text.maximum() << endl;
  corbaconv_text_t::fillUsString(t,&ct->text);
  //t.fillUsString(us);
  // delete us;
  // cout << "Encoding text\n";
  ct->encoding = 0;
  // cout << "Encoding text\n";
  return ct;
}

int corbatext_corbatext(text_t t, corbatext_t_var ct)
{
  cout << "A" << endl;
  corbaconv_text_t::fillUsString(t,&ct->text);
  cout << "B" << endl;
  ct->encoding = 0;
  cout << "C" << endl;

  return 0;
}

void corbatext_corbaArrayToArray(corbatext_tarray ca, text_tarray *ta)
{
  unsigned int i;
  corbaconv_text_t *cct;

  ta->clear();
  ta->reserve(ca.length());
  for (i = 0; i < ca.length(); i ++)
    {
      cct = new corbaconv_text_t(ca[i]);
      ta->push_back(*cct); // cast to text_t
    }  
}

void corbatext_arrayToCorbaArray(text_tarray ta, corbatext_tarray *_ca)
{
  text_tarray::iterator here = ta.begin();
  text_tarray::iterator end = ta.end();
  corbatext_tarray ca(end - here);
  unsigned int i;

  i = 0;
  (*_ca).length(end - here);

  while (here != end)
    {
      corbatext_t_var ptr;
      //      cout << "Array: " << (*_ca).maximum() << " " << (*_ca).length() << endl;
      ptr = corbatext_corbatext(*here);
      (*_ca)[i] = ptr;
      here ++;
      i++;      
    }
}

void corbatext_corbaArrayToSet(corbatext_tarray t, text_tset *ts)
{ unsigned int i;
 
  for (i = 0; i < t.length(); i ++)
    {
      ts->insert((corbaconv_text_t) t[i]); // further typecast to text_t
    } 

  return;
}

void corbatext_setToCorbaArray(text_tset ts, corbatext_tarray *t)
{
  corbatext_t str;
  text_tset::iterator here = ts.begin();
  text_tset::iterator end = ts.end();
  unsigned int i;

  i = 0;
  while (here != end)
    {
      (*t).length(i+1);
      (*t)[i] = *corbatext_corbatext(*here);
      here ++;
      i ++;
    }
}

void corbatext_mapToCorbaMap(text_tmap map, corbatext_tmap *cmap)
{
  text_tmap::iterator here = map.begin();
  text_tmap::iterator end  = map.end();
  unsigned int i = 0;

  while (here != end)
    {
      (*cmap).names.length(i+1);
      (*cmap).values.length(i+1);
      cmap->names[i] = *corbatext_corbatext((*here).first);
      cmap->values[i] = *corbatext_corbatext((*here).second);
      here ++;
      i ++;
    }
}

void corbatext_corbaMapToMap(corbatext_tmap cmap, text_tmap &map)
{
  unsigned int i;

  for (i = 0; i < cmap.names.length(); i ++)
    {
      map.insert(make_pair(*(new corbaconv_text_t(cmap.names[i])), 
                           *(new corbaconv_text_t(cmap.values[i])))); // further typecast to text_t
    }
}


// map of metanames to array(lang, value). So each metaname[i] maps to array (lang, value).
// Note that corbacollectionmeta_map is an array of pairs
// each pair consists of (string, map)
void corbatext_colmetamapToCorbaColmetamap(collectionmeta_map cm, corbatext_tcollectionmeta_map *ccm)
{
  collectionmeta_map::iterator here = cm.begin();
  collectionmeta_map::iterator end  = cm.end();
  unsigned int i = 0;

  while (here != end)
    {
      ccm->length(i+1); // increase size of array of (name, tuples) pairs by 1 each time

      // fill in the metaname field
      // metaname is of type text_t, get the corbatext value for the text_t key
      (*ccm)[i].name = *corbatext_corbatext((*here).first); // metaname is of type text_t

      // fill in the tuples field
      corbatext_mapToCorbaMap((*here).second, &((*ccm)[i].tuples));

      here ++;
      i ++;
    }

}

void corbatext_corbaColmetamapToColmetamap(corbatext_tcollectionmeta_map ccm, collectionmeta_map &cm)
{
  unsigned int i;

  for (i = 0; i < ccm.length(); i ++) 
    {
    // ccm is an array
    // where each element is a pair of (name, tuples)

    // 1. get the (corba_text_t) name and convert it to text_t
    text_t* meta_name = new corbaconv_text_t(ccm[i].name);

    corbatext_tmap corbameta_tuples = ccm[i].tuples;

    text_tmap *tuples_map = new text_tmap();
    corbatext_corbaMapToMap(ccm[i].tuples, *tuples_map);


    cm.insert(make_pair(*meta_name,
    			*tuples_map));			
    }
}

