/* 
 * Balcony
 * 
 */

#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include "auth.h"
#include "bs_proto.h"
#include "storage.h"

#include <string.h>
#include "response_list.h"
#include "bs.h"
#include "system.h"

int main(int argc, char **argv) {

	if (argc != 3) {
		printf("Parametros invalidos. Exemplo: test-storage Users.dat Titles.dat\n");
		return -1;
	}
	
	if (system_init(argv[1], argv[2]) != 0) {
		printf("Erro: Ficheiro invalido.\n");
		return -1;
	}
		
	
/*	
	//Teste do bs_give e system_give
	
	bs_pdu_req_t req;
	int reqsz;
	res_list_t* lista;
	
	req.type = REQ_GIVE;
	strncpy(req.body.give.titles, "M3 F7 P1 E4", MAX_TITLES_SIZE);
	reqsz = sizeof(req) - sizeof(req.body) + sizeof(req.body.give);
	
	printf("Size: %d\n", reqsz);
	
	bs_give(&req, reqsz, &lista);
	
	res_lst_destroy(lista);

	//Teste do bs_search
	bs_pdu_req_t req;
	int reqsz;
	char username[MAX_USERNAME_SIZE] = "U1";
	char password[MAX_PASSWORD_SIZE] = "P1";
	//char password[MAX_PASSWORD_SIZE] = "P2";
	char titles[MAX_TITLES_SIZE] = "A1 E4 M3 P2";
	
	res_list_t* lista;
	
	req.type = REQ_SEARCH;
	strncpy(req.body.search.username, username, MAX_USERNAME_SIZE);
	strncpy(req.body.search.password, password, MAX_PASSWORD_SIZE);
	strncpy(req.body.search.titles, titles, MAX_TITLES_SIZE);
	reqsz = sizeof(req) - sizeof(req.body) + sizeof(req.body.search);
	
	bs_search(&req, reqsz, &lista);
	
	response_t *response;
	
	while ((response = res_lst_pop(lista)) != NULL) {
		printf("Type:%d Status:%d Size:%d ", response->res.type, response->res.status, response->ressz);
		printf("Title:%s Counter:%s Weight:%d Neighbours:%s Reason:%s\n", response->res.body.search.title,
			   response->res.body.search.counter,
			   response->res.body.search.weight,
			   response->res.body.search.neighbours,
			   response->res.body.search.reason);
		
		bs_free_response(response);
	}
	printf("Vou destruir a lista.\n");
	res_lst_destroy(lista);
	

	//Teste do bs_cancel
	bs_pdu_req_t req;
	int reqsz;
	char username[MAX_USERNAME_SIZE] = "U2";
	// o cliente aceita isto char ids[MAX_IDS_SIZE] = "A-M3 A-P2" mas o servidor só recebe isto:
	char titles[MAX_TITLES_SIZE] = "A1 M3";
	
	res_list_t* lista;
	
	req.type = REQ_CANCEL;
	strncpy(req.body.cancel.username, username, MAX_USERNAME_SIZE);
	strncpy(req.body.cancel.titles, titles, MAX_TITLES_SIZE);
	
	reqsz = sizeof(req) - sizeof(req.body) + sizeof(req.body.cancel);
	printf("Request size: %d\n", reqsz);
	
	bs_cancel(&req, reqsz, &lista);
	
	res_lst_destroy(lista);


	//Teste do bs_take
	bs_pdu_req_t req;
	int reqsz;
	char username[MAX_USERNAME_SIZE] = "U1";
	char titlecs[MAX_TITLES_SIZE] = "M3_0 P2_1 P1_1 M3_1";
	
	res_list_t* lista;
	
	req.type = REQ_TAKE;
	strncpy(req.body.take.username, username, MAX_USERNAME_SIZE);
	strncpy(req.body.take.titlecs, titlecs, MAX_TITLES_SIZE);
	
	reqsz = sizeof(req) - sizeof(req.body) + sizeof(req.body.take);
	printf("Request size: %d\n", reqsz);
	
	bs_take(&req, reqsz, &lista);
	
	response_t *response;
	
	while ((response = res_lst_pop(lista)) != NULL) {
		int size = response->res.body.take.size;
		
		printf("Type:%d Status:%d Size:%d ", response->res.type, response->res.status, response->ressz);
		printf("Title:%s Counter:%s Size:%d Reason:%s\n", response->res.body.take.title,
			   response->res.body.take.counter,
			   response->res.body.take.size,
			   response->res.body.take.reason);
		
		if (size != 0) {
			FILE *fp;
			char cover_filename[MAX_FILENAME_SIZE];
			
			strcpy(cover_filename, "/home/daniel/RC08C10/");
			strcpy(cover_filename + strlen(cover_filename), response->res.body.take.title);
			strcpy(cover_filename + strlen(cover_filename), COVER_EXTENSION);
			
			fp = fopen(cover_filename, "w+");
			
			if (fp == NULL) {
				exit(-1);
			}
			
			fwrite(response->res.body.take.cover, sizeof(char), size, fp);
			fclose(fp);
		}
		
		bs_free_response(response);
	}
	
	res_lst_destroy(lista);
	

	//Teste do bs_release
	bs_pdu_req_t req;
	int reqsz;
	// 1 if sent to the predefined server. 0 otherwise
	int makeavailable = 1;
	char username[MAX_USERNAME_SIZE] = "U1";
	char titles[MAX_TITLES_SIZE] = "M3 P2";
	
	res_list_t* lista;
	
	req.type = REQ_RELEASE;
	strncpy(req.body.release.username, username, MAX_USERNAME_SIZE);
	strncpy(req.body.release.titles, titles, MAX_TITLES_SIZE);
	req.body.release.makeavailable = makeavailable;
	
	reqsz = sizeof(req) - sizeof(req.body) + sizeof(req.body.release);
	
	bs_release(&req, reqsz, &lista);
	
	response_t *response;
	
	while ((response = res_lst_pop(lista)) != NULL) {
		printf("Type:%d Status:%d Size:%d ", response->res.type, response->res.status, response->ressz);
		printf("Title:%s Counter:%s Reason:%s\n", response->res.body.release.title,
			   response->res.body.release.counter, response->res.body.release.reason);
		
		bs_free_response(response);
	}
	printf("Vou destruir a lista.\n");
	res_lst_destroy(lista);

*/
	//Teste do bs_return
	bs_pdu_req_t req;
	int reqsz;
	char titles[MAX_TITLES_SIZE] = "M3 P2 G4";
	
	res_list_t* lista;
	
	req.type = REQ_RETURN;
	strncpy(req.body.returns.titles, titles, MAX_TITLES_SIZE);
	
	reqsz = sizeof(req) - sizeof(req.body) + sizeof(req.body.returns);
	printf("Request size: %d\n", reqsz);
	
	bs_return(&req, reqsz, &lista);
	
	response_t *response;
	
	while ((response = res_lst_pop(lista)) != NULL) {
		printf("Type:%d Status:%d Size:%d\n", response->res.type, response->res.status, response->ressz);
		bs_free_response(response);
	}
	printf("Vou destruir a lista.\n");
	res_lst_destroy(lista);
	
	return 0;
}
