#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <time.h>
#include <dirent.h>
#include <unistd.h>
/* include main header file */
#include "mud.h"

NOTE_DATA *note_first;

void startup_notes()
{
    log_string("Loading: Notes");
    load_notes();
}

/*
 * load_notes()
 *
 * Loads up the notes
 */
void load_notes()
{
    FILE *fp;
    NOTE_DATA *n;
    char *from;
    
    note_first = NULL;
    
    fp = fopen (NOTE_FILE, "r");
    
    /* No Notes, dont load! */
    if (!fp)
        return;

    from    = strdup(fread_word(fp));
    
    /* until end_marker is reached */
    while (!compares(from, END_MARKER))
    {
        {
        /* add new area */
        n = malloc(sizeof(NOTE_DATA));
        n->from     = strdup(from);
        n->number   = fread_number(fp);
        n->datestamp= fread_number(fp);
        n->to       = fread_string(fp);
        n->subject  = fread_string(fp);
        n->text     = fread_string(fp);
        n->next     = note_first;
        note_first  = n;
        
        log_string("DEBUG: Note added From: %s To: %s Subject: %s", n->from, n->to, n->subject);

        }
        from = strdup(fread_word(fp));
    }

    fclose (fp);
}

/*
 * save_notes()
 *
 * Runs through the list and saves
 * all valid notes to NOTE_FILE
 */
void save_notes()
{
    FILE *fp;
    NOTE_DATA *n;
    
    if (!note_first)
    {
        unlink (NOTE_FILE);
        return; 
    }
    
    fp = fopen (NOTE_FILE, "w");
    
    if (!fp)
    {
        bug ("Could not open " NOTE_FILE " for writing",0);
        return;
    }
    
    for (n = note_first; n ; n = n->next)
        fprintf (fp, "%s\n%d %ld\n%s\n%s\n%s\n", n->from, n->number, n->datestamp, n->to, n->subject, n->text);
        
    fprintf (fp, "%s\n",END_MARKER);
        
    fclose (fp);
}

/*
 * clear_note()
 *
 * Completely nulls out ch->in_progress
 */
/* NOT WORKING */
void clear_note (D_MOBILE * ch)
{
    ch->in_progress->from   = "Myself";
    ch->in_progress->to     = "no one";
    ch->in_progress->subject= "no subject";
    ch->in_progress->text   = "no body";
    ch->in_progress->number = -1;
    ch->in_progress->datestamp = -1;
}

void cmd_note (D_MOBILE * ch, char *arg)
{
    char buf[MAX_BUFFER];
    char arg1[MAX_BUFFER];
    
    arg = one_arg(arg, arg1);
    return;
    if (arg1[0] == '\0')
    {
        stc("Syntax is:\n\r", ch);
        stc(" note read                     - Reads next unread note\n\r", ch);
        stc(" note read <number>            - Read specified note number\n\r", ch);
        stc(" note to <who>                 - Sets who note is for (person/all/imm)\n\r", ch);
        stc(" note subject <subject>        - Sets subject\n\r", ch);
        stc(" note + <one line>             - adds one line\n\r", ch);
        stc(" note clear                    - Destroys all text in body of note\n\r", ch);
        stc(" note list                     - Shows list of all notes\n\r", ch);
        stc(" note show                     - Shows current note in progress\n\r", ch);
        return;
    }
    
    if (compares(arg1, "clear"))
    {
        clear_note(ch);
        stc("Note cleared.\n\r", ch);
        return;
    }
    
    if (compares(arg1, "show"))
    {
        sprintf(buf, "From:       %s\n\r", ch->name);
        stc(buf, ch);
        sprintf(buf, "To:         %s\n\r", ch->in_progress->to);
        stc(buf, ch);
        sprintf(buf, "Subject:    %s\n\r", ch->in_progress->subject);
        stc(buf, ch);
        sprintf(buf, "Text:\n%s\n\r",      ch->in_progress->text);
        stc(buf, ch);
        return;
    }
    
    /* Handle arg 'to' */
    if (compares(arg1, "to"))
    {
        ch->in_progress->to = strdup(arg);
        sprintf(buf, "Note to %s\n", ch->in_progress->to);
        stc(buf, ch);
        return;
    }
    /* Handle arg 'subject' */
    if (compares(arg1, "subject"))
    {
        ch->in_progress->subject = strdup(arg);
        sprintf(buf, "Subject %s\n", ch->in_progress->subject);
        stc(buf, ch);
        return;
    }
    /* Handle arg 'text' */
    if (compares(arg1, "text"))
    {
        ch->in_progress->text = strdup(arg);
        sprintf(buf, "text %s\n", ch->in_progress->text);
        stc(buf, ch);
        return;
    }

}