-------------------------------------------------------------------------------------------------
---- Tweak Overrides
-------------------------------------------------------------------------------------------------

Tweaks.daysoff = false;
Tweaks.citybusinessesownable = false;
People.subwaythreshold = 5;

-------------------------------------------------------------------------------------------------
---- Local variables.
-------------------------------------------------------------------------------------------------
l_overdraft=-750;	-- Cash limit for the player; they cannot spend beyond this amount
l_targetvalue = 200;
l_sales = 0;
l_gameending = false;
l_allinprofit = false;
l_outofstock = false;
l_donenightwarning=false;
l_donestockwarning=false;
l_donerestockwarning=false;
l_donecashwarning=false;

-- these two numbers are passed through to the prompt box
-- so that we know what to do after the box has been dismissed.
l_failurecode = 998;
l_successcode = 999;

-------------------------------------------------------------------------------------------------
-- INITIALISE_Level. Standard level event
-------------------------------------------------------------------------------------------------
function EVENTINITIALISE_Level()
	Game.Pause();
	-- load the building and business file
	C_RunScript("scripts\\level3\\map.lua");
	C_RunScript("scripts\\level3\\initialpref.lua");
end;

-------------------------------------------------------------------------------------------------
-- Level. Standard level event.
-------------------------------------------------------------------------------------------------
function EVENT_Level()
	-- create an initial prompt
	Interface.CreatePrompt("Level 1", "Welcome to Monopoly City. In this level you must make 200 sales in any one day, with all of your stores turning a profit.", 2);
	Interface.PositionPrompt(175,105);
	SetCamPos(72.2,758,-146.4,24.5,0.29,88.9);
	Camera.posinertia = FALSE;
	Camera.targinertia = FALSE;
	Camera.inertiavalue = 1;
	-- TODO:: After 2 real minutes - Check if Demand screen has been used - if not pop up advice.
	-- TODO:: After 4 real minutes - Check if a building has been erected - if not pop up advice.
	C_TriggerEvent("LevelAboutToFail", ((GAME_YEAR*2)-GAME_DAY));
	C_TriggerEvent("LevelFailed", GAME_YEAR*2);
	C_TriggerEvent("IntroduceBusiness", GAME_DAY, 1);
end;

-------------------------------------------------------------------------------------------------
-- LevelAboutToFail. Called one day before time limit, to give player a warning
-------------------------------------------------------------------------------------------------
function EVENT_LevelAboutToFail()
	if (l_sales < l_targetvalue) and (l_allinprofit == FALSE) then
		Interface.CreatePrompt("Level 1 Update", "It's the last day! You must make at least 200 sales today and ensure all of your businesses are in profit!");
	elseif (l_sales < l_targetvalue) and (l_allinprofit == TRUE) then
		Interface.CreatePrompt("Level 1 Update", "It's the last day! All of your stores made a profit yesterday, but you must make at least 200 sales today!");
	elseif (l_sales >= l_targetvalue) and (l_allinprofit == FALSE) then
		Interface.CreatePrompt("Level 1 Update", "It's the last day! You are currently making enough sales, but you must ensure that all of your businesses are in profit!");
	end;
end;

-------------------------------------------------------------------------------------------------
-- LevelFailed. Called when the time limit for the level has expired
-------------------------------------------------------------------------------------------------
function EVENT_LevelFailed()
	-- TODO :: Check whether the player has any businesses, and advise them to sell them off..... (Not for this level any more, but needed later I'm sure
	-- TODO :: It would be cool to log the players best daily sales figure to show them at the end of the game.
	if (l_sales < l_targetvalue) and (l_allinprofit == FALSE) then
		Interface.CreatePrompt("Level 1 Failed", "You didn't manage to sell 200 units and keep all of your stores in profit on the same day. Try again and see if you can do better.", l_failurecode);
	elseif (l_sales < l_targetvalue) and (l_allinprofit == TRUE) then
		Interface.CreatePrompt("Level 1 Failed", "Although all of your stores were in profit, you failed to make 200 sales. Try again. This time you will need to attract more customers.", l_failurecode);
	elseif (l_sales >= l_targetvalue) and (l_allinprofit == FALSE) then
		Interface.CreatePrompt("Level 1 Failed", "Although you met your target of 200 sales, not all of your businesses were in profit. Try again, but this time you need to keep a closer eye on your businesses performance.", l_failurecode);
	end;
end;

-------------------------------------------------------------------------------------------------
-- LevelWon. Called when the player has achieved his objectives. Triggered from HorlyUpdate
-------------------------------------------------------------------------------------------------
function EVENT_LevelWon()
	Interface.CreatePrompt("Level 1 Completed", "Well done! You have won!!! You should move on to the next level now. You should find it more of a challenge than this one.", l_successcode);
end;


-------------------------------------------------------------------------------------------------
-- OutOfStock. Called the first time a player's business runs out of stock
-------------------------------------------------------------------------------------------------
function EVENT_OutOfStock()
	Interface.CreatePrompt("Trouble With Your Business?", "One of your businesses has run out of stock! To maximise your sales ability, you should open the business control panel and set your stock levels a little higher. (You might even consider building a bigger store!) To access the business controls, double-click on your store.");
	l_donestockwarning=TRUE;
end;

-------------------------------------------------------------------------------------------------
-- LowOnCash. Called the first time a player spends too much money
-------------------------------------------------------------------------------------------------
function EVENT_LowOnCash()
	Interface.CreatePrompt("Low on cash!", "Your bank balance doesn't look very healthy! You will lose this level if you dip below $"..l_overdraft.."!" );
	l_donecashwarning=TRUE;
end;

-------------------------------------------------------------------------------------------------
-- NightWarning. Called the first time a player gets past midnight
-------------------------------------------------------------------------------------------------
function EVENT_NightWarning()
	Interface.CreatePrompt("Don't Panic!", "Don't be alarmed! Every night at about this time, all of your stores do a stock-check. If any stock is needed, it will be bought and so that's why your bank balance went downwards." );
	l_donenightwarning=TRUE;
end;

-------------------------------------------------------------------------------------------------
-- IntroduceBusiness. Called every 2 days to introduce a new business.
-------------------------------------------------------------------------------------------------
function EVENT_IntroduceBusiness(a)
	if a == 1 then
		BusinessInfo[FURNITURE_STORE].available = true;
		Interface.CreatePrompt("New Business Available", "You are now able to create a Furniture Store! Check the Business Browser for information.");
	elseif a == 2 then
		BusinessInfo[RESTAURANT].available = true;
		Interface.CreatePrompt("New Business Available", "You are now able to create a Restaurant! Check the Business Browser for information.");
	elseif a == 3 then
		BusinessInfo[THEATRE].available = true;
		Interface.CreatePrompt("New Business Available", "You are now able to create a Theatre! Check the Business Browser for information.");
	end;

	C_TriggerEvent("IntroduceBusiness", GAME_DAY*2, a+1);
end;

-------------------------------------------------------------------------------------------------
-- GetTotalSales. Goes through all of player1's businesses, and adds up the total sales
-------------------------------------------------------------------------------------------------
function GetTotalSales()
	sales = 0;
	local f = function(i,v)
		sales = sales + v:GetTotalSales();
	end;
	C_Iterate(g_playerbusinesses, f);
	return sales;
end;

--------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------  Catch game events  ------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------
-- InterfacePromptDismiss. Called everytime the player closes the prompt box.
-------------------------------------------------------------------------------------------------
function CATCHEVENT_InterfacePromptDismiss(a)
	if a == 1 then
		Interface.CreatePrompt("Level 1", "To do this you will need to build some shops. Be sure to build shops that supply what the people want!.", 2);
		Interface.PositionPrompt(175,105);
	elseif a == 2 then

		Interface.CreatePrompt("Level 1", "You can build on either of the following city blocks...", 3);
		Interface.PositionPrompt(175,105);
	elseif a == 3 then
		Interface.CreatePrompt("Level 1", "New York Avenue...", 4);
		Interface.PositionPrompt(175,105);
		SetCamPos(95,183,-20,202,0.29,143);
		Camera.posinertia = TRUE;
		Camera.targinertia = TRUE;
		Camera.inertiavalue = 20;
	elseif a == 4 then
		Interface.CreatePrompt("Level 1", "or Marvin Gardens.", 5);
		Interface.PositionPrompt(175,105);
		SetCamPos(-72.7,170.9,-186.3,-208,0.25,-49.6);
		Camera.posinertia = TRUE;
		Camera.targinertia = TRUE;
		Camera.inertiavalue = 20;
	elseif a == 5 then
		Interface.CreatePrompt("Level 1", "Shops already exist in the city, so you will have to lure customers away from them and make them use your stores instead.", 6);
		Interface.PositionPrompt(175,105);
		SetCamPos(72.2,758,-146.4,24.5,0.29,88.9);
		Camera.posinertia = TRUE;
		Camera.targinertia = TRUE;
		Camera.inertiavalue = 20;
	elseif a == 6 then
		Interface.CreatePrompt("Level 1", "You only have access to a few business types, so check the Business Browser to see which ones are available. Good Luck!", 7);
		Interface.PositionPrompt(175,105);
	elseif a == 7 then
		Interface.CreatePrompt("Level 1", "Oh, by the way. You must achieve your objective before 1932! Better get a move on!", 8);
		Interface.PositionPrompt(175,105);
		SetCamPos(-4.515,457.8,-279.39,-4.515,262.877,-179.39);
		Camera.posinertia = TRUE;
		Camera.targinertia = TRUE;
		Camera.inertiavalue = 20;
	elseif a == 8 then
		Game.Unpause();
	elseif a == l_failurecode then
		Game.End(false);
	elseif a == l_successcode then
		Game.End(true);
	end;
end;

-------------------------------------------------------------------------------------------------
-- HourlyUpdate event. Called everytime the game calendar ticks another hour over.
-------------------------------------------------------------------------------------------------
function CATCHEVENT_HourlyUpdate(a) -- takes the current hour as an argument
	-- TODO :: need to check bankruptcy against GetWorth, instead of GetMoney.

	if (Player.GetMoney() <= 100) and (Player.GetMoney() >= l_overdraft) then
		if l_donecashwarning==false then
			EVENT_LowOnCash();
		end;
	end;	

	if (Player.GetMoney() < l_overdraft) and (l_gameending==false) then
		Interface.CreatePrompt("Level 1 Failed", "Oh dear, oh dear. What happened here? You're bankrupt! You are going to have to be more careful with your money next time. Still, you can always try again - it's only Monopoly money after all. Thank goodness!", l_failurecode);
		l_gameending=true;
		return 0;
	end;	

	l_sales = GetTotalSales(1);

	l_allinprofit = false;
	l_outofstock = false;

	local f = function(i, v)
		if v:GetCommodityStockByIndex(0) == 0 then
			l_outofstock = true;
		end;

		if v:GetProfit() <= 0 then
			l_allinprofit = false;
			return true;
		else
			l_allinprofit = true;
		end;
	end;

	C_Iterate(g_playerbusinesses, f);

--	for i,v in g_playerbusinesses do
--		if v:GetCommodityStockByIndex(0) == 0 then
--			l_outofstock = true;
--			break;
--		else
--			l_outofstock = false;
--		end;
--	end;
--
--	for i,v in g_playerbusinesses do
--		if v:GetProfit() <= 0 then
--			l_allinprofit = false;
--			break;
--		else
--			l_allinprofit = true;
--		end;
--	end;

	if (l_outofstock==true) and (l_donestockwarning==false) then
		EVENT_OutOfStock();
	end;

	if (a==1) and (l_donenightwarning==false) then
		EVENT_NightWarning();
	end;

	if (l_sales >= l_targetvalue) and (l_allinprofit) and (l_gameending==false) then
		EVENT_LevelWon();
		l_gameending=true;
	end;
end;

		


