#include "abk.h"
#include "lprintf.h"
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define DMID 14
#include "debug.h"

#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE 1
#endif

/*
typedef struct abk {
  char *nick;
  Pair *data;
} Abk;
*/

/*
[Test3]
abk_email test3@ally
abk_full_name Test User III
abk_can_view_my_calendar 
abk_can_edit_my_calendar 

[Test]
abk_address This is the postal address\nit has 3 lines\nthis is the 3rd line\n
abk_full_name Test User
abk_email test@ally
abk_phone 123 456
abk_mobile 021 123 456
abk_fax 0 123 456
abk_note This is a note\nit has 2 lines
*/

int abk_cmp(Abk **p1, Abk **p2)
{
	Abk *a1 = *p1;
	Abk *a2 = *p2;
	return stricmp(a1->nick,a2->nick);
}

void abk_free(Abk **a)
{
	if (!a || !*a) return ;
	pair_free(&(*a)->data);
	free((*a)->nick);
	free(*a);
}

int loadAbk(Array **abk, char *file, int skip_misc, char *err, int bfsz)
{
	char tmp[BFSZ];
	char bf[BFSZ];
	Abk *a = NULL;
	FILE *f;
	char *s;
	char *e;

	f = fopen(file,"r");
	if (!f) {
		ncpy(err,strerror(errno),bfsz-1);
		return FALSE;
	}

	if (!*abk) {
		*abk = array_new(100);
		array_setup(*abk,(int(*)(void**,void**))abk_cmp,(void(*)(void**))abk_free);
	}

	while(fgets(bf,BFSZ,f)) {
		if (bf[0] == '[') {
			if (a) array_add(*abk,a);
			a = calloc(1,sizeof(Abk));

			s = &bf[1];
			lib_copy_upto(&s,']',tmp,BFSZ);
			a->nick = strdup(tmp);
		}
		else if (a) {
			for(s = bf; *s && !sisspace(*s); s++) {}
			if (*s) {
				for(*s++ = 0; sisspace(*s); s++) {}
				for(e = s; *e; e++) {}
				for(; e >= s && iseol(*e); e--) *e = 0;
			}
			if (strcmpnc(bf,"abk_email")==0) {
				a->email = strdup(s);
				continue;
			}
			if (strcmpnc(bf,"abk_instant")==0 || strcmpnc(bf,"abk_nused")==0 || strcmpnc(bf,"abk_lastused")==0){
				pair_add(&a->data,bf,s);
				continue;
			}
			if (skip_misc) continue;
			pair_add(&a->data,bf,s);
		}		
	}
	fclose(f);
	if (a) array_add(*abk,a);
	return TRUE;
}

int saveAbk(Array *abk, char *file, char *err, int bfsz)
{
	char bf[BFSZ];
	Pair *p;
	FILE *f;
	Abk *a;	
	int i;

	lprintf(bf,BFSZ,"%s.tmp",file);
	f = fopen(bf,"w");
	if (!f) {
		ncpy(err,strerror(errno),bfsz-1);
		return FALSE;
	}

	for(i = 0; (a = (Abk*)array_get(abk,i)); i++) {
		fprintf(f,"[%s]\r\n",a->nick);
		fprintf(f,"abk_email %s\r\n",a->email);
		for(p = a->data; p; p = p->next)
			fprintf(f,"%s %s\r\n",p->var,p->val);
		fprintf(f,"\r\n");
	}

	fclose(f);

	return remove_rename(file,bf,err,bfsz);
}

int abk_shortlist_sort_cb(const void *arg1, const void *arg2)
{
	return (*(Abk**)arg2)->nused - (*(Abk**)arg1)->nused;
}

int abk_shortlist_init(Array *abk)
{
	int i,total,top;
	Abk *a, **tmp;

	for(i = 0; (a = (Abk*)array_get(abk,i)); i++) {
		if (a->type!=ABK_NONE){
			return TRUE;			// This has already been done, no need to do so again
		}

		// For now same shortlist algorithm as surgeplus uses (see src\surgeplus\aContacts.cpp)
		a->nused = atoi(pair_find(a->data,"abk_instant"))*1000 + atoi(pair_find(a->data,"abk_nused"));
	}
	total=i;

	tmp=calloc(total,sizeof(Abk*));
	if (!tmp) return FALSE;

	for(i = 0; (a = (Abk*)array_get(abk,i)); i++) {
		tmp[i] = a;
	}
	qsort(tmp,total,sizeof(Abk*),abk_shortlist_sort_cb);

	top=20;
	for(i = 0; i<total; i++) {
		// Now find the top 20
		if (top<=0){
			tmp[i]->type=ABK_ALL;
			continue;
		}			
		// make sure we have mailed someone several times before adding to short list
		if (tmp[i]->nused<4){
			tmp[i]->type=ABK_ALL;
			continue;
		}			
			
		tmp[i]->type=ABK_SHORT;
		top--;
	}

	free(tmp);
	return TRUE;
}


