#include <glib.h>

#include "../include/includes.h"
#include "../include/ramdata.h"
#include "../include/ppsm.h"
#include "../include/param.h"
#include "../include/tariff.h"



typedef struct ue_record
{
	char	used_flag;
	int	index;
	int   tb_index;
	long	long  msisdn;
}_ue_record;

typedef struct ocs_ue_index_table
{
	int  head_index;
	int  used_record;
	int  record_num;
	_ue_record	record[MAX_SUBS_RECORD];
}_ocs_ue_index_table;



static _ocs_ue_index_table		ue_table;

static GHashTable *htb_ue = NULL;  

/* ----------------------------------------------*/
int ocs_init_hash_table()
{

	if(htb_ue != NULL)
		g_hash_table_destroy(htb_ue);
    
	htb_ue = g_hash_table_new(g_direct_hash,g_direct_equal);
	
	return 1;	
}


int hash_add_ue_entity(_ue_record *ue_ptr)
{
	if(ue_ptr != NULL)
		g_hash_table_insert(htb_ue, GINT_TO_POINTER(ue_ptr->msisdn), ue_ptr);
    
	return 1;
}

int ocs_hash_clean_ue_entity(_ue_record *ue_ptr)
{
	if(ue_ptr != NULL)    
		g_hash_table_remove(htb_ue, GINT_TO_POINTER(ue_ptr->msisdn));
    
	return 1;
}

unsigned int ocs_get_ue_index(long long msisdn)
{
	unsigned int  index = INVALID_UE_INDEX;
	_ue_record *ptr=NULL;
  	
	ptr = (_ue_record *)g_hash_table_lookup(htb_ue, GINT_TO_POINTER(msisdn));
	if(ptr != NULL)
		index = ptr->index;
        
	return index;
}

unsigned int ocs_get_ue_tb_index(long long msisdn)
{
	unsigned int  index = INVALID_UE_INDEX;
	_ue_record *ptr=NULL;
  	
	ptr = (_ue_record *)g_hash_table_lookup(htb_ue, GINT_TO_POINTER(msisdn));
	if(ptr != NULL)
		index = ptr->tb_index;
        
	return index;
}

 _ue_record *ocs_get_ue_record(long long msisdn)
{
	unsigned int index = ocs_get_ue_index(msisdn);
	 _ue_record *ue_ptr=NULL;

	if(index != INVALID_UE_INDEX)
	{
		ue_ptr = &ue_table.record[index];
	}
	
	return ue_ptr;
}

int ocs_check_ue_record(long long msisdn)
{
	unsigned int  index = ocs_get_ue_index(msisdn);
	
	if(index != INVALID_UE_INDEX)
	{
		return 1;
	}
	
	return 0;

}

long long ocs_get_ue_by_index(int store_index)
{
	long long msisdn=0;
	_ue_record *ue_ptr;
	
	if(store_index != INVALID_UE_INDEX)
	{
		ue_ptr = &ue_table.record[store_index];
		msisdn = ue_ptr->msisdn;
	}

	
	return msisdn;
}

_ue_record *ocs_assign_ue_index(long long msisdn, int store_index)
{
	register int loop;
	int	index;
	_ue_record *ue_ptr;
	
	for(loop = 0; loop < MAX_SUBS_RECORD; loop++)
	{
		//index = ue_table.head_index++;
		index = loop;
		ue_table.head_index %= MAX_SUBS_RECORD;
		if(ue_table.record[index].used_flag == 0)
		{
			ue_table.record_num ++;
			
			ue_ptr = &ue_table.record[index];
			ue_ptr->used_flag = 1;
			ue_ptr->index = store_index;
			ue_ptr->msisdn = msisdn;
			ue_ptr->tb_index = loop;
			return ue_ptr;
		}
	}
	
	return NULL;
}

int ocs_delete_ue_record(long long msisdn)
{
	unsigned int  tb_index = ocs_get_ue_tb_index(msisdn);
	
	if(tb_index != INVALID_UE_INDEX)
	{
		ue_table.record_num --;
		ocs_hash_clean_ue_entity(&ue_table.record[tb_index]);
		memset(&ue_table.record[tb_index], 0x00, sizeof(_ue_record));
	
		return 1;
	}
	
	return 0;

}

int ocs_add_ue_record(long long msisdn, int store_index)
{
	unsigned int  index = ocs_get_ue_index(msisdn);
	_ue_record *ue_ptr = NULL;

	if(index != INVALID_UE_INDEX)
	{
		return 0;
	}

	ue_ptr = ocs_assign_ue_index(msisdn, store_index);
	if(ue_ptr == NULL)
		return 0;
	hash_add_ue_entity(ue_ptr);
	
	return 1;

}
/* ----------------------------------------------*/
