//initexit.cpp
//Initialize everything for the game to run and dispose everything 
//after the game has terminated

#include <stdio.h>
#include "fpu_exc.h"
#include "screen.h"
#include "fileio.h"
#include "textures.h"
#include "models.h"
#include "text.h"
#include "gamesound.h"
#include "random.h"
#include "controls.h"
#include "network.h"
#include "config.h"
#include "environment.h"
#include "interface.h"
#include "writeout.h"
#include "music.h"
#include "interfaceutil.h"
#include "gametime.h"
#include "error.h"
#include "gamesystem.h"
#include "initexit.h"
#include "tracker.h"
#include "transparency.h"
#include "log.h"

#include "stdtypes.h"
#include "reg_tool_3.h"
#include "rt3_redline.h"
#include "ASWRegistrationCarbonAPI.h"
#include "file_tool.h"
#include "localtracker.h"

int IsInFront();

void ShowRegistrationScreen()
{
	HandleError(RT3_Open(true,VERSION_3_CODES,RT3_PRODUCT_NAME,"Redline"));
	Bool8 launched;
	UInt64 code=RT3_GetLicenseCode();
/*	int test=false;
	qRT3_LicenseIsSameCode(code,RT3_PIRATED_CODE_01,test);
	printf("license test: %d\n",test);*/

	int valid=true;
	int invalid=false;
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_CODE_01,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_CODE_02,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_03,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_04,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_05,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_06,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_07,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_08,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_09,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_10,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_11,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_12,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_13,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_14,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_15,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_16,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_17,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_18,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_19,invalid);
	if(!invalid)qRT3_LicenseIsSameCode(code,RT3_PIRATED_FAKE_20,invalid);
	valid=!invalid;
	if(!valid)
		FT_FileDelete(kPathRefPreferences, "Redline License");
	if(!RT3_IsRegistered())
	{
		ASWReg_Initialization();
		ASWReg_ShowDialog(true);
	}
/*	if(RT3_DisplayNotice(!valid,&launched)==nsvErr)
	{
		short hit;
		AlertStdAlertParamRec alertParam={
			false,false,nil,
			"\pExit",
			nil,
			nil,
			kAlertStdAlertOKButton,
			0,
			kWindowDefaultPosition};
		StandardAlert(kAlertStopAlert,
			"\pCan't find Register Redline app.",
			"\pMake sure the Register Redline application is in the same folder as Redline, or reinstall Redline.",
			&alertParam,
			&hit);
			ExitToShell();
	}
	if(launched)
		ExitToShell();
	if(!IsInFront())
	{
		gSystemSuspended=true;
	}	*/
}

void CarSelectionDrawCar(tFileRef carRef,float time,int addOns,int color);

void Init()
{	
	SystemInit();
	//EnableFPUExceptions();
	ShowRegistrationScreen();
	FileInitIO();

	ConfigInit();
	InitTransparency();
	LogInit();
	NetworkInit();
	TrackerStartVersionCheck();
	ScreenInit();
	InitLocalTracker();

	InterfaceFadeInImageFromBlack(0.291,0.57,FileGetReference("ambrosia.pct"),0.6);
	MusicInit();
	float startTime=TimeGetSeconds();
	ControlInit();
	SoundInit();
	LoadAllSounds();
	RandomInit();


	TextLoadFont(FileGetReference("test.font"));
	LoadEnvironment(FileGetReference("showroom.senv"));
	while(!(TimeGetSeconds()>startTime+1));	
	InterfaceFadeInImageFromImage(0.291,0.57,FileGetReference("ambrosia.pct"),1,1,FileGetReference("logo.pct"),1.0);
//	TexturesSelectTex(FileGetReference("fence1.tif"));
//	while(!(TimeGetSeconds()>startTime+3));	
	InterfaceInit();
	if(gFileTampered)
	{
		InterfaceDisplayMessage(-1,"Data files corrupted.","Please re-install Redline.");
		Exit();
	}
	FlushKeys();
}

void Exit()
{
	SoundSilence();
	MusicStopSong();
	ControlExit();
	NetworkStopAdvertising();
	NetworkExit();
	WriteOutFile(FileGetReference(kConfigFileName),gConfig,kParserTypeConfigDesc);
	ScreenExit();
	RT3_Close();
	SystemExit();
	ExitToShell();
}