/*
 *  Copyright (C) 2008 Nuke (wiinuke@gmail.com)
 *  Copyright (c) 2009 WiiPower
 *
 *  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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <sys/unistd.h>
#include "geckomenu.h"
#include "font.h"
#include "geckodvd.h"
#include "apploader.h"
#include "patchcode.h"
#include "tools.h"
#include "libcios.h"
#include "config.h"
#include "background.h"
#include "tools.h"

#include "easywbfs.h"


#define logogfx_size 563200


u32 *xfb = NULL;
static GXRModeObj *rmode = NULL;
u8 gecko_command = 0;

extern s32 __SYS_LoadMenu();

static void power_cb () 
{
	Power_Flag = true;
}

static void reset_cb () 
{
	Reset_Flag = true;
}

int main(int argc, char **argv) 
{
	exitworks = (*(u32*)0x80001800);
	//u32 pressed;
	//u32 pressedGC;

	s32 di_error_code;

	Load_Config();
	
	if (Load_IOS(CIOS_VERSION) < 0)
	{
		CIOS_VERSION = 249;
		Load_IOS(CIOS_VERSION);
	}

	/* Init Video, Pad, Fonts */
	VIDEO_Init();
	init_font();
	
	Power_Flag = false;
	Reset_Flag = false;
	SYS_SetPowerCallback (power_cb);
    SYS_SetResetCallback (reset_cb);
	
	rmode = VIDEO_GetPreferredMode(NULL);
	
	xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));

	VIDEO_Configure(rmode);

	VIDEO_ClearFrameBuffer(rmode, xfb, COLOR_BLACK);
	VIDEO_SetNextFramebuffer(xfb);

	VIDEO_SetBlack(FALSE);

	VIDEO_Flush();

	VIDEO_WaitVSync();
	if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
    VIDEO_ClearFrameBuffer(rmode, xfb, COLOR_BLACK);
	
	u32 screenheight = rmode->xfbHeight;
	
	DrawBackground(rmode);
	
	*(vu32*)0xCD00643C = 0x00000000;	// 32Mhz on Bus

	// TODO
	/*
	geckoattached = usb_isgeckoalive(EXI_CHANNEL_1);

	if (geckoattached)
	{
		// Confirm if it's really an usb gecko 
		write_font(80, 150, "Press A to confirm that you have an");
		write_font(80, 174, "usb gecko in memory card slot 2");

		waitforbuttonpress(&pressed, &pressedGC);
		if (pressed != WPAD_BUTTON_A && pressedGC != PAD_BUTTON_A)
		{
			geckoattached = 0;
		}
	}

	if(!geckoattached)
	{	
		drawicon(xfb, 22, screenheight - 63, 32/2, 27,0);
		VIDEO_WaitVSync();
	}
	else 
	{
		drawicon(xfb, 22, screenheight - 63, 32/2, 27,1);
		usb_flush(EXI_CHANNEL_1);
		VIDEO_WaitVSync();
	}
	*/
	drawicon(xfb, 22, screenheight - 63, 32/2, 27,0);
	VIDEO_WaitVSync();

	if (IOS_GetVersion() == 249)
	{
		if (IOS_GetRevision() == 8 || IOS_GetRevision() == 12 || IOS_GetRevision() == 15)
		{
			write_font(80, 140, "Your cIOS has revison %u", IOS_GetRevision());
			write_font(80, 164, "If you get 001 error, it's the");
			write_font(80, 188, "cIOS' fault");
			
			sleep(2);
			write_font(80, 236, "Press any button to continue");
			
			waitforbuttonpress(NULL, NULL);
		} else
		if (IOS_GetRevision() == 16)
		{
			write_font(80, 140, "Your cIOS has revison %u", IOS_GetRevision());
			write_font(80, 164, "Happy timeouts...");
			
			sleep(2);
			write_font(80, 236, "Press any button to continue");
			
			waitforbuttonpress(NULL, NULL);
		} else
		if (IOS_GetRevision() != 14 && IOS_GetRevision() != 13)
		{
			write_font(80, 140, "Your cIOS has revison %u", IOS_GetRevision());
			write_font(80, 164, "To achieve best compatibility");
			write_font(80, 188, "use revision 13a(!) or 14");
			//write_font(80, 212, "");
			
			//sleep(2);
			write_font(80, 236, "Press any button to continue");
			
			waitforbuttonpress(NULL, NULL);
		}
	}

	clearmenu();
	
	drawmenu(0);
	drawselected(0);
	
	write_font(80, screenheight - 60, "NeoGamma R8 beta 17, IOS%u (Rev %u)", IOS_GetVersion(), IOS_GetRevision());
	
	/* Stop Drive */
	di_error_code = dvd_openios();
	di_error_code = dvd_stopdrive();
	dvd_closeios();
	
	/* Main Loop */
	while(1)
	{
		/*
		if(geckoattached)
		{
		}	
		*/

		processwpad();
	}

	return 0;
}
