#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 10000

int main(int argc, char *argv[])
{
	FILE *testlist_fp;
	FILE *fp;
	FILE *q_fp;
	FILE *source_fp;
	char str[MAX] = {0};
	char str2[MAX] = {0};
	char str3[MAX] = {0};
	char str4[MAX] = {0};
	char filename[100] = {0};
	char q_filename[20] = {0};
	char tmp[20] = {0};
	char q_PID[20] = {0};
	char t_PID[20] = {0};
	char *pch;
	char q_type[20] = {0};
	char t_type[20] = {0};
	int cur_N, total_sample=0, correct=0, wrong=0;
	int N;
	int enterout;
	float identity=0;
	float c_ident=0, w_ident=0;

	if(argc == 1) {
		fprintf(stderr, "Usage: ./evaluate [test.list] [source_file] [N]\n");
		fprintf(stderr, "N is the number of candidates for a query\n");
		return 0;
	}

	N = atoi(argv[3]);
	
	if( (testlist_fp = fopen( argv[1], "r")) == NULL) {
		fprintf(stderr, "File %s open error\n", argv[1]);
		return 0;
	}
	
	if( (source_fp = fopen( argv[2], "r")) == NULL) {
		fprintf(stderr, "File %s open error\n", argv[2]);
		return 0;
	}

	while(fgets(str, MAX, testlist_fp) != NULL) {
		
		pch = strtok(str, "\n\r");
		strcpy(filename, pch);
		strcpy(q_filename, pch);
		strcat(filename, ".out");	
		strcat(q_filename, ".test");
		
		// open xxx.test file
		if( (q_fp = fopen( q_filename, "r")) == NULL){
			fprintf(stderr, "File %s open error\n", q_filename);
			return 0;
		}

		// open xxx.out file
		if( (fp = fopen(filename, "r")) == NULL){
			fprintf(stderr, "File %s open error\n", filename);
			return 0;
		}
		cur_N = 0;
		enterout = 0;
		// read xxx.out file
		while(fgets(str2, MAX, fp) != NULL) {
			if(str2[0] == '#') 	// skip header
				continue;
			if(cur_N >= N)
				break;
			total_sample++;
			enterout = 1;
			memset(t_PID, 0, 20);
			memset(q_PID, 0, 20);
			
			pch = strtok(str2, " \t");	// q_PID
			strcpy(q_PID, pch);
			pch = strtok(NULL, " \t");
			strcpy(t_PID, pch);	// t_PID
			pch = strtok(NULL, " \t");
			identity = atof(pch);
			//fprintf(stdout, "query: %s   target: %s\n", q_PID, t_PID);
						
			if(fgets(str3, MAX, q_fp) == NULL){
				fprintf(stderr, "File %s has error, may be empty!\n");
				return 0;
			}
			
			memset(tmp, 0, 20);
			memset(q_type, 0, 20);
			memset(t_type, 0, 20);
			
			pch = strtok(str3, " \t");	// PID
			pch = strtok(NULL, " \t");
			strcpy(tmp, pch);				// tmp stores information of fold type
			pch = strtok(tmp, ".");
			strcpy(q_type, pch);
			pch = strtok(NULL, ".");
			strcat(q_type, pch);

			//fprintf(stdout,"%s %s\n", q_PID, q_type);			
			
			// read source.txt to evaluate answer
			while(fgets(str4, MAX, source_fp) != NULL){
				if(str4[0] != '>')
					continue;
				else{
					pch = strtok(str4, " \t");
					// if find target_PID
					if(strcmp(pch+1, t_PID) == 0) {
						pch = strtok(NULL, " \t");
						strcpy(tmp, pch);
						pch = strtok(tmp, ".");
						strcpy(t_type, pch);
						pch = strtok(NULL, ".");
						strcat(t_type, pch);
						if(strcmp(t_type, q_type) == 0) {
							fprintf(stdout, "O query: %s   target: %s   iden.: %.2f\n", q_PID, t_PID, identity);
							correct++;
							c_ident += identity;
						}
						else {
							fprintf(stdout, "X query: %s   target: %s   iden.: %.2f\n", q_PID, t_PID, identity);
							w_ident += identity;
							wrong++;
						}						
						break;
					}
					// end if
				}
				
			}
					
			rewind(source_fp);
			cur_N++;
		}
		if(enterout == 0)
			fprintf(stderr, "%s empty\n", filename);
		fclose(fp);
		fclose(q_fp);
	}
	fprintf(stdout, "Accuracy is %f (%d/%d)\n", (float)correct/total_sample, correct, total_sample)	;
	fprintf(stderr, "Total has %d sample, correct %d, ACC. %f\n", total_sample, correct, (float)correct/total_sample);
	fprintf(stderr, "Average ident. of correct samples is %f\n", c_ident/correct);
	fprintf(stderr, "Average ident. of wrong samples is %f\n", w_ident/wrong);
	fclose(testlist_fp);
	fclose(source_fp);
	return 1;
	
}

