


#include <stdio.h>
#include <glob.h>
#include <string.h>
#include <stdbool.h>

#include "../mecha/mecha.h"
#include "../modtcl/modtcl.h"

#include "m3.h"

#include "m3_tcl.h"

static int m3_tcl_get_loser (ClientData ignored, Tcl_Interp *interp, int argc, const char **argv);
static int m3_tcl_get_displayname (ClientData ignored, Tcl_Interp *interp, int argc, const char **argv);
static int m3_tcl_skip_track (ClientData ignored, Tcl_Interp *interp, int argc, const char **argv);
static int m3_tcl_get_users (ClientData ignored, Tcl_Interp *interp, int argc, const char **argv);
static int m3_tcl_add_track (ClientData ignored, Tcl_Interp *interp, int argc, const char **argv);
static int m3_tcl_reload_playlist (ClientData ignored, Tcl_Interp *interp, int argc, const char **argv);

void
m3_tcl_init()
{
    tclFbind_dyn_command ((char *) "m3_get_displayname", &m3_tcl_get_displayname);
    tclFbind_dyn_command ((char *) "m3_skip_track", &m3_tcl_skip_track);
    tclFbind_dyn_command ((char *) "m3_get_users", &m3_tcl_get_users);
    tclFbind_dyn_command ((char *) "m3_get_loser", &m3_tcl_get_loser);
    tclFbind_dyn_command ((char *) "m3_add_track", &m3_tcl_add_track);
    tclFbind_dyn_command ((char *) "m3_reload_playlist", &m3_tcl_reload_playlist);
}

static int
m3_tcl_get_loser (MECHA_UNUSED_ARGUMENT ClientData ignored, Tcl_Interp *interp, 
        MECHA_UNUSED_ARGUMENT int argc, MECHA_UNUSED_ARGUMENT const char **argv)
{
    httpTtrans *trans = httpFthread_gettrans();

    if (m3_get_skip_addr (trans))
    {
        char *ip = Tcl_Alloc (17);

        utlFip_to_str (m3_get_skip_addr (trans), ip, 16);
        Tcl_SetResult (interp, ip, TCL_DYNAMIC);
    }

    return (TCL_OK);
}

static int
m3_tcl_get_displayname (MECHA_UNUSED_ARGUMENT ClientData ignored, 
        Tcl_Interp *interp, MECHA_UNUSED_ARGUMENT int argc, 
        MECHA_UNUSED_ARGUMENT const char **argv)
{
    Tcl_AppendResult (interp, m3_get_songname (httpFthread_gettrans()), (char *) NULL);

    return (TCL_OK);
}

static int
m3_tcl_get_users (MECHA_UNUSED_ARGUMENT ClientData ignored, Tcl_Interp *interp, 
        MECHA_UNUSED_ARGUMENT int argc, MECHA_UNUSED_ARGUMENT const char **argv)
{
    char *users = Tcl_Alloc (33);

    snprintf (users, 32, "%d", m3_get_user_count (httpFthread_gettrans()));
    Tcl_SetResult (interp, users, TCL_DYNAMIC);

    return (TCL_OK);
}

static int
m3_tcl_skip_track (MECHA_UNUSED_ARGUMENT ClientData ignored, 
        MECHA_UNUSED_ARGUMENT Tcl_Interp *interp, 
        MECHA_UNUSED_ARGUMENT int argc, MECHA_UNUSED_ARGUMENT const char **argv)
{
    httpTtrans *trans = httpFthread_gettrans ();
    char ipaddr[18];

    utlFip_to_str (trans->cli_ipv4_addr, ipaddr, 17);
    logFmsg (CONT_LOG_OK, "%s: skipped %s", ipaddr, m3_get_songname (trans));

    m3_skip_track (trans);

    return (TCL_OK);
}

static int
m3_tcl_add_track (MECHA_UNUSED_ARGUMENT ClientData ignored, 
        Tcl_Interp *interp, int argc, const char **argv)
{
    if (argc != 3)
    {
        Tcl_SetResult (interp, (char *) "ERROR: requires two arguments: plalistname, filename", TCL_STATIC);

        return (TCL_ERROR);
    }

    if (m3_add_file (httpFthread_gettrans(), argv[1], argv[2]))
    {
        Tcl_AppendResult (interp, "file ", argv[2], " added", (char *) NULL);
        return (TCL_OK);
    }
    else
    {
        Tcl_AppendResult (interp, "ERROR: Could not add file '", argv[2], "' to playlist '", argv[1], "'", (char *) NULL);
        return (TCL_ERROR);
    }
}

static int
m3_tcl_reload_playlist (MECHA_UNUSED_ARGUMENT ClientData ignored, 
        MECHA_UNUSED_ARGUMENT Tcl_Interp *interp, 
        MECHA_UNUSED_ARGUMENT int argc, MECHA_UNUSED_ARGUMENT const char **argv)
{
    if (m3_reload_playlist (httpFthread_gettrans()) == true)
    {
        Tcl_AppendResult (interp, "Playlist reloaded.", NULL);
        return (TCL_OK);
    }
    else
    {
        Tcl_AppendResult (interp, "Unable to reload playlist.", NULL);
        return (TCL_ERROR);
    }
}
