#include "fxlib.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#define FILESIZE() (Bfile_GetFileSize(fichier)+1)
#define FREE(pointeur) free(pointeur);\
				pointeur=NULL;
#define MESURE 0
#define TYPE 1
#define HAUTEUR 2
#define TEMPS 3
#define DUREE 4
#define POINTE 5
#define NOTE 1
#define NOTE_X partition[numero_note][2]*9
#define NOTE_Y -partition[numero_note][3]*3
#define RONDE 0
#define BLANCHE 1
#define NOIRE 2
#define CROCHE 3


//int intro(void);
//int testmalloc(void);
//char decoupage_ligne(char *ligne);
//char *substr(char texte[],long debut,long fin);
//void nouvelle_note(Note *note,long parametre_note,int type_parametre_note);



int AddIn_main(int isAppli, unsigned short OptionNum)
{
	unsigned int key;
	Bdisp_AllClr_DDVRAM();
	intro();
	while(1)
	{
		GetKey(&key);
	}
	return 1;
}

char *substr(char texte[],long debut,long fin)
{
	long compteur=0;
	char *retour=NULL;
	retour=malloc((fin-debut+1)*sizeof(char));
	for(compteur=debut;compteur<=fin;compteur++)
		retour[compteur-debut]=texte[compteur];
	retour[compteur-debut+1]='\0';
	return retour;
}

char decoupage_ligne(long ***partition,char texte[],int *nombre_malloc)
{

	int compteur=0;
	int nombre_ligne=0;
	int parametre_note=0;
	char affichage[21];
	int debut=0;

	for(compteur=0;compteur<=strlen(texte);compteur++)
	{
		if(texte[compteur]=='\n')
		{
//			sprintf(affichage,"%ld",compteur);
//			PrintMini(compteur,15,affichage,0);
			nombre_ligne++;
		}
	}

	*partition=malloc(sizeof(long*)*nombre_ligne);
	for(compteur=0;compteur<nombre_ligne;compteur++)
		(*partition)[compteur]=malloc(5*sizeof(int));

	nombre_ligne=0;
	
	for(compteur=0;compteur<=strlen(texte);compteur++)
	{
		if(texte[compteur]==',')
		{
			//ICI !!!!
			(*partition)[nombre_ligne][parametre_note]=atol(substr(texte,debut,compteur-1));
			sprintf(affichage,"%ld",(*partition)[nombre_ligne][parametre_note]);
			PrintMini(compteur*10+1,1,affichage,0);
			parametre_note++;
			debut=compteur+1;
		}
		else if(texte[compteur]=='\n')
		{
			nombre_ligne++;
			parametre_note=0;
		}
	}
	sprintf(affichage,"%ld",nombre_ligne);
	PrintMini(compteur,22,affichage,0);
	for(compteur=0;compteur<nombre_ligne;compteur++)
	{
		sprintf(affichage,"%ld",(*partition)[compteur][3]);
		PrintMini(compteur*10,22,affichage,0);
	}
	*nombre_malloc=nombre_ligne;

}

int intro(void)
{
	int mesure=0;
	char affichage[15];
	int nombre_malloc=0;
	int compteur=0;
	char *retour_fichier=NULL;
	long **partition=NULL;
	long retour=0;
	FONTCHARACTER nom_fichier[]={'\\','\\','c','r','d','0','\\','f','i','n','a','l','e','.','t','x','t',0};
	int fichier;
	Bfile_CreateFile(nom_fichier,3);

	fichier=Bfile_OpenFile(nom_fichier,_OPENMODE_READ);

	retour_fichier=malloc(FILESIZE()*sizeof(char));
	Bfile_ReadFile(fichier,retour_fichier,FILESIZE(),0);
	decoupage_ligne(&partition,retour_fichier,&nombre_malloc);
	Bfile_CloseFile(fichier);

	FREE(retour_fichier);
	

	locate(8,4);
	Print("FINALE");
	PrintMini(3,45,"Gerez vos partitions musicales",0);
	sprintf(affichage,"%ld",nombre_malloc);
//	sprintf(affichage,"%ld",partition[0][0]);
	PrintMini(1,1,affichage,0);
	Bdisp_PutDisp_DD();
	
	moteur_rendu(partition,nombre_malloc);


	for(compteur=0;compteur<((nombre_malloc)-1);compteur++)
		FREE(partition[compteur]);

	FREE(partition);



}

int moteur_rendu(long **partition,int nombre_notes)
{
	long mesure=0;
	int numero_note=0;
	Bdisp_AllClr_DDVRAM();
	portee(mesure);
	for(numero_note=0;numero_note<nombre_notes;numero_note++)
	{	
		if(partition[numero_note][MESURE]==mesure)
		{
			if(partition[numero_note][TYPE]==NOTE)
			{
				if(partition[numero_note][DUREE]==RONDE || partition[numero_note][DUREE]==BLANCHE)
				{
					Bdisp_DrawLineVRAM(NOTE_X+16,NOTE_Y+37,NOTE_X+16,NOTE_Y+36);
					Bdisp_DrawLineVRAM(NOTE_X+16,NOTE_Y+36,NOTE_X+17,NOTE_Y+35);
					Bdisp_DrawLineVRAM(NOTE_X+17,NOTE_Y+35,NOTE_X+19,NOTE_Y+35);
					Bdisp_DrawLineVRAM(NOTE_X+19,NOTE_Y+35,NOTE_X+20,NOTE_Y+36);
					Bdisp_DrawLineVRAM(NOTE_X+20,NOTE_Y+36,NOTE_X+20,NOTE_Y+37);
					Bdisp_DrawLineVRAM(NOTE_X+20,NOTE_Y+37,NOTE_X+20,NOTE_Y+38);
					Bdisp_DrawLineVRAM(NOTE_X+20,NOTE_Y+38,NOTE_X+19,NOTE_Y+39);
					Bdisp_DrawLineVRAM(NOTE_X+19,NOTE_Y+39,NOTE_X+17,NOTE_Y+39);
					Bdisp_DrawLineVRAM(NOTE_X+17,NOTE_Y+39,NOTE_X+16,NOTE_Y+38);
					if(partition[numero_note][DUREE]==BLANCHE)
					{
						Bdisp_DrawLineVRAM(NOTE_X+16,NOTE_Y+38,NOTE_X+16,NOTE_Y+37);
						Bdisp_DrawLineVRAM(NOTE_X+20,NOTE_Y+38,NOTE_X+20,NOTE_Y+26);
					}
				}
				else if(partition[numero_note][DUREE]==NOIRE || partition[numero_note][DUREE]==CROCHE)
				{
					Bdisp_DrawLineVRAM(NOTE_X+16,NOTE_Y+38,NOTE_X+16,NOTE_Y+36);
					Bdisp_DrawLineVRAM(NOTE_X+16,NOTE_Y+36,NOTE_X+17,NOTE_Y+39);
					Bdisp_DrawLineVRAM(NOTE_X+17,NOTE_Y+39,NOTE_X+17,NOTE_Y+35);
					Bdisp_DrawLineVRAM(NOTE_X+17,NOTE_Y+35,NOTE_X+18,NOTE_Y+39);
					Bdisp_DrawLineVRAM(NOTE_X+18,NOTE_Y+39,NOTE_X+18,NOTE_Y+35);
					Bdisp_DrawLineVRAM(NOTE_X+18,NOTE_Y+35,NOTE_X+19,NOTE_Y+39);
					Bdisp_DrawLineVRAM(NOTE_X+19,NOTE_Y+39,NOTE_X+19,NOTE_Y+35);
					Bdisp_DrawLineVRAM(NOTE_X+19,NOTE_Y+35,NOTE_X+20,NOTE_Y+38);
					Bdisp_DrawLineVRAM(NOTE_X+20,NOTE_Y+38,NOTE_X+20,NOTE_Y+26);

					if(partition[numero_note][DUREE]==CROCHE)
						Bdisp_DrawLineVRAM(NOTE_X+20,NOTE_Y+26,NOTE_X+22,NOTE_Y+29);
				}
			}
			
		}
	}

}



portee(long mesure)
{
	int compteur=0;
	for (compteur=1;compteur<=5;compteur++)
		Bdisp_DrawLineVRAM(1,64-21-compteur*6,127,64-21-compteur*6);
	if(mesure==0)
	{
		Bdisp_SetPoint_VRAM(8,9,1);
		Bdisp_DrawLineVRAM(7,10,8,10);
		Bdisp_DrawLineVRAM(6,11,9,11);
		Bdisp_DrawLineVRAM(6,12,9,12);
		Bdisp_SetPoint_VRAM(6,13,1);
		Bdisp_SetPoint_VRAM(9,13,1);
		Bdisp_SetPoint_VRAM(6,14,1);
		Bdisp_SetPoint_VRAM(9,14,1);
		Bdisp_SetPoint_VRAM(6,15,1);
		Bdisp_SetPoint_VRAM(9,15,1);
		Bdisp_SetPoint_VRAM(6,16,1);
		Bdisp_DrawLineVRAM(8,16,9,16);
		Bdisp_SetPoint_VRAM(6,17,1);
		Bdisp_DrawLineVRAM(8,17,9,17);
		Bdisp_DrawLineVRAM(6,18,8,18);
		Bdisp_DrawLineVRAM(6,19,8,19);
		Bdisp_DrawLineVRAM(5,20,7,20);
		Bdisp_DrawLineVRAM(4,21,7,21);
		Bdisp_DrawLineVRAM(3,22,5,22);
		Bdisp_SetPoint_VRAM(7,22,1);
		Bdisp_DrawLineVRAM(2,23,5,23);
		Bdisp_SetPoint_VRAM(7,23,1);
		Bdisp_DrawLineVRAM(2,24,4,24);
		Bdisp_DrawLineVRAM(7,24,9,24);
		Bdisp_DrawLineVRAM(1,25,3,25);
		Bdisp_DrawLineVRAM(6,25,10,25);
		Bdisp_DrawLineVRAM(1,26,2,26);
		Bdisp_DrawLineVRAM(5,26,11,26);
		Bdisp_DrawLineVRAM(1,27,2,27);
		Bdisp_SetPoint_VRAM(5,27,1);
		Bdisp_SetPoint_VRAM(8,27,1);
		Bdisp_DrawLineVRAM(10,27,12,27);
		Bdisp_DrawLineVRAM(1,28,2,28);
		Bdisp_SetPoint_VRAM(5,28,1);
		Bdisp_SetPoint_VRAM(8,28,1);
		Bdisp_DrawLineVRAM(11,28,12,28);
		Bdisp_SetPoint_VRAM(2,29,1);
		Bdisp_SetPoint_VRAM(5,29,1);
		Bdisp_SetPoint_VRAM(8,29,1);
		Bdisp_DrawLineVRAM(11,29,12,29);
		Bdisp_SetPoint_VRAM(2,30,1);
		Bdisp_SetPoint_VRAM(6,30,1);
		Bdisp_SetPoint_VRAM(8,30,1);
		Bdisp_SetPoint_VRAM(11,30,1);
		Bdisp_SetPoint_VRAM(3,31,1);
		Bdisp_DrawLineVRAM(7,31,8,31);
		Bdisp_SetPoint_VRAM(11,31,1);
		Bdisp_DrawLineVRAM(4,32,5,32);
		Bdisp_DrawLineVRAM(7,32,10,32);
		Bdisp_DrawLineVRAM(5,33,9,33);
		Bdisp_SetPoint_VRAM(9,34,1);
		Bdisp_SetPoint_VRAM(9,35,1);
		Bdisp_DrawLineVRAM(5,36,6,36);
		Bdisp_SetPoint_VRAM(9,36,1);
		Bdisp_DrawLineVRAM(4,37,7,37);
		Bdisp_SetPoint_VRAM(9,37,1);
		Bdisp_DrawLineVRAM(4,38,6,38);
		Bdisp_SetPoint_VRAM(9,38,1);
		Bdisp_DrawLineVRAM(4,39,5,39);
		Bdisp_SetPoint_VRAM(9,39,1);
		Bdisp_DrawLineVRAM(5,40,8,40);
	}
}



#pragma section _BR_Size
unsigned long BR_Size;
#pragma section


#pragma section _TOP
int InitializeSystem(int isAppli, unsigned short OptionNum)
{
    return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
}

#pragma section