local script = globalObject;

local interface;
local commandMap = {};

local delay = 0;

local strBack = "char";
local currentang = 180;
local currentang2 = 0;

local cars = {};
cars[1]   = "bourak";
cars[2]   = "futuro";
cars[3]   = "lambo";
cars[4]   = "police";
cars[5]   = "firetruck";
cars[6]   = "towtruck";

local gun_price = 500;
local rocket_price = 600;
local mine_price = 700;

local cps = {};

cps[1] = {};
cps[1][1] = 50;
cps[1][2] = 50;
cps[1][3] = 50;
cps[1][4] = 0;

cps[1][5] = 60;
cps[1][6] = 120;
cps[1][7] = 180;

cps[2] = {};
cps[2][1] = 50;
cps[2][2] = 75;
cps[2][3] = 75;
cps[2][4] = 2000;

cps[2][5] = 60;
cps[2][6] = 120;
cps[2][7] = 180;


cps[3] = {};
cps[3][1] = 50;
cps[3][2] = 100;
cps[3][3] = 100;
cps[3][4] = 2500;

cps[3][5] = 60;
cps[3][6] = 120;
cps[3][7] = 180;


cps[4] = {};
cps[4][1] = 75;
cps[4][2] = 100;
cps[4][3] = 75;
cps[4][4] = 3000;

cps[4][5] = 60;
cps[4][6] = 120;
cps[4][7] = 180;

cps[5] = {};
cps[5][1] = 100;
cps[5][2] = 50;
cps[5][3] = 75;
cps[5][4] = 3500;

cps[5][5] = 60;
cps[5][6] = 240;
cps[5][7] = 180;


cps[6] = {};
cps[6][1] = 100;
cps[6][2] = 75;
cps[6][3] = 75;
cps[6][4] = 4000;

cps[6][5] = 60;
cps[6][6] = 120;
cps[6][7] = 180;

local currentcar = 1;

local incenter = false;


local locations = {};
locations[1] = 1;
locations[2] = 2;
locations[3] = 3;
locations[4] = 4;
locations[5] = 5;
locations[6] = 6;
locations[7] = 1;
locations[8] = 2;
locations[9] = 3;
locations[10] = 4;
locations[11] = 5;
locations[12] = 6;
locations[13] = 1;
locations[14] = 2;
locations[15] = 3;
locations[16] = 4;
locations[17] = 5;
locations[18] = 6;
locations[19] = 1;
locations[20] = 2;
locations[21] = 3;
locations[22] = 4;
locations[23] = 5;
locations[24] = 6;

local types = {};
types[1] = 1;
types[2] = 0;
types[3] = 1;
types[4] = 0;
types[5] = 1;
types[6] = 0;

types[7] = 0;
types[8] = 1;
types[9] = 0;
types[10] = 1;
types[11] = 0;
types[12] = 1;

types[13] = 0;
types[14] = 1;
types[15] = 0;
types[16] = 1;
types[17] = 0;
types[18] = 1;

types[19] = 1;
types[20] = 0;
types[21] = 1;
types[22] = 0;
types[23] = 1;
types[24] = 0;

local lock_button = false;
local lock_time = 0;
local lock_show = false;

local function UpdateMissionInfo()
	local current = GetCurrentRace();
	for i = 1,24
	do
			SetWindowVisible(interface, "_level"..i, false);
	end
	SetWindowVisible(interface, "_gold", false);
	SetWindowVisible(interface, "_silver", false);
	SetWindowVisible(interface, "_bronze", false);
	if (GetMedalType(current) == 3)
	then
		SetWindowVisible(interface, "_gold", true);
	elseif (GetMedalType(current) == 2)
	then
		SetWindowVisible(interface, "_silver", true);
	elseif (GetMedalType(current) == 1)
	then
		SetWindowVisible(interface, "_bronze", true);
	end	
	SetWindowVisible(interface, "_level"..current, true);
	SetText(interface, "race_number_txt", current.."/24");
	SetTextString(interface, "location_txt", "road"..locations[current]);
	SetTextString(interface, "race_type_txt", "race_type"..types[current]);
	
end



local function UpdateInOut()
	if (not incenter)
	then
		if (IsCarInGarage(currentcar))
		then
			SetWindowVisible(interface, "center", true);
			SetWindowVisible(interface, "ingarage", true);
			SetWindowVisible(interface, "buy", false);
			SetWindowVisible(interface, "notenough", false);
			SetText(interface, "price", "");
		else
			SetWindowVisible(interface, "center", false);
			SetWindowVisible(interface, "ingarage", false);
			SetText(interface, "price", "$"..cps[currentcar][4]);
			if (GetMoney() >= cps[currentcar][4])
			then
				SetWindowVisible(interface, "buy", true);
				SetWindowVisible(interface, "notenough", false);
			else
				SetWindowVisible(interface, "buy", false);
				SetWindowVisible(interface, "notenough", true);
			end
		end
	end
	
	if (IsCarInGarage(currentcar))
	then
--		SetWindowVisible(interface, "arrow2", true);
		SetWindowVisible(interface, "race", true);
		SetWindowVisible(interface, "race_fail", false);
		SetWindowVisible(interface, "lock", false);
		SetText(interface, "sel_car", "");
		lock_button = false;
	else	
--		SetWindowVisible(interface, "arrow2", false);
		SetWindowVisible(interface, "race", false);
		SetWindowVisible(interface, "race_fail", true);
	--	SetWindowVisible(interface, "lock", true);
	--	SetTextString(interface, "sel_car", "sel_car");
		lock_button = true;
	end
	
	SetText(interface, "credits", "$"..string.format("%0.5d", GetMoney()));
	UpdateInterfaceBuffers(interface);
end

local function UpdateWeapons()
	if (incenter)
	then
			SetWindowVisible(interface, "weapons", true);
			if (IsWeaponMounted(currentcar, 1))
			then
				SetWindowVisible(interface, "notenough1", false);
				SetWindowVisible(interface, "mounted1", true);
				SetWindowVisible(interface, "buy1", false);
				SetText(interface, "price1", "");
			else
				SetText(interface, "price1", "$"..gun_price);
				SetWindowVisible(interface, "mounted1", false);
				if (GetMoney() >= gun_price)
				then
					SetWindowVisible(interface, "buy1", true);
					SetWindowVisible(interface, "notenough1", false);
				else
					SetWindowVisible(interface, "buy1", false);
					SetWindowVisible(interface, "notenough1", true);
				end
			end
			
			if (IsWeaponMounted(currentcar, 2))
			then
				SetWindowVisible(interface, "notenough2", false);
				SetWindowVisible(interface, "mounted2", true);
				SetWindowVisible(interface, "buy2", false);
				SetText(interface, "price2", "");
			else
				SetText(interface, "price2", "$"..rocket_price);
				SetWindowVisible(interface, "mounted2", false);
				if (GetMoney() >= rocket_price)
				then
					SetWindowVisible(interface, "buy2", true);
					SetWindowVisible(interface, "notenough2", false);
				else
					SetWindowVisible(interface, "buy2", false);
					SetWindowVisible(interface, "notenough2", true);
				end
			end
			
			if (IsWeaponMounted(currentcar, 3))
			then
				SetWindowVisible(interface, "notenough3", false);
				SetWindowVisible(interface, "mounted3", true);
				SetWindowVisible(interface, "buy3", false);
				SetText(interface, "price3", "");
			else
				SetText(interface, "price3", "$"..mine_price);
				SetWindowVisible(interface, "mounted3", false);
				if (GetMoney() >= mine_price)
				then
					SetWindowVisible(interface, "buy3", true);
					SetWindowVisible(interface, "notenough3", false);
				else
					SetWindowVisible(interface, "buy3", false);
					SetWindowVisible(interface, "notenough3", true);
				end
			end
			
	else
			SetWindowVisible(interface, "weapons", false);
			SetWindowVisible(interface, "notenough1", false);
			SetWindowVisible(interface, "notenough2", false);
			SetWindowVisible(interface, "notenough3", false);
			SetText(interface, "price1", "");
			SetText(interface, "price2", "");
			SetText(interface, "price3", "");
			SetWindowVisible(interface, "mounted1", false);
			SetWindowVisible(interface, "mounted2", false);
			SetWindowVisible(interface, "mounted3", false);
			SetWindowVisible(interface, "buy1", false);
			SetWindowVisible(interface, "buy2", false);
			SetWindowVisible(interface, "buy3", false);

	end
	SendCommandByName(cars[currentcar].."_mesh", "update_parts", "");
	SetText(interface, "credits", "$"..string.format("%0.5d", GetMoney()));
	UpdateInterfaceBuffers(interface);
end

local function ExecuteCommands()
	while(SetNextCommand(script))
	do
		local command = GetCurrentCommand(script);
		if (commandMap[command])
		then
			commandMap[command]();
		end
	end
end

local function SetCarName()
	for i = 1,6
	do
		SetWindowVisible(interface, cars[i], false);
	end
	SetWindowVisible(interface, cars[currentcar], true);
	
	SetWindowSize(interface, "armor", cps[currentcar][1]/100.0*212.0, 13);
	SetWindowSize(interface, "accel", cps[currentcar][2]/100.0*212.0, 13);
	SetWindowSize(interface, "speed", cps[currentcar][3]/100.0*212.0, 13);
		
	UpdateInterfaceBuffers(interface);
end

local function UpdateCenter()
	if (incenter)
	then
		LockButton(interface, "center");
		SetWindowVisible(interface, "center", false);
		UnlockButton(interface, "out");
		SetWindowVisible(interface, "out", true);
	else
		LockButton(interface, "out");
		SetWindowVisible(interface, "out", false);
		UnlockButton(interface, "center");
		SetWindowVisible(interface, "center", true);
	end
	UpdateInterfaceBuffers(interface);
end
local f1, f2, f3 = false, false, false;

local function Button()
	local name = GetCurrentParams(script);
	if (name == "buy1")
	then
		SetWeaponMounted(currentcar, 1);
		SetMoney(GetMoney() - gun_price);
		UpdateWeapons();
		f1 = false;
	elseif (name == "buy2")
	then
		SetWeaponMounted(currentcar, 2);
		SetMoney(GetMoney() - rocket_price);
		UpdateWeapons();
		f2 = false;
	elseif (name == "buy3")
	then
		SetWeaponMounted(currentcar, 3);
		SetMoney(GetMoney() - mine_price);
		UpdateWeapons();
		f3 = false;
	elseif (name == "buy")
	then
		SetCarInGarage(currentcar);
		SetMoney(GetMoney() - cps[currentcar][4]);
		UpdateInOut();
	elseif (name == "back")
	then
		LockButton(interface, "single");
		LockButton(interface, "champ");
		LockButton(interface, "back");
		SendCommandByName("mainmenu", "set_new_game", "false");
--		Update();
		SendCommandByName("main", "road", ""); 	
	elseif (name == "race")
	 then
		 LockButton(interface, "single");
			LockButton(interface, "champ");
		 LockButton(interface, "back");
--		SendCommandByName("main", "char", "day"); 
 SendCommandByName("main", "set_single", "true");
--		 SendCommandByName("main", "road", "");
Update();
--		SendCommandByName("main", "mission", "mission"..6); 
		Update();
		SetPlayerName(cars[currentcar]);
		SetCurrentCar(currentcar);
		SendCommandByName("main", "load_choice", "");

	elseif (name == "right")
	 then
		currentang = currentang + 60;
		if (not incenter)
		then
			currentcar = currentcar + 1;
			if (currentcar > 6)
			then
				currentcar = 1;
			end
			SetCarName();
		end
		SetDirectParameters(currentang, false);
		UpdateInOut();
	elseif (name == "left")
	 then
		currentang = currentang - 60;
		if (not incenter)
		then
			currentcar = currentcar - 1;
			if (currentcar < 1)
			then
				currentcar = 6;
			end
			SetCarName();
		end
		SetDirectParameters(currentang, false);
		UpdateInOut();
	elseif (name == "center")
	 then
		SendCommandByName(cars[currentcar].."_mesh", "set_active", "");
		SetDirectParameters3(4);
		incenter = true;
		UpdateCenter();
		while (currentang > 360)
		do
			currentang = currentang - 360;
		end
		while (currentang < 0)
		do
			currentang = currentang + 360;
		end
		currentang2 = currentang;
		SetDirectParameters(currentang, true);	
		UpdateWeapons();		
	elseif (name == "out")
	 then
		currentang = currentang2;
		SetDirectParameters(currentang, true);		
		SetDirectParameters2(0, 0, 0);
		SetDirectParameters3(12);
		incenter = false;
		UpdateCenter();
		UpdateWeapons();
	elseif(name == "champ")
	then
		LockButton(interface, "single");
		LockButton(interface, "champ");
		LockButton(interface, "back");
		SendCommandByName("main", "set_single", "false");
		SendCommandByName("main", "char", "day"); 
		--SendCommandByName("main", "road", "");
--~ 		if (GetNumRacesPassed() == 1)
--~ 		then
--~ 			SetLevelTransportCount(3);
--~ 			SetNumRounds(5);
--~ 		elseif (GetNumRacesPassed() == 2)
--~ 		then
--~ 			SetLevelTransportCount(3);
--~ 			SetNumRounds(5);
--~ 		elseif (GetNumRacesPassed() == 3)
--~ 		then
--~ 			SetLevelTransportCount(3);
--~ 			SetNumRounds(5);
--~ 		elseif (GetNumRacesPassed() == 4)
--~ 		then
--~ 			SetLevelTransportCount(3);
--~ 			SetNumRounds(3);
--~ 		elseif (GetNumRacesPassed() == 5)
--~ 		then
--~ 			SetLevelTransportCount(5);
--~ 			SetNumRounds(5);
--~ 		elseif (GetNumRacesPassed() == 6)
--~ 		then
--~ 			SetLevelTransportCount(3);
--~ 			SetNumRounds(5);
--~ 		end
	end
end

local flag = true;
local function MouseOver()
	local name = GetCurrentParams(script);
	if (name == "buy1")
	then
		SendCommandByName(cars[currentcar].."_mesh", "show_part", "1,1");
		f1 = true;
	  currentang = currentang2 + cps[currentcar][5];
		SetDirectParameters(currentang, false);
	elseif (name == "buy2")
	then
		SendCommandByName(cars[currentcar].."_mesh", "show_part", "2,1");
		f2 = true;
	  currentang = currentang2 + cps[currentcar][6];
		SetDirectParameters(currentang, false);
	elseif (name == "buy3")
	then
		SendCommandByName(cars[currentcar].."_mesh", "show_part", "3,1");
		f3 = true;
	  currentang = currentang2 + cps[currentcar][7];
		SetDirectParameters(currentang, false);
	else
		if (f1 and not IsWeaponMounted(currentcar, 1))
		then
			SendCommandByName(cars[currentcar].."_mesh", "show_part", "1,0");
			f1 = false;
		end
		if (f2 and not IsWeaponMounted(currentcar, 2))
		then
			SendCommandByName(cars[currentcar].."_mesh", "show_part", "2,0");
			f2 = false;
		end
		if (f3 and not IsWeaponMounted(currentcar, 3))
		then
			SendCommandByName(cars[currentcar].."_mesh", "show_part", "3,0");
			f3 = false;
		end
	end
end

local function MouseOut()
	if (f1 and not IsWeaponMounted(currentcar, 1))
	then
		SendCommandByName(cars[currentcar].."_mesh", "show_part", "1,0");
		f1 = false;
	end
	if (f2 and not IsWeaponMounted(currentcar, 2))
	then
		SendCommandByName(cars[currentcar].."_mesh", "show_part", "2,0");
		f2 = false;
	end
	if (f3 and not IsWeaponMounted(currentcar, 3))
	then
		SendCommandByName(cars[currentcar].."_mesh", "show_part", "3,0");
		f3 = false;
	end
end 

local function Window()
	local name = GetCurrentParams(script);
	UpdateInterfaceBuffers(interface);
end 

local function MenuUp()
	IMenuRight();
end

local function MenuDown()
	IMenuLeft();
end

local function MenuLeft()
	IMenuLeft();
end

local function MenuRight()
	IMenuRight();
end



local function AddObject()
	local obj = GetCurrentParams(script);
	interface = StringToPointer(obj);
	
	strBack = "mainmenu";
	
	SetWindowVisible(interface, "back_square", false);
	SetWindowVisible(interface, "single_square", false);
	SetWindowVisible(interface, "champ_square", false);
--	SetWindowVisible(interface, "square", true);
	SetWindowVisible(interface, "square", false); 
	
	UnlockButton(interface, "single");
	UnlockButton(interface, "champ");
	UnlockButton(interface, "back");
	
	IMenuRight();
	
	SetCarName();
	UpdateMissionInfo();
	SendCommandByName(cars[currentcar].."_mesh", "update_parts", "");
	SendCommandByName(cars[currentcar].."_mesh", "set_active", "");
	Update();
	if (incenter)
	then
		SetDirectParameters3(4);
		SetDirectParameters(currentang, true);	
	else
		SetDirectParameters2(0, 0, 0);
		SetDirectParameters3(12);
		SetDirectParameters(currentang, true);	
	end
	
	lock_button = false;
	lock_time = 0;
	lock_show = false;
	
	UpdateCenter();
	UpdateInOut();
	UpdateWeapons();

end

local function DeleteObject()
	Release(interface);
	StopScript(script);
	interface = nil;
end

local function ReleaseAll()
	Release(interface);
	interface = nil;
end

commandMap["release_all"] 	 = ReleaseAll;
commandMap["add_object"] 	 = AddObject;
commandMap["delete_object"] = DeleteObject;
commandMap["mouse_button_over"] = MouseOver; 
commandMap["mouse_button_out"]  = MouseOut; 
commandMap["button"] 	= Button;
commandMap["window"] 	= Window;
commandMap["menu_up"] 	= MenuUp;
commandMap["menu_down"]	= MenuDown;
commandMap["menu_left"]	= MenuLeft;
commandMap["menu_right"]	= MenuRight;
commandMap["back"]		= function () strBack = GetCurrentParams(script); end

while(true)
do
	deltaTime = GetDeltaTime();
	if(flag)
	then 
		delay = delay + deltaTime; 
	end
--	if(delay >= 0.5)
--	then
--		SetWindowVisible(interface, "square", true); 
--		UpdateInterfaceBuffers(interface);
--	end

	if (lock_button)
	then
		lock_time = lock_time + deltaTime;
		if ((lock_time >= 0.25 and not lock_show) or (lock_time >= 0.5 and lock_show))
		then
			lock_time = 0;
			lock_show = not lock_show;
			if (lock_show)
			then
					SetWindowVisible(interface, "lock", true);
					SetTextString(interface, "sel_car", "sel_car");
			else
					SetWindowVisible(interface, "lock", false);
					SetText(interface, "sel_car", "");
			end
			UpdateInterfaceBuffers(interface);
			
		end
	end
	ExecuteCommands();
	UpdateInterface(interface);
	Update();
end
