#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "conv_prefix.h"

_conv_prefix	conv_prefix;

/*------------------------------------------------------------------*/

int	readConvPrefixConf()
{
	char	filename[]="./conf/conv_prefix.conf";
	FILE	*fpConf  = NULL;
	int i;
	
	fpConf = fopen(filename,"r");
	if(fpConf == NULL) return 0;
	fread(&conv_prefix,sizeof(_conv_prefix),1,fpConf);
	fclose(fpConf);

       for(i=0;i<MAX_CONV_PREFIX;i++)
        {
                if(strlen(conv_prefix.conv_prefix[i].prefix))
                        conv_prefix.conv_prefix[i].used_flag = 1;
                else
                        conv_prefix.conv_prefix[i].used_flag = 0;
                if(strlen(conv_prefix.group_conv_prefix[i].startNumber))
                        conv_prefix.group_conv_prefix[i].used_flag = 1;
                else
                        conv_prefix.group_conv_prefix[i].used_flag = 0;

        }
        for(i=0;i<MAX_CLI_PREFIX;i++)
        {
                if(strlen(conv_prefix.cli_prefix[i].prefix))
                        conv_prefix.cli_prefix[i].used_flag = 1;
                else
                        conv_prefix.cli_prefix[i].used_flag = 0;

        }

	return 1;
}

int xappSearchGroupTable(char *msisdn_unk)
{
	int loop=0;
	for(loop=0;loop<MAX_CONV_PREFIX;loop++)
	{
		if(conv_prefix.group_conv_prefix[loop].used_flag)
		{
		if((strcmp(msisdn_unk,conv_prefix.group_conv_prefix[loop].startNumber))>=0 && 
			(strcmp(conv_prefix.group_conv_prefix[loop].endNumber,msisdn_unk))>=0)
			return loop;
		}
	}
	return -1;
}

int msisdnUnkToInt(char *msisdn_unk, char *msisdn_int)
/* return value: 
	-1 = error length;
	 0 = cannn't find appropriate prefix;
	 1 = conv. succeed
*/
{
	int	retval = 0, index = -1;
	int	loop,len=0,max_len=0,old_len;
	int	delete_len,add_len;

  strcpy(msisdn_int,msisdn_unk);
	index = xappSearchGroupTable(msisdn_unk);
	if(index>=0)
	{
		char tmpStart[17],tmpEnd[17];
		
		strcpy(tmpStart,msisdn_unk);
		tmpStart[conv_prefix.group_conv_prefix[index].repStartDigit] = 0;
		
		strcpy(tmpEnd,&msisdn_unk[conv_prefix.group_conv_prefix[index].repEndDigit]);

		sprintf(msisdn_int,"%s%s%s",
				tmpStart,
				conv_prefix.group_conv_prefix[index].repDigitWith,
				tmpEnd);

		return 1;
	}
	for(loop=0;loop<MAX_CONV_PREFIX;loop++)
	{
		if(conv_prefix.conv_prefix[loop].used_flag)
		{
			len = strlen(conv_prefix.conv_prefix[loop].prefix);
			if(len>0)
			{
				if(len>max_len)
				if(strncasecmp(msisdn_unk,
					conv_prefix.conv_prefix[loop].prefix,
					len)==0)
				{
					max_len = len;
					index = loop;
				}//cmp
			}//len>0
		}//used
	}

	if(index>=0 && index<MAX_CONV_PREFIX)
	{
		len = strlen(msisdn_unk);

		if(len>24) len = 24;
		old_len = len;
		
	
		if(conv_prefix.conv_prefix[index].valid_length>0)
		{
			if(len != conv_prefix.conv_prefix[index].valid_length)
			{
				strcpy(msisdn_int,msisdn_unk);
				return -1;
			}
		}
		delete_len = conv_prefix.conv_prefix[index].delete_length;
		if(delete_len>len) 
		{
			strcpy(msisdn_int,msisdn_unk);
			return -1;
		}

		add_len	= conv_prefix.conv_prefix[index].add_length;
		if(add_len != strlen(conv_prefix.conv_prefix[index].add_digits))
		{
			strcpy(msisdn_int,msisdn_unk);
			 return -1;
		}
		strcpy(msisdn_int,
				conv_prefix.conv_prefix[index].add_digits);
		strcat(msisdn_int,
				&msisdn_unk[delete_len]);
		msisdn_int[add_len+old_len-delete_len] = 0;
		retval = 1;
	}
	return retval;
}

