/****************************[ DELETE THIS WHEN NO LONGER NEEDED ]***************************
 *                                                                                          *
 * Installation instructions, you will first need to create a function that dumps your      *
 * Current social table out into a file, you can most likely just hack my save_social_table *
 * to do this.  Be sure to add a define for SOCIAL_FILE in your header file somewhere.      *
 * You will need to change the ch_output and maybe the logs to get it to compile correctly  *
 * since i take advantage of va_list stuff.  For the commands you can either rename them to *
 * fit your macro/naming convention needs, or simple call them via a wrapper.               *
 *                                                                                          *
 ********************************************************************************************/

/****************************************************************************
 * All materials contained within are copyrights of their respective        *
 * authors.                                                                 *
 * This includes, but is not limited to:                                    *
 *     Ryan Mangrum <Kale/Jalerit>                                          *
 *     Matt Goff <Kline>                                                    *
 ****************************************************************************/

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

/* include main header file */
#include "mud.h"

void save_social_table();
void load_social_table();
void init_social_table();
void resize_social_table( int amt);


/* init_social_table will do the initial memory allocation
    to the social table at bootup time.  It will first set
    the size of the main table, then allocate memory to each
    of the entries, this function should probably be a macro,
    but..i dont wanna
*/
void init_social_table()
{
        int i;

        social_table = (SOCIAL_DATA **)malloc( top_social * sizeof( SOCIAL_DATA *));
        for( i = 0; i < top_social; i++)
                social_table[i] = (SOCIAL_DATA *) malloc(sizeof(SOCIAL_DATA));
        return;
}

/* the amt parameter is the amt you want to add *NOT*
   what you want the new max entry number to be,
   This function should simply have to increment the size
   of top_social, reallocate the memory for the FULL table,
  then allocate memory for each of the new entries
*/
void resize_social_table( int amt)
{
        int i;
        int total = amt + top_social;

        social_table = (SOCIAL_DATA **)realloc(social_table, total * sizeof(SOCIAL_DATA *));

        for( i = top_social; i < (top_social + amt); i++)
                social_table[i] = (SOCIAL_DATA *) malloc(sizeof(SOCIAL_DATA));
        top_social = top_social + amt;
        return;
}

void load_socials()
{
        FILE *fp;
        char *word;
        bool done = FALSE;
        int control = -1; /* start off before 0 so it increments properly*/

        boot_log("Initializing social table...");
        init_social_table();
        if( (fp = fopen(SOCIAL_FILE, "r")) == NULL)
        {
                log("Could not open social_file for reading.");
                return; 
        }
        word = fread_word(fp);
        while( !done)
        {
                bool found = FALSE;
                
                switch( word[0])
                {
                        default:
                                break;
                        case 'C':
                        case 'c':
                                SREAD("Charnoarg", social_table[control]->char_no_arg);
                                SREAD("Charfound", social_table[control]->char_found);
                                SREAD("Charauto", social_table[control]->char_auto);
                        case 'E': 
                        case 'e':
                                if( compares( word, "End"))
                                {
                                        found = TRUE;
                                        done = TRUE;
                                        break;
                                }
                        case 'N':
                        case 'n':
                                WREAD("Name",social_table[control]->name);
                        case 'O':
                        case 'o':
                                SREAD("Othersnoarg", social_table[control]->others_no_arg);
                                SREAD("Othersfound", social_table[control]->others_found);
                                SREAD("Othersauto", social_table[control]->others_auto);
                        case 'S':
                        case 's':
                                if( compares( word, "SOCIAL"))
                                {
                                        control++;
                                        found = TRUE;
                                        if( control >= top_social)
                                                resize_social_table(1);
                                        break;
                                }
                        case 'V':
                        case 'v':
                                SREAD("Victfound", social_table[control]->vict_found);
                }
                if( !found)
                {
                 log("Load_socials: unexpected word: %s", word);
                 done = TRUE;
                }
                if( !done )
                 word = fread_word(fp);
        }
        fclose(fp);
        return;
}
                        
void save_social_table()
{
        FILE *fp;
        int i;
                                        
        if( (fp = fopen( SOCIAL_FILE, "w")) == NULL)
        {
                log("Unable to open social_file for writing.");
                return;
        }
        for( i = 0; social_table[i]->name[0] != '\0'; i++)
        {
                fprintf(fp, "COMMAND\n");
                fprintf(fp, "Name         %s\n", social_table[i]->name);
                fprintf(fp, "Charnoarg    %s~\n", social_table[i]->char_no_arg);
                fprintf(fp, "Charfound    %s~\n", social_table[i]->char_found);
                fprintf(fp, "Charauto     %s~\n", social_table[i]->char_auto);
                fprintf(fp, "Othersnoarg  %s~\n", social_table[i]->others_no_arg);
                fprintf(fp, "Victfound    %s~\n", social_table[i]->vict_found);
                fprintf(fp, "Othersfound  %s~\n", social_table[i]->others_found);
                fprintf(fp, "Othersauto   %s~\n", social_table[i]->others_auto);
        }
        fprintf(fp, "End\n");
        fclose(fp);
        return;
}
                        
void edit_social( D_MOBILE *ch, char *argument)
{
        char *arg1;
        char *arg2;
        int social;
                 
        argument = one_arg( argument, arg1);
        argument = one_arg( argument, arg2);
                 
        if( arg1[0] == '\0')
        {
                send_to_char("Edit which social.\n\r", ch);
                return;
        }               
        if( arg2[0] == '\0')
        {
                send_to_char("Edit what?\n\r", ch);
                return;
        }                               
        if( argument[0] == '\0')
        {
                send_to_char("How would you like to edit that?\n\r", ch);
                return;
        }
        if( (social = get_social( arg1)) == -1)
        {
                ch_output(ch, "Can't find social: %s.\n\r", arg1);
                return;
        }
        if( is_prefix( arg2, "name"))
        {
		free_string(social_table[social]->name);
                social_table[social]->name = strdup(argument);
                return;
        }
        else if( is_prefix( arg2, "charnoarg"))
        {
		free_string(social_table[social]->char_no_arg);
                social_table[social]->char_no_arg = strdup(argument);
                return;
        }
        else if( is_prefix( arg2, "charfound"))
        {
		free_string(social_table[social]->char_found);               
                social_table[social]->char_found = strdup(argument);
                return;
        }
        else if( is_prefix( arg2, "charauto"))
        {
		free_string(social_table[social]->char_auto);
                social_table[social]->char_auto = strdup(argument);
                return;
        }
        else if( is_prefix( arg2, "othersnoarg"))
        {
		free_string(social_table[social]->others_no_arg);
                social_table[social]->others_no_arg = strdup(argument);
                return;
        }
        else if( is_prefix( arg2, "othersfound"))
        {
		free_string(social_table[social]->others_found);
                social_table[social]->others_found = strdup(argument);
                return;
        }
        else if( is_prefix( arg2, "othersauto"))
        {
		free_string(social_table[social]->others_auto);
                social_table[social]->others_auto = strdup(argument);
                return;
        }
        else
        {
                send_to_char("Options are: name, charnoarg, charfound, charauto, othersnoarg, othersfound,\n\r", ch);
                send_to_char("and othersauto.\n\r", ch);
                return;
        }
        return;
}
                
int get_social(char *name)
{
        int ret_val = -1;
        int i;
                
        for( i = 0; i < top_social; i++)
        {
                if( is_prefix( name, social_table[i]->name))
                {       
                        ret_val = i;
                        break;
                }
        }
        return ret_val;
}
                
void display_social( D_MOBILE *ch, char *argument)
{
        char arg[MAX_BUFFER];
        int social;
                
        argument = one_arg(argument, arg);
        
        if( arg[0] == '\0')
        {
                send_to_char("Display which social?\n\r", ch);
                return;
        }
        if( (social = get_social(arg)) == -1)
        {
                ch_output(ch, "Could not find social: %s.\n\r", arg);
                return;
        }   
        ch_output( ch, "Name:        %s\n\r", social_table[social]->name);
        ch_output( ch, "Charnoarg:   %s\n\r", social_table[social]->char_no_arg);
        ch_output( ch, "Charfound:   %s\n\r", social_table[social]->char_found);
        ch_output( ch, "Charauto:    %s\n\r", social_table[social]->char_auto);
        ch_output( ch, "Othersnoarg: %s\n\r", social_table[social]->others_no_arg);
        ch_output( ch, "Victfound:   %s\n\r", social_table[social]->vict_found);
        ch_output( ch, "Othersfound: %s\n\r", social_table[social]->others_found);
        ch_output( ch, "Othersauto:  %s\n\r", social_table[social]->others_auto);
        return;
}
        
void save_socials( D_MOBILE *ch, char *argument)
{               
        save_social_table();
        send_to_char("Done.\n\r", ch);
        return;
}
                                
void create_social( D_MOBILE *ch, char *argument)
{
        char arg[MAX_BUFFER];

        argument = one_arg( argument, arg);
        
        if( arg[0] == '\0')
        {
                send_to_char("Create what social?\n\r", ch);
                return; 
        }
        if( get_social( arg) > -1)
        {
                send_to_char("That social already exists.\n\r", ch);
                return;
        }
        resize_social_table(1);
        social_table[top_social-1]->name = strdup(arg);
        log("New social created: %s.", arg);
        return;
}
         
void list_socials( D_MOBILE *ch, char *argument)
{
        int i;
        int count;
        char buf[MAX_BUFFER];   
        char buf2[MAX_BUFFER];
                
        buf[0] = '\0';
        buf2[0] = '\0';

        for( i = 0; i < top_social; i++)
        {   
                if( social_table[i] == NULL)
                {
                        log( "social_table out of bounds.");
                        return;
                }   
                sprintf(buf, "%-10s", social_table[i]->name);
                count++;
                if( count >= 5)
                {
                        strcat(buf, "\n\r");
                        count = 0;
                }
                strcat(buf2, buf);
        }
        send_to_char(buf2, ch);
        return;
}