#include<stdio.h>
#include "config.h"
#include<string.h>

//tokenize the line
int chop(char s[],char a[][maxstr],char delim[]){
    int n=0;
    char *str;
    
    str=(char*)strtok(s,delim);
    while(str!=NULL){
        strcpy(a[n],str);
        str=(char*)strtok(NULL,delim);
        n++;
    }
    
    return n;
}

// draw the graph based on data in input
void draw(char *input,char *output){
    int i;
    FILE *f;
    f=fopen(input,"r");
    FILE *out;
    out=fopen(output,"w");
    FILE *da;
    da=fopen(tempfile,"w");
    
    char line[maxstr];
    
    char a[maxparam][maxstr];
    fgets(line,maxstr,f);
    rem(line);
    int nparam=chop(line,a,"|");
    
    fprintf(out,"set terminal png transparent nocrop enhanced font arial 8 size 420,320\n");
    
    fprintf(out,"set output '%s.png'\n",input);
    fprintf(out,"set style data linespoints\n");
  
    fprintf(out,"plot ");
    for (i=1;i<nparam-1;i++) fprintf(out,"'temp.txt' using 1:%i title '%s',",i+1,a[i]);
    fprintf(out,"'temp.txt' using 1:%i title '%s'",nparam,a[i]);
    
    while(fgets(line,maxstr,f)){
        rem(line);
        int n=chop(line,a,"|");

        for (i=0;i<n;i++) fprintf(da,"%s ",a[i]);
        fprintf(da,"\n");
    }    
    
    fclose(f);
    fclose(out);
    fclose(da);    
}

// check if parameters set satisfy the constraints
int check(char param[][maxstr],char cond[][maxstr],int m,int n,char *red){
    int i,j;
    int dd[maxstr];
    
    for (j=0;j<n;j++) dd[j]=1;
    
    for (i=2;i<m;i+=2){
        int ok=0;
        for (j=0;j<n;j+=2)
            if (strcmp(param[i],cond[j])==0&&strcmp(param[i+1],cond[j+1])==0){
                ok=1;dd[j+1]=0;
            }
        if (!ok) return 0;
    }
    
    red[0]=0;
    
    for (j=1;j<n;j+=2)
        if (dd[j]){
            strcat(red," ");
            strcat(red,cond[j]);
        }
    return 1;
}

    char a[maxstr][maxstr][20]; // store the table 
    char b[maxstr][maxstr];  // set of parameters
    int column=0,row=0;

void concate(char *s1,char *s2,char *s3){
    int i;
    
    int n=strlen(s1);
//    printf("son %i %s %s %s\n",n,s1,s2,s3);    
    for (i=0;i<strlen(s2);i++) s1[n++]=s2[i];
    for (i=0;i<strlen(s3);i++) s1[n++]=s3[i];
    s1[n]=0;
}

int readResults(char name[],int argc,char argv[][maxstr]){
    int i,j;
    char line[maxstr];
    
    column=0;

    char header[maxstr];
    
    i=0;
    while(1){
        
        // expt file i
        FILE *f;

        char number[maxstr];
        sprintf(number,"%i",i);

        char file[maxstr];
        for (j=0;j<strlen(name);j++) file[j]=name[j];
        file[strlen(name)]=0;

        concate(file,".",number);

        f=fopen(file,"r");
        
        printf("%s\n",file);
        if (f==NULL) break;
        i++;
        
        int ok=0,add=0;

        int n=0;
        while(fgets(line,maxstr,f)){
            rem(line);
            if (line[0]=='*'){
                // parameter
                char *s=(char*)strtok(line,":* ");
                strcpy(b[n],s);
                n++;

                s=(char*)strtok(NULL,":* ");
                strcpy(b[n],s);
                n++;
            }else if (line[0]!='#'&&(ok||check(argv,b,argc,n,header))){
                char *s=(char*)strtok(line,"| ");                                                
                if (!ok) row=1;

                if (column==0){
                    strcpy(a[row][0],s);add=2;
                    s=(char*)strtok(NULL,"| ");
                    
                    strcpy(a[row][column+1],s);
                    strcpy(a[0][0],"-");
                    strcpy(a[0][column+1],header);
                }else{
                    add=1;
                    s=(char*)strtok(NULL,"| ");
                    strcpy(a[row][column],s);
                    strcpy(a[0][column],header);                                       
                }
                
                row++;
                ok=1;
            }else{
                // ignore
            }
        }
        
        column+=add;
        
        fclose(f);
    }    
    
    if (i==0) return 0;
    return 1;
}

void output(char fname[]){
    int i,j;
    
    FILE *f;
    f=fopen(fname,"w");
    
    for (i=0;i<row;i++){
        for (j=0;j<column;j++) fprintf(f,"%s|",a[i][j]);
        fprintf(f,"\n");
    }

    fclose(f);

    draw(fname,"plot");
    
    char command[maxstr];

    system("gnuplot plot");

//    printf("row :%i column: %i\n",row,column);    
    printf("Analyzing is completed\n");
    printf("The results are in %s\n",fname);
    printf("Graph is in %s.png\n",fname);
}

int main(int argc,char *argv[]){
    if (argc<2||argc%2==1){
        printf("Parameters are incorrect\n");return;
    }

    char arg[maxstr][maxstr];
    int i;
    for (i=0;i<argc;i++) strcpy(arg[i],argv[i]);

    //read database
    if (readDatabase(argv[1])==0){
        printf("Name or id is not found\n");
        return;
    }
    
    if (num==-1){
        printf("Experiment not found\n");return;
    }
    
    //read results, summarize    

    if (readResults(name,argc,arg)==0) printf("Experiment has not run yet\n");
    else output(name);
}
