#include <sqlite3.h>
#include <time.h>
#include <string.h>
#include "bbs.h"

extern struct bbs_config conf;
extern struct user_record *gUser;

int blog_load(struct blog_entry_t ***entries) {
    int blog_entry_count = 0;
    struct blog_entry_t **blog_entries;
    char *sql = "SELECT author, title, body, date FROM blog ORDER BY date DESC";

    char buffer[PATH_MAX];
    int rc;
    sqlite3 *db;
	sqlite3_stmt *res;

    snprintf(buffer, PATH_MAX, "%s/blog.sq3", conf.bbs_path);

    rc = sqlite3_open(buffer, &db);

	if (rc != SQLITE_OK) {
		dolog("Cannot open database: %s", sqlite3_errmsg(db));
        *entries = NULL;
		return 0;
    }
    sqlite3_busy_timeout(db, 5000);
    rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);

    if (rc != SQLITE_OK) {
	    dolog("Failed to execute statement: %s", sqlite3_errmsg(db));
	    sqlite3_close(db);
        *entries = NULL;
        return 0;
	}   
    while (sqlite3_step(res) == SQLITE_ROW) {
        if (blog_entry_count == 0) {
            blog_entries = (struct blog_entry_t **)malloc(sizeof(struct blog_entry_t *));
        } else {
            blog_entries = (struct blog_entry_t **)realloc(blog_entries, sizeof(struct blog_entry_t *) * (blog_entry_count + 1));
        }
        blog_entries[blog_entry_count] = (struct blog_entry_t *)malloc(sizeof(struct blog_entry_t));

        blog_entries[blog_entry_count]->author = strdup(sqlite3_column_text(res, 0));
        blog_entries[blog_entry_count]->subject = strdup(sqlite3_column_text(res, 1));
        blog_entries[blog_entry_count]->body = strdup(sqlite3_column_text(res, 2));
        blog_entries[blog_entry_count]->date = sqlite3_column_int(res, 3);
        blog_entry_count++;
    }

    sqlite3_finalize(res);
    sqlite3_close(db);

    *entries = blog_entries;
    return blog_entry_count;
}

void blog_display() {
        
    struct blog_entry_t **blog_entries;
    int blog_entry_count = 0;
    
    int i;
    struct tm thetime;
    char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???"};
    char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???"};
    char c;
    int hour;
    int j;
    int lines = 2;
    s_printf("\e[2J\e[1;1H");
    s_printf(get_string(280));
    s_printf(get_string(281));

    blog_entry_count = blog_load(&blog_entries);
    if (blog_entry_count == 0) {
        s_printf(get_string(282));
        s_printf(get_string(6));
        s_getchar();
        return;
    }

    c = 'y';

    for (i=0;i<blog_entry_count;i++) {
        localtime_r(&blog_entries[i]->date, &thetime);

        s_printf(get_string(283), blog_entries[i]->subject, blog_entries[i]->author);
        lines++;
        if (lines == 22 && tolower(c) != 'c') {
            s_printf("\r\n");
            s_printf(get_string(223));
            c = s_getchar();
            if (tolower(c) == 'n') {
                break;
            }
            s_printf("\r\n\r\n");
            lines = 0;
        }
        if (thetime.tm_hour >= 12) {
            hour = thetime.tm_hour - 12;
        } else {
            hour = thetime.tm_hour;
        }
        s_printf(get_string(284), (hour == 0 ? 12 : hour), thetime.tm_min, (thetime.tm_hour >= 12 ? "pm" : "am"), days[thetime.tm_wday], months[thetime.tm_mon], thetime.tm_mday, thetime.tm_year + 1900);

        lines++;
        if (lines == 22 && tolower(c) != 'c') {
            s_printf("\r\n");
            s_printf(get_string(223));
            c = s_getchar();
            if (tolower(c) == 'n') {
                break;
            }
            s_printf("\r\n\r\n");
            lines = 0;
        }
        
        s_printf("\r\n\e[0m");
        lines++;
        if (lines == 22 && tolower(c) != 'c') {
            s_printf("\r\n");
            s_printf(get_string(223));
            c = s_getchar();
            if (tolower(c) == 'n') {
                break;
            }
            s_printf("\r\n\r\n");
            lines = 0;
        }        
        for (j=0; j<strlen(blog_entries[i]->body); j++) {
            if (blog_entries[i]->body[j] == '\r') {
                s_printf("\r\n");
                lines++;
                if (lines == 22 && tolower(c) != 'c') {
                    s_printf("\r\n");
                    s_printf(get_string(223));
                    c = s_getchar();
                    if (tolower(c) == 'n') {
                        break;
                    }
                    s_printf("\r\n\r\n");
                    lines = 0;
                }                
            } else {
                s_printf("%c", blog_entries[i]->body[j]);
            }
        }

        if (tolower(c) == 'n') {
            break;
        }
        s_printf("\r\n");
        lines++;
        if (lines == 22 && tolower(c) != 'c') {
            s_printf("\r\n");
            s_printf(get_string(223));
            c = s_getchar();
            if (tolower(c) == 'n') {
                break;
            }
            s_printf("\r\n\r\n");
            lines = 0;
        }
    }
    for (i=0;i<blog_entry_count;i++) {
        free(blog_entries[i]->subject);
        free(blog_entries[i]->author);
        free(blog_entries[i]->body);
        free(blog_entries[i]);
    }

    free(blog_entries);

    s_printf(get_string(6));
    s_getchar();

}

void blog_write() {
   	char *csql = "CREATE TABLE IF NOT EXISTS blog ("
    					"id INTEGER PRIMARY KEY,"
						"author TEXT COLLATE NOCASE,"
						"title TEXT,"
						"body TEXT,"
                        "date INTEGER);";

	char *isql = "INSERT INTO blog (author, title, body, date) VALUES(?, ?, ?, ?)";
    int rc;
    sqlite3 *db;
	sqlite3_stmt *res;
    char *blog_entry;
    char buffer[PATH_MAX];
    char *blog_subject;
    char *err_msg = 0;

    s_printf(get_string(285));
    s_readstring(buffer, 64);
    s_printf("\r\n");

    if (strlen(buffer) == 0) {
        s_printf(get_string(39));
        return;
    }

    blog_subject = strdup(buffer);

    blog_entry = external_editor(gUser, "No-One", "No-One", NULL, 0, "No-One", "Blog Editor", 0, 1);
	
    if (blog_entry != NULL) {
        snprintf(buffer, PATH_MAX, "%s/blog.sq3", conf.bbs_path);
        rc = sqlite3_open(buffer, &db);

	    if (rc != SQLITE_OK) {
		    dolog("Cannot open database: %s", sqlite3_errmsg(db));
            free(blog_entry);
            free(blog_subject);
		    return;
        }
        sqlite3_busy_timeout(db, 5000);
	    rc = sqlite3_exec(db, csql, 0, 0, &err_msg);
	    if (rc != SQLITE_OK ) {
		    dolog("SQL error: %s", err_msg);
		    sqlite3_free(err_msg);
		    sqlite3_close(db);
            free(blog_entry);
            free(blog_subject);            
		    return;
	    }

	    rc = sqlite3_prepare_v2(db, isql, -1, &res, 0);

	    if (rc == SQLITE_OK) {
		    sqlite3_bind_text(res, 1, gUser->loginname, -1, 0);
		    sqlite3_bind_text(res, 2, blog_subject, -1, 0);
		    sqlite3_bind_text(res, 3, blog_entry, -1, 0);
		    sqlite3_bind_int(res, 4, time(NULL));
	    } else {
		    dolog("Failed to execute statement: %s", sqlite3_errmsg(db));
		    sqlite3_finalize(res);
		    sqlite3_close(db);
            free(blog_entry);
            free(blog_subject);             
		    return;
	    }
	    sqlite3_step(res);

	    sqlite3_finalize(res);
	    sqlite3_close(db);
        free(blog_entry);
        free(blog_subject); 
        return;
	}
    free(blog_subject);
}