#include "cueparser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 

int print_cuedata(CueData* cuedata)
{
    printf("PERFORMER: %s\n", cuedata -> performer);
    printf("TITLE: %s\n", cuedata -> title);
    for(cuedata -> trackdata = cuedata -> track_head -> next; cuedata -> trackdata != NULL; cuedata -> trackdata = cuedata -> trackdata -> next){
        printf("TRACK NO: %d\n", cuedata -> trackdata -> track_number);
        printf("TRACK PERFORMER: %s\n", cuedata -> trackdata -> performer);
        printf("TRACK TITLE: %s\n", cuedata -> trackdata -> title);
        printf("TRACK START TIME: %d\n", cuedata -> trackdata -> start_time);
    }
    return 0;
}

int freeup_cuedata(CueData* cuedata)
{
    TrackData * tmp;
    for(cuedata -> trackdata = cuedata -> track_head -> next; cuedata -> trackdata != NULL; cuedata -> trackdata = cuedata -> trackdata -> next, free(tmp)){
        tmp = cuedata -> trackdata;
    }
    free(cuedata);
    return 0;
}




CueData* read_cuefile(char* filename)
{
    int phase = 1;
    char buf[MAX_STRING];
    int mm, ss, ms;
    int trackno;
    CueData* cuedata = NULL;
    FILE* file = fopen(filename, "r");

    // init

    cuedata = (CueData*)malloc(sizeof(CueData));
    cuedata -> trackdata = (TrackData*)malloc(sizeof(TrackData));
    cuedata -> total_tracks = 0;
    cuedata -> track_head = cuedata -> trackdata;
    cuedata -> trackdata -> track_number = 0;
    cuedata -> trackdata -> next = NULL;
    cuedata -> trackdata -> prev = NULL;
    cuedata -> trackdata -> title[0] = 0;
    cuedata -> trackdata -> performer[0] = 0;
    
    //init_cuedata(cuedata);
    //Parse Phase 1: Get the global music info
    while(!feof(file) && phase == 1){
        fscanf(file, "%[^\n]\n", buf);
        if(strncmp(buf, "PERFORMER", 9) == 0){
            strcpy(cuedata -> performer, strstr(buf, " "));
        }

        if(strncmp(buf, "TITLE", 5) == 0){
            strcpy(cuedata -> title, strstr(buf, " "));
        }

        if(strncmp(buf, "TRACK", 5) == 0){
            phase = 0;
        }
    }
    //fclose(file);
    //Parse Phase 2: Get each track info
    while(!feof(file)){
        if(phase == 2){
            fscanf(file, "%[^\n]\n", buf);
        }

        if(strncmp(buf, "TRACK", 5) == 0){
            //Every time I see TRACK, I will know the next track
            cuedata -> trackdata -> next = (TrackData*)malloc(sizeof(TrackData));
            cuedata -> trackdata  = cuedata -> trackdata -> next;
            cuedata -> trackdata -> next = NULL;
            cuedata -> trackdata -> prev = cuedata -> trackdata;
            sscanf(buf, "TRACK %d AUDIO", &trackno);
            cuedata -> total_tracks += 1;
            cuedata -> trackdata -> track_number = trackno;
            phase = 2;
        }
        if(strncmp(buf, "TITLE", 5) == 0 && phase == 2){
            strcpy(cuedata -> trackdata -> title, strstr(buf, " \"") + 1);
        }

        if(strncmp(buf, "PERFORMER", 9) == 0 && phase == 2){
            strcpy(cuedata -> trackdata -> performer, strstr(buf, " \"") + 1);
        }
        if(strncmp(buf, "INDEX 01", 8) == 0 && phase == 2){
            sscanf(buf, "INDEX 01 %d:%d:%d", &mm, &ss, &ms);
            cuedata -> trackdata -> start_time = mm * 60 + ss;
        }
    }
    fclose(file);
    return cuedata;
}

    
