#include <andrewos.h>
#include <class.h>
#include <sys/param.h>
#include <msgfolder.ih>
#include <extlist.ih>
#include <fldlist.ih>
#include <attrvalst.ih>
#include <msgdb.ih>
#include <msgdbcol.eh>

#define BUFFSIZE 1000

#ifdef AFS_ENV
char *defaultDataBaseType = "ams";
#else
char *defaultDataBaseType = "mh";
#endif /* AFS_ENV */

boolean msgdatabasecollection__InitializeObject(classid, self)
struct classheader *classid;
struct msgdatabasecollection *self;
{
    self->databases = extendedlist_New();

    return TRUE;
}

void msgdatabasecollection__FinalizeObject(classid, self)
struct classheader *classid;
struct msgdatabasecollection *self;
{

}

static void ProcessSearchPath(self, sp)
struct msgdatabasecollection *self;
struct fieldlist *sp;
{
    char *entry;
    long spInd;
    long spCount = fieldlist_NumberOfElements(sp);
    char *pathname;
    char *type;
    struct msgdatabase *db;

    for (spInd = 0; spInd < spCount; spInd++) {
	struct attributevalueset *attrValSet;

	entry = fieldlist_GetNth(sp, spInd);

	attrValSet = attributevalueset_New();

	if (*entry == '{') {
	    entry = attributevalueset_ParseString(attrValSet, entry, '{', '}');
	}

	if (! attributevalueset_Lookup(attrValSet, "type", &type)) {
	    attributevalueset_Insert(attrValSet, "type", defaultDataBaseType);
	    type = defaultDataBaseType;
	}

	if (! attributevalueset_Lookup(attrValSet, "name", NULL)) {
	    attributevalueset_Insert(attrValSet, "name", type);
	}

	while (*entry != '\0' && isspace(*entry)) {
	    entry++;
	}

	pathname = (char *) malloc(strlen(entry) + 1);
	strcpy(pathname, entry);

	attributevalueset_Insert(attrValSet, "path", pathname);

	/*
	 Will create the appropriate type of database object
	 at this point.  Right now we will just print out the
	 information that we read in to see if this has worked.
	 Under normal circumstances we will pass in the attribute
	 list to the new database and then destroy it upon return.
	*/

	if (type != NULL) {
	    char className[1000];

	    sprintf(className, "%sdatabase", type);

	    db = (struct msgdatabase *) class_NewObject(className);

	    if (db != NULL) {
		msgdatabase_SetDatabaseCollection(db, self);
		msgdatabase_SetAttributes(db, attrValSet);
		msgdatabase_SetPriority(db, extendedlist_NumberOfElements(self->databases));

		extendedlist_AddElement(self->databases, db);
	    }
	}

	attributevalueset_Destroy(attrValSet);
    }
}

struct msgdatabasecollection *msgdatabasecollection__Create(classid, path)
struct classheader *classid;
char *path;
{
    struct msgdatabasecollection *mdbc;

    mdbc = msgdatabasecollection_New();

    if (mdbc != NULL) {
	struct fieldlist *sp;
	long count;

	sp = fieldlist_Create(path, ":", '\0', '\0', NULL);

	count = fieldlist_NumberOfElements(sp);

	extendedlist_SetSize(mdbc->databases, count);

	ProcessSearchPath(mdbc, sp);

	fieldlist_Destroy(sp);
    }

    return mdbc;
}
    
static boolean IsThereNewMail(database, rock)
struct msgdatabase *database;
void *rock;
{
    return msgdatabase_IsThereNewMail(database);
}

boolean msgdatabasecollection__IsThereNewMail(self)
struct msgdatabasecollection *self;
{
    return extendedlist_ForEach(self->databases, IsThereNewMail, NULL) >= 0;
}

static boolean CountOfNewMail(database, count)
struct msgdatabase *database;
long *count;
{
    *count += msgdatabase_CountOfNewMail(database);
}

long msgdatabasecollection__CountOfNewMail(self)
struct msgdatabasecollection *self;
{
    long count = 0;

    extendedlist_ForEach(self->databases, CountOfNewMail, &count);
    return count;
}

struct processinfo {
    long good;
    long bad;
    long locked;
    long error;
    boolean errorFound;
    char *buffer;
    long bufferSize;
};

static boolean ProcessNewMail(database, info)
struct msgdatabase *database;
struct processinfo *info;
{
    long good;
    long bad;
    long locked;
    long error;
    char buffer[BUFFSIZE];

    if (msgdatabase_ProcessNewMail(database, &good, &bad, &locked, &error, buffer, BUFFSIZE) != 0) {
	if (! info->errorFound) {
	    long copySize;

	    info->errorFound = TRUE;
	    info->error = error;
	    copySize = (BUFFSIZE > info->bufferSize) ? info->bufferSize : BUFFSIZE;
	    /* TODO: Boundary condition in strncpy */
	    strncpy(info->buffer, buffer, copySize);
	}
	info->good += good;
	info->bad += bad;
	info->locked += locked;
    }
    return FALSE;
}

boolean msgdatabasecollection__ProcessNewMail(self, numGood, numBad, numLocked, firstError, errorBuf, bufferSize)
struct msgdatabasecollection *self;
long *numGood;
long *numBad;
long *numLocked;
long *firstError;
char *errorBuf;
long bufferSize;
{
    struct processinfo info;
    
    info.good = 0;
    info.bad = 0;
    info.locked = 0;
    info.errorFound = FALSE;
    info.error = 0;
    info.buffer = errorBuf;
    info.bufferSize = bufferSize;

    extendedlist_ForEach(self->databases, ProcessNewMail, &info);

    if (numGood != NULL) {
	*numGood = info.good;
    }
    if (numBad != NULL) {
	*numBad = info.bad;
    }
    if (numLocked != NULL) {
	*numLocked = info.locked;
    }
    if (firstError != NULL) {
	*firstError = info.error;
    }
    return info.errorFound;
}

struct getfolderinfo {
    char *dbName;
    char *name;
    struct msgfolder *folder;
};

static boolean GetFolder(database, info)
struct msgdatabase *database;
struct getfolderinfo *info;
{
    if (info->dbName == NULL || strcmp(msgdatabase_GetName(database), info->dbName) == 0) {
	if ((info->folder = msgdatabase_GetFolder(database, info->name)) != NULL) {
	    return TRUE;
	}
    }
    return FALSE;
}

struct msgfolder *msgdatabasecollection__GetFolder(self, name)
struct msgdatabasecollection *self;
char *name;
{
    char *dbend;
    char databaseName[MAXPATHLEN];
    struct getfolderinfo info;

    info.folder = NULL;
    dbend = index(name, '#');
    if (dbend != NULL) {
	strncpy(databaseName, name, dbend - name);
	databaseName[dbend-name] = '\0';
	info.dbName = databaseName;
	info.name = dbend + 1;
    }
    else {
	info.dbName = NULL;
	info.name = name;
    }

    extendedlist_ForEach(self->databases, GetFolder, &info);
    return info.folder;
}
    
struct listfoldersinfo {
    char *pattern;
    boolean (*testProc)();
    void *testRock;
    struct extendedlist *fullList;
    struct extendedlist *(*listProc)();
};

static boolean ListFolders(database, info)
struct msgdatabase *database;
struct listfoldersinfo *info;
{
    struct extendedlist *dbFolderList;

    dbFolderList = (*info->listProc)(database, info);
    extendedlist_Merge(info->fullList, dbFolderList);
    extendedlist_Destroy(dbFolderList);
    return FALSE;
}

static struct extendedlist *ListOfAllFolders(database, info)
struct msgdatabase *database;
struct listfoldersinfo *info;
{
    return msgdatabase_GetListOfAllFolders(database, info->pattern);
}

struct extendedlist *msgdatabasecollection__GetListOfAllFolders(self, pattern)
struct msgdatabasecollection *self;
char *pattern;
{
    struct listfoldersinfo info;

    info.fullList = extendedlist_New();
    info.pattern = pattern;
    info.listProc = ListOfAllFolders;
    
    extendedlist_ForEach(self->databases, ListFolders, &info);
    return info.fullList;
}

static struct extendedlist *ListOfFolders(database, info)
struct msgdatabase *database;
struct listfoldersinfo *info;
{
    return msgdatabase_GetListOfFolders(database, info->testProc, info->testRock, info->pattern);
}

struct extendedlist *msgdatabasecollection__GetListOfFolders(self, testProc, rock, pattern)
struct msgdatabasecollection *self;
procedure testProc;
void *rock;
char *pattern;
{
    struct listfoldersinfo info;

    info.fullList = extendedlist_New();
    info.pattern = pattern;
    info.listProc = ListOfAllFolders;
    info.testProc = testProc;
    info.testRock = rock;

    extendedlist_ForEach(self->databases, ListOfFolders, &info);
    return info.fullList;
}

static struct extendedlist *ListOfSubscribedFolders(database, info)
struct msgdatabase *database;
struct listfoldersinfo *info;
{
    return msgdatabase_GetListOfSubscribedFolders(database, info->pattern);
}

struct extendedlist *msgdatabasecollection__GetListOfSubscribedFolders(self, pattern)
struct msgdatabasecollection *self;
char *pattern;
{
    struct listfoldersinfo info;

    info.fullList = extendedlist_New();
    info.pattern = pattern;
    info.listProc = ListOfSubscribedFolders;
    
    extendedlist_ForEach(self->databases, ListFolders, &info);
    return info.fullList;
}

static struct extendedlist *ListOfPersonalFolders(database, info)
struct msgdatabase *database;
struct listfoldersinfo *info;
{
    return msgdatabase_GetListOfPersonalFolders(database, info->pattern);
}

struct extendedlist *msgdatabasecollection__GetListOfPersonalFolders(self, pattern)
struct msgdatabasecollection *self;
char *pattern;
{
    struct listfoldersinfo info;

    info.fullList = extendedlist_New();
    info.pattern = pattern;
    info.listProc = ListOfPersonalFolders;
    
    extendedlist_ForEach(self->databases, ListFolders, &info);
    return info.fullList;
}

static boolean SaveProfile(database, rock)
struct msgdatabase *database;
void *rock;
{
    return msgdatabase_SaveProfile(database);
}

boolean msgdatabasecollection__UpdateState(self)
struct msgdatabasecollection *self;
{
    return extendedlist_ForEach(self->databases, SaveProfile, NULL) >= 0;
}

