// Change History
// 11/20/03 AV Development: BC6 conversion. Cons/none const

#include "drs/drs_appl.h"
#include "drs/drs_indexlist.h"
#include "drs/drs_srch.h"

DRS_IndexList::DRS_IndexList( DRS_Database const &rDB, AT_String sFile, AT_String sSection, DRS_Setting BaseKey)
  : rDatabase( rDB)
{
  DRS_Setting key = BaseKey;
  key += "IdxFile";
  iIndexFileID = GetPrivateProfileIntA( sSection, (LPCSTR) *key, -2, sFile);
  if( iIndexFileID == -2) throw AT_Corrupt_Err( *this, *key);

  key = BaseKey + "IdxIDBase";
  iIndexIDBase = GetPrivateProfileIntA( sSection, (LPCSTR) *key, MAX_INT, sFile);
  if( iIndexIDBase == MAX_INT) throw AT_Corrupt_Err( *this, *key);
  //Neg for link to other search field. Remapped on request.
}

int DRS_IndexList::IndexIDBase() const
{
  //Neg for -1 based link to other search field. Remapped on request.
  if( iIndexIDBase < 0)
    {
    DRS_IndexList const &rLink = rDatabase.SearchField( abs( iIndexIDBase) - 1).IndexList();
    return rLink.IndexIDBase() + rLink.IndexCount();
    }
  return iIndexIDBase;
}

DRS_IndexListEnumerated::DRS_IndexListEnumerated( DRS_Database const &rDB, AT_String sFile, AT_String sSection, DRS_Setting BaseKey)
      : DRS_IndexList( rDB, sFile, sSection, BaseKey), iIndexCount(0)
{

  DRS_Setting key = BaseKey;
  key += "IdxCount";
  iIndexCount = GetPrivateProfileIntA( sSection, (LPCSTR) *key, 0, sFile);
  if( !iIndexCount) throw AT_Corrupt_Err( *this, *key);

  aaIndexDefs = new auto_ptr< DRS_IndexDef>[iIndexCount];
  for( uint i = 0; i < iIndexCount; ++i)
    {
    key = BaseKey;
    key += "Idx";
    key +=  i;
    key += "Type";
    uint iIndexType = GetPrivateProfileIntA( sSection, (LPCSTR) *key, MAX_UINT, sFile);
    switch( iIndexType)
      {
      case INDEXTYPE_TEXT:
        key = BaseKey;
        key += "Idx";
        key +=  i;
        aaIndexDefs[i].reset( new DRS_IndexDefText( sFile, sSection, key));
        break;
      case INDEXTYPE_INTEGER:
        key = BaseKey;
        key += "Idx";
        key +=  i;
        aaIndexDefs[i].reset( new DRS_IndexDefInteger( sFile, sSection, key));
        break;
      default:
        throw AT_Corrupt_Err( *this, *key);
      }
    }
}

DRS_Index * DRS_IndexListEnumerated::IndexOpen( uint iIndex)
  const
{
  AT_Assert( (iIndex < iIndexCount), *this, "IndexOpen");
  return  aaIndexDefs[iIndex].get()->IndexOpen( (DRS_Database &)rDatabase,
    iIndexFileID, IndexIDBase() + iIndex);
}

/*
DRS_IndexListMapped::DRS_IndexListMapped( DRS_Database const &rDB, AT_String sFile, AT_String sSection, DRS_Setting BaseKey)
      : DRS_IndexList( rDB, sFile, sSection, BaseKey), pFieldMap(0)
{

  DRS_Setting key = BaseKey;
  key += "IdxFieldMap";
  iFieldMapID = GetPrivateProfileIntA( sSection, (LPCSTR) *key, MAX_UINT, sFile);
  if( iFieldMapID == MAX_UINT) throw AT_Corrupt_Err( *this, *key);

  //Get field map from database
  try
  {
  pFieldMap = &(rDatabase.FieldMap(iFieldMapID));
  }
  catch( AT_Error &err)
  {
    throw AT_Corrupt_Err( *this, *key); //Link to prev error!!!!!!
  }

  key = BaseKey + "Idx0Type";
  uint iIndexType = GetPrivateProfileIntA( sSection, (LPCSTR) *key, MAX_UINT, sFile);
  switch( iIndexType)
    {
    case INDEXTYPE_TEXT:
      key = BaseKey + "Idx0";
      apIndexDef.reset( new DRS_IndexDefText( sFile, sSection, key));
      break;
    case INDEXTYPE_INTEGER:
      key = BaseKey + "Idx0";
      apIndexDef.reset( new DRS_IndexDefInteger( sFile, sSection, key));
      break;
    default:
      throw AT_Corrupt_Err( *this, *key);
    }
}

DRS_Index * DRS_IndexListMapped::IndexOpen( uint iIndex)
  const
{
  AT_Assert( (iIndex < pFieldMap->Size()), *this, "IndexOpen");
  return  apIndexDef.get()->IndexOpen( (DRS_Database &)rDatabase, iIndexFileID,
    IndexIDBase() + iIndex);
}
*/
