-- Level Manager
-- load the static definitions
dofile("Scripts/levelDefines.lua");
dofile("Scripts/luaDebug.lua");

LevelManager = nil;
LevelManager = {};
LevelManager.__index = LevelManager;

function LevelManager.create()
	local mgr = {};
	setmetatable(mgr, LevelManager);

	mgr.game_list = nil;
	mgr.game_list = {};

	mgr.game_queue = nil;
	mgr.game_queue = {};

	mgr.upgrade_idx = -1;
	
	mgr.table_list = nil;
	mgr.table_list = {};
	
	mgr.guest_list = nil;
	mgr.guest_list = {};

	mgr.seated_list = nil;
	mgr.seated_list = {};

	mgr.candle_list = nil;
	mgr.candle_list = {};

	mgr.level_settings = nil;
	mgr.level_settings = DefaultLevel;

	mgr.match_settings = nil;
	mgr.match_settings = {};

	mgr.background_image = "game_bg";

	return mgr;
end

function LevelManager:AddGame(nType)
	table.insert(self.game_list, nType);
end

function LevelManager:SetGameQueue(gameQueue)
	self.game_queue = gameQueue;
end

function LevelManager:AwardUpgrade(nUpgrade)
	self.upgrade_idx = nUpgrade;
end

function LevelManager:AddTable(nType, xPos, yPos)
	new_table = {}
	new_table.type = nType;
	new_table.x = xPos;
	new_table.y = yPos;

	table.insert(self.table_list, new_table);
end

function LevelManager:AddCandle(xPos, yPos, nDuration)
	new_candle = {}
	new_candle.x = xPos;
	new_candle.y = yPos;
	new_candle.duration = nDuration;

	table.insert(self.candle_list, new_candle);
end

function LevelManager:AddGuest(guestData)
	table.insert(self.guest_list, guestData);
end

function LevelManager.GenGuest(guestType)
	-- return a guest based on its guest type
	for guest_key, guest_value in ipairs(GuestDefinitionList) do
		if guest_value.type == guestType then

			temp_guest = {};

			for key, value in pairs(guest_value) do
				temp_guest[key] = value;
			end

			return temp_guest;
		end
	end

	return {};
end

function LevelManager:SetBaseAttribute(guestTypes, attribute, value)

	-- increment through the guest types to change
	for change_key, change_value in ipairs(guestTypes) do
		
		for guest_key, guest_value in ipairs(self.guest_list) do
			
			if(guest_value.type == change_value) then
				-- we've found the right type of guest to change, now change the attribute
				self.guest_list[guest_key][attribute] = value;
			end
		end
	end
end

function LevelManager:AdjustGuestHappiness(percentage)

	target_pct = tonumber(percentage)/100;

	-- increment through the guest list
	for guest_key, guest_value in ipairs(self.guest_list) do
		guest_value.happiness = tonumber(guest_value.happiness) * target_pct;		
	end
end

function LevelManager:AddSeated(seatedData)
	table.insert(self.seated_list, seatedData);
end

function LevelManager.GenSeated(seatedType)
	-- return a seated based on its seated type
	for seated_key, seated_value in ipairs(SeatedDefinitionList) do
		if seated_value.type == seatedType then

			temp_seated = {};

			for key, value in pairs(seated_value) do
				temp_seated[key] = value;
			end

			return temp_seated;
		end
	end

	return {};
end

-- Level Settings Functionality
function LevelManager:SetBackgroundImage(imgString)
	self.background_image = imgString;
end

function LevelManager:GetBackgroundImage()
	return self.background_image;
end

function LevelManager:SetLevelNumber(map,level)
	self.level_settings.map = map;
	self.level_settings.level = level;
end

function LevelManager:SetLevelScores(passScore, highScore)
	self.level_settings.passScore = passScore;
	self.level_settings.highScore = highScore;
end

function LevelManager:SetLevelTime(totalTime)
	self.level_settings.totalTime = totalTime;
end

function LevelManager:SetSnackTime(time)
	self.level_settings.snackTime = time;
end

function LevelManager:SetSpecialRequestTime(totalTime)
	self.level_settings.specialRequestTime = totalTime;
end

function LevelManager:SetHostQueue(max)
	self.level_settings.hostMaxQueue = max;
end

function LevelManager:AddPodium()
	self.level_settings.usePodium = 1;
end

function LevelManager:RemovePodium()
	self.level_settings.usePodium = 0;
end

function LevelManager:SetBarBreakTime(time)
	self.level_settings.barBreakTime = time;
end

function LevelManager:SetBaseFixTime(time)
	self.level_settings.baseFixTime = time;
end

function LevelManager:SetMaxChemistry(fChem)
	self.level_settings.maxChemistry = fChem;
end

function LevelManager:AddTutorial(firstStep, lastStep, guestLock)
	new_tutorial = {}

	if((firstStep ~= nil) and (firstStep ~= 0)) then
		new_tutorial.firststep = firstStep;
	end

	new_tutorial.steps = lastStep;
	
	if(guestLock == true) then
		new_tutorial.guestlock = 1;
	else
		new_tutorial.guestlock = 0;
	end

	self.level_settings.tutorial = new_tutorial;
end

-- Match Round Settings
function LevelManager:CreateHeart(nId)
	emptyHeart = {
		id = nId,
		rules = {}, 
		matches = {},
	} 
	
	table.insert(self.match_settings, emptyHeart);
end

function LevelManager:AddRule(nId, nType, nParam, nWho)
	new_rule = {}
	new_rule.type = nType;
	new_rule.param = nParam;
	new_rule.who = nWho;

	TraceToLog("<<<<<<<<LevelManager:AddRule");
	--TraceToLog(string.format("type = %d", nType));
	--TraceToLog(string.format("param = %d", new_rule.param));
	--TraceToLog(string.format("who = %d", new_rule.who));

	for heart_key, heart_value in ipairs(self.match_settings) do
		
		if(heart_value.id == nId) then
			-- we've found the right heart, insert the rule
			table.insert(self.match_settings[heart_key].rules, new_rule);
		end
	end
end

function LevelManager:AddMatch(nId, nSingle)
	new_match = {}
	new_match.type = nSingle;

	for heart_key, heart_value in ipairs(self.match_settings) do
		
		if(heart_value.id == nId) then
			-- we've found the right heart, insert the match person
			table.insert(self.match_settings[heart_key].matches, new_match);
		end
	end
end

-- Generators
function LevelManager:GenerateGameObject()
	game_object = {}

	-- Add the game list
	game_object.gameList = self.game_list;

	-- Add the game queue if any
	game_object.gameQueue = self.game_queue;

	-- Add the table list
	game_object.availableTables = self.table_list;

	-- Add the candle list
	game_object.availableCandles = self.candle_list;

	-- Randomize the guest list before you set it ?

	-- Now add the guest list
	game_object.guestList = self.guest_list;

	-- Now add the seated list
	game_object.seatedList = self.seated_list;

	-- Now add the match settings
	game_object.matchSettings = self.match_settings;

	-- Add the upgrade selection
	game_object.upgradeIdx = self.upgrade_idx;

	-- dump to log
	-- PrintTabTable(game_object);

	-- clear the tutorial steps
	TutorialSteps =
	{ 
		start = {
			action = "", 
			triggers = {},
		} 
	};
	
	return game_object;
end

function LevelManager:GenerateLevelSettings()
	return self.level_settings;
end