#include <string.h>
#include <stdio.h>
#include <c++tk.h>

#include "cd-write.h"

static cuesheet *cue_sheet = 0;
static int *fsizes;

static Button b, bset;
static Control C[3][100];

static void newcue ();

static void
bumpmsf (char *m, char *s, char *f, int l)
{
    int x = l/2352;
    if (l % 2352)
	x++;
    x += *m * 60*75 + *s * 75 + *f;
    *m = x/(60*75);
    *s = (x - *m * 60*75)/75;
    *f = x - (*m * 60*75 + *s * 75);
}
static void 
dismiss (int argc, char **argv)
{
    int i;
    cuesheet *c = cue_sheet;

    for (i = 0; fsizes[i]; i++) {
	c->pregap->m = atoi (C[0][i].subwidget ("entry", "get"));
	c->pregap->s = atoi (C[1][i].subwidget ("entry", "get"));
	c->pregap->f = atoi (C[2][i].subwidget ("entry", "get"));
	c = c->next;
    }
    TkDestroy (".cue");
}
static void 
reset (int argc, char **argv)
{
    int i = 0;
    cuesheet *c = cue_sheet;
    while (fsizes[i]) {
	C[0][i].configure ("-value 0");
	C[1][i].configure ("-value 2");
	C[2][i].configure ("-value 0");
	c->pregap->m = 0;
	c->pregap->s = 2;
	c->pregap->f = 0;
	c = c->next;
	i++;
    }
    TkUpdate ();

}
static void
newcue ()
{
    int i;
    cuesheet *c;

    delete_cue_sheet ();
    for (i = 0; fsizes[i]; i++) {
	if (!cue_sheet)
	    cue_sheet = c = new cuesheet;
	else {
	    c->next = new cuesheet;
	    c = c->next;
	}
	c->pregap->m = 0; 
	c->pregap->s = 2; 
	c->pregap->f = 0; 
    }
}
void
kill_cue ()
{
    dismiss (0, 0);
}
void
delete_cue_sheet ()
{
    if (*TkWinfo ("exists", ".cue") == 1)
	TkDestroy (".cue");
    if (cue_sheet)
	delete cue_sheet;
    cue_sheet = 0;
}
cuesheet *
check_cue_sheet (int *sizes)
{
    int i, j;
    cuesheet *c, *prev;
    char min, sec, frame;
    
    fsizes = sizes;

    if (!cue_sheet)
	newcue ();
    else {
	c = cue_sheet;
	for (i = 0; sizes[i]; i++) {
	    if (sizes[i+1] && !c->next) {
		c->next = new cuesheet;
		c->next->pregap->m = 0; 
		c->next->pregap->s = 2; 
		c->next->pregap->f = 0; 
	    }
	    prev = c;
	    c = c->next;
	}
	if (c) {
	    delete c;
	    prev->next = 0;
	}
    }

    c = cue_sheet;
    min = sec = frame = 0;
    for (i = 0; sizes[i]; i++) {
	if (*TkWinfo ("exists", ".cue") == '1') {
	    c->pregap->m = atoi (C[0][i].subwidget ("entry", "get"));
	    c->pregap->s = atoi (C[1][i].subwidget ("entry", "get"));
	    c->pregap->f = atoi (C[2][i].subwidget ("entry", "get"));
	}
	j = 60*75*c->pregap->m + 75*c->pregap->s + c->pregap->f;
	c->track = i + 1;
	if (i)
	    bumpmsf (&min, &sec, &frame, j*2352);
	if (c->index) {
	    delete c->index;
	    c->index = 0;
	}
	c->addindex (min, sec, frame); // start of track
	bumpmsf (&min, &sec, &frame, fsizes[i]);
	c->addindex (min, sec, frame); // end of track
	c = c->next;
    }
    return cue_sheet;
}
void
edit_cue_sheet (int *sizes)
{
    int i, j;
    char s[180], p[120], w[30], f[40], min, sec, frame;
    cuesheet *c;


    fsizes = sizes;

    c = check_cue_sheet (sizes);

    TkToplevel (".cue");
    TkWm ("title .cue", "{Edit cue sheet}");

    for (i = 0; sizes[i]; i++) ;
    if (i < 4)
	i = 4;
    if (i > 10)
	i = 10;
    sprintf (s, "-height %d", 34*i);
    ScrolledWindow SW = tixScrolledWindow (".cue.w", s);
    strcpy (w, SW.subwidget ("window", ""));
    for (i = 0; i < sizes[i]; i++) {
	sprintf (f, "%s.f%d", w, i);
	TkFrame (f);
	sprintf (s, "%s.t -relief groove -state disabled -disabledforeground black -text {track %02d}", f, i+1);
	TkButton (s);
	*strchr (s, ' ') = 0;
	TkPack (s, " -side left -expand y -fill x");

	min = sec = frame = 0;
	bumpmsf (&min, &sec, &frame, sizes[i]);
	sprintf (s, "%s.d -relief groove -state disabled -disabledforeground black -text {duration: %02d:%02d:%02d}", f,
		 min, sec, frame);
	TkButton (s);
	*strchr (s, ' ') = 0;
	TkPack (s, " -side left -expand y -fill x");

	sprintf (s, "%s.m", f);
	sprintf (p, "-label { pregap: } -integer true -value %d -min 0 -max 73\
  	   -options {entry.width 2 label.anchor e}", c->pregap->m);
	C[0][i] = tixControl (s, p);
	TkPack (s, " -side left -expand y -fill x");
	sprintf (s, "%s.s", f);
	sprintf (p, "-label {} -integer true -value %d -min 0 -max 59\
  	   -options {entry.width 2 label.anchor e}", c->pregap->s);
	C[1][i] = tixControl (s, p);
	TkPack (s, " -side left -expand y -fill x");
	sprintf (s, "%s.f", f);
	sprintf (p, "-label {} -integer true -value %d -min 0 -max 74\
  	   -options {entry.width 2 label.anchor e}", c->pregap->f);
	C[2][i] = tixControl (s, p);
	TkPack (s, " -side left -expand y -fill x");

	TkPack (f, "-side top");
	c = c->next;
    }

    for (i = 0 ;i < 3; i++)
	C[i][0].subwidget ("entry", "configure -state disabled");
    ButtonBox bbox = tixButtonBox (".cue.b", "-orientation horizontal");
    bbox.add ("reset", "-text Reset");
    bbox.add ("dismiss",  "-text Dismiss");
    bbox.subwidget ("dismiss")->command (dismiss);
    bbox.subwidget ("reset")->command (reset);

    TkPack (".cue.b .cue.w", "-side top -fill x -expand y");
}
void
cuesheet::addindex (char m, char s, char f)
{
    msf *n = new msf (m, s, f); 
    
    if (!index)
       index = n;
    else {
       msf *x = index;
       while (x->next)
           x = x->next;
       x->next = n;
    }
}
