#include "../../include/audio/timeline.h"

//int ImportSample(TimelineData* timeline, char* file) {
int ImportSample(TimelineData* timeline, char* file, uint32_t beatOffset, uint8_t track) {

	if (timeline->sampleNum >= MAX_SAMPLE_NUM) { 
		
		printf("Maximum number of samples imported.\n");
		return -1; 
	}
	

	//see if file is already imported to save memory
	int i = 0;
	bool copy = false;
	for (i; i < timeline->sampleNum; i++) {

		if (timeline->files[i] != NULL) {	
			
			copy = (strcmp(file, timeline->files[i]) == 0);
			if (copy) { break; }
		}
	}

	
	//save file name
	if (timeline->files[timeline->sampleNum] == NULL) {

		if (copy == false) {

			//save string to index
			timeline->files[timeline->sampleNum] = malloc(sizeof(char)*strlen(file));
			strcpy(timeline->files[timeline->sampleNum], file);
		}
	} else {
		printf("Something wrong with the files.\n");
		return -2;
	}

	

	if (copy) {
		//copy already existing sample to save memory
		timeline->samples[timeline->sampleNum] = CreateSample(NULL, timeline, timeline->samples[i], beatOffset, 0, 0, 0, track);
	} else {
		//store pointer to audio data in timeline
		timeline->samples[timeline->sampleNum] = CreateSample(file, timeline, NULL, beatOffset, 0, 0, 0, track);
		//timeline->samples[timeline->sampleNum] = CreateSample(file, timeline, NULL, 0, 0, 0, 0, 0);
		timeline->samples[timeline->sampleNum]->fileIndex = timeline->sampleNum;
	}
	
	
	//store pointer to audio data in timeline
	//timeline->samples[timeline->sampleNum] = CreateSample(file, timeline, NULL, 0, 0, 0, 0, 0);
	
	//check if sample was actually created
	if (timeline->samples[timeline->sampleNum] == NULL) {
		
		if (copy == false) { free(timeline->files[timeline->sampleNum]); }
		timeline->files[timeline->sampleNum] = NULL;
		printf("Could not create requested sample.\n");
		return -3;
	}

	timeline->sampleNum++;
	printf("Imported sample to timeline.\n");

	return 0;
}


void AdjustSampleBeats(AlsaData* alsa_data, TimelineData* timeline) {
	
	//set new beat period for daw
	timeline->spb = (double)(60.0/((double)timeline->bpm));

	//adjust beat length of sample to change in bpm
	for (int i = 0; i < timeline->sampleNum; i++) {
	
		Sample* sample = timeline->samples[i];

		uint32_t saveBeat = sample->beatOffset-sample->beatCropFront;
		double newBeatCrop = (((double)sample->beatCropFront)/((double)sample->beatLength));
		
		sample->beatLength = (uint32_t)(((double)timeline->bpm)*(sample->seconds/60.0)*16.0)+1;
		
		sample->beatCropFront = (uint32_t)round(((double)sample->beatLength)*newBeatCrop);
		sample->beatOffset += (int32_t)(sample->beatOffset-(saveBeat+sample->beatCropFront));
		/*
		double realLength = ((double)timeline->bpm)*(timeline->samples[i]->seconds/60.0)*16.0;
		double ratio = ((double)timeline->samples[i]->beatCropFront / (double)timeline->samples[i]->beatLength);
		uint32_t startingBeat = timeline->samples[i]->beatOffset + timeline->samples[i]->beatCropFront;
		timeline->samples[i]->beatCropFront = (uint32_t)(ratio*realLength) + 1;

		if (timeline->samples[i]->beatOffset+timeline->samples[i]->beatCropFront != startingBeat) {
		
			timeline->samples[i]->beatOffset = (startingBeat - timeline->samples[i]->beatCropFront);
		}
		
		timeline->samples[i]->beatLength = ((uint32_t)realLength) + 1;
		*/

		timeline->samples[i]->playing = false;
	}
}

/*
void HandleAudio(AlsaData* alsa_data, TimelineData* timeline, ) {
}
*/
