/*
 *  Copyright (C) 2008 Nuke (wiinuke@gmail.com)
 *
 *  this file is part of GeckoOS for USB Gecko
 *  http://www.usbgecko.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ogcsys.h>
#include <gccore.h>	
#include <malloc.h>
#include <gcmodplay.h>
#include <mp3player.h>
#include "font.h"
#include "tools.h"
#include "geckomusic.h"
#include "geckomenu.h"


MODPlay play;

#define musicbuffer 0x90080000
#define packetsize 3968


unsigned int commands_uploadmod()
{
	u8 modfilesize[4];
	u8 ack_packet = 0xAA;
	//char fail_packet = 0xFF;
	u8 *modbuffer = (u8*)musicbuffer;
	static u8 packetbuffer[packetsize] ATTRIBUTE_ALIGN(32);
	u32 modulesize = 0;
	u32 fullpackets = 0;
	u32 currentpacket = 0;
	u32 lastpacketbytes = 0;

	usb_sendbuffer_safe(EXI_CHANNEL_1,&ack_packet,1);
	
	usb_recvbuffer_safe(EXI_CHANNEL_1,&modfilesize, 4);
	modulesize = be32(modfilesize);

	memset((u8*)modbuffer, 0, modulesize);

	fullpackets = modulesize / packetsize;			
    lastpacketbytes  = modulesize % packetsize;	
	doprogress(0,fullpackets); // Init and draw progress box
	
	// Read In Data
	for (currentpacket=0;currentpacket < fullpackets;) 
	{
		usb_recvbuffer_safe(EXI_CHANNEL_1,&packetbuffer, packetsize);
		memcpy(modbuffer, packetbuffer, packetsize);
		modbuffer += packetsize;
		doprogress(1,fullpackets);
		currentpacket++;								
		usb_sendbuffer_safe(EXI_CHANNEL_1,&ack_packet,1);
	}
	doprogress(2,fullpackets); // Padding
	// Do remainder //
	usb_recvbuffer_safe(EXI_CHANNEL_1,&packetbuffer, lastpacketbytes);
	memcpy(modbuffer, packetbuffer, lastpacketbytes);
	DCFlushRange((void*)musicbuffer, modulesize);

	modbuffer = (u8*)musicbuffer;
	// Play
	MODPlay_Init (&play);
	MODPlay_SetMOD (&play, (u8*)modbuffer);
	MODPlay_Start (&play);

	moduleloaded = 1;
	return 1;
}


/*---------------------------------------------------------------------------*
    Name:               commands_uploadmp3
    Description:        Uploads MP3 file from PC
*---------------------------------------------------------------------------*/


u32 commands_uploadmp3()
{

	u8 *mp3buffer = (u8*)musicbuffer;
	u8 mp3filesize[4];
	u32 mp3filesizeint = 0;

	u8 ack_packet = 0xAA;
	//char fail_packet = 0xFF;
	static u8 packetbuffer[packetsize] ATTRIBUTE_ALIGN(32);
	u32 fullpackets = 0;
	u32 currentpacket = 0;
	u32 lastpacketbytes = 0;

	usb_sendbuffer_safe(EXI_CHANNEL_1,&ack_packet,1);
	usb_recvbuffer_safe(EXI_CHANNEL_1,&mp3filesize, 4);

	mp3filesizeint = be32(mp3filesize);
	memset((void*)musicbuffer, 0, mp3filesizeint);	
	
	fullpackets = mp3filesizeint / packetsize;		
    lastpacketbytes  = mp3filesizeint % packetsize;
	doprogress(0,fullpackets); // Init and draw progress box

	// Read In Data
	for (currentpacket=0;currentpacket < fullpackets;) 
	{
		usb_recvbuffer_safe(EXI_CHANNEL_1,&packetbuffer, packetsize);
		memcpy(mp3buffer, packetbuffer, packetsize);
		mp3buffer += packetsize;
		doprogress(1,fullpackets);
		currentpacket++;								
		usb_sendbuffer_safe(EXI_CHANNEL_1,&ack_packet,1);
	}
	// Remainder
	doprogress(2,fullpackets); // Padding
	usb_recvbuffer_safe(EXI_CHANNEL_1,&packetbuffer, lastpacketbytes);
	memcpy(mp3buffer, packetbuffer, lastpacketbytes);
	DCFlushRange((void*)musicbuffer, mp3filesizeint);
	mp3buffer = (void*)musicbuffer;
	
	// MP3 now loaded into memory //
	MP3Player_Init ();
	MP3Player_PlayBuffer((void*)mp3buffer,mp3filesizeint,NULL);
	mp3loaded = 1;
	return 1;
}

void checkmusicloaded()
{
	if(moduleloaded) {
		MODPlay_Unload(&play);
		moduleloaded = 0;
	}

	if(mp3loaded) {
		MP3Player_Stop();
		mp3loaded = 0;
	}
}
