--
-- Alterac und Arathi Battleground Mods
--
-- based on v1.0 from LeoLeal,  thanks
-- modivied by Nitram to v1.2 with auto turn-In for alterac valley
-- modivied by Nitram to v1.4 with Arathi Basin Time to End Bar, bugfixes etc.
--

LVBM.AddOns.Battlegrounds = { 
	["Name"] = "Battlegrounds", 
	["Abbreviation1"] = "bg", 
	["Version"] = "1.4", 
	["Author"] = "Nitram + LeoLeal", 
	["Description"] = "Shows timer bars for Alterac Valley and Arathi Basin flag captures. Enable auto turn-in's for Alterac Valley.", 
	["Instance"] = LVBM_OTHER, 
	["GUITab"] = LVBMGUI_TAB_OTHER, 
	["Sort"] = 0, 
	["Options"] = {
		["Enabled"] = true, 
		["Announce"] = true, 
		["AVAutoTurnIn"] = true, 
	}, 
	["DropdownMenu"] = {
		[1] = {
			["variable"] = "LVBM.AddOns.Battlegrounds.Options.AVAutoTurnIn", 
			["text"] = LVBM_BGMOD_LANG.AVTURNININFO, 
			["func"] = function() LVBM.AddOns.Battlegrounds.Options.AVAutoTurnIn = not LVBM.AddOns.Battlegrounds.Options.AVAutoTurnIn; end, 
		}, 
	}, 
	["Events"] = {
		["CHAT_MSG_MONSTER_YELL"] = true,
		["CHAT_MSG_BG_SYSTEM_NEUTRAL"] = true,
		["CHAT_MSG_BG_SYSTEM_ALLIANCE"] = true, 
		["CHAT_MSG_BG_SYSTEM_HORDE"] = true, 
		["PLAYER_ENTERING_WORLD"] = true, 
		["GOSSIP_SHOW"] = true, 			-- Turn In
		["QUEST_PROGRESS"] = true, 			-- Turn In
		["QUEST_COMPLETE"] = true, 			-- Turn In
--		["UPDATE_BATTLEFIELD_SCORE"] = true, 
--		["UPDATE_BATTLEFIELD_STATUS"] = true,
	}, 	
	["GetItemName"] = function(bag,  slot)
		local itemlink;
		if (bag == -1) then	itemlink = GetInventoryItemLink("player",  slot);
		else			itemlink = GetContainerItemLink(bag,  slot);
		end
		if (itemlink) then
			local _,  _,  name = string.find(itemlink,  "^.*%[(.*)%].*$");
			return name;
		else	return "";
		end
	end, 
	["GetItemCount"] = function(ItemName)
		local anzl,  bagNr,  bagSlot = 0;
		for bagNr = 0,  10,  1 do
			for bagSlot = 1,  GetContainerNumSlots(bagNr),  1 do
				if( LVBM.AddOns.Battlegrounds.GetItemName(bagNr,  bagSlot) == ItemName) then
					local _,  itemCount = GetContainerItemInfo(bagNr,  bagSlot);
					anzl = anzl + itemCount;
				end
			end
		end
		return anzl;
	end, 
	["ABGetScore"] = function()		-- LVBM.AddOns.Battlegrounds.ABGetScore
		if( GetZoneText() == LVBM_BGMOD_LANG.ABZONE ) then
			_, mAllyInfo = GetWorldStateUIInfo(1);
			_, mHordeInfo = GetWorldStateUIInfo(2);
			if( not mAllyInfo or not mHordeInfo ) then return false; end

			_, _, AllyBases, AllyScore = string.find(mAllyInfo, LVBM_BGMOD_LANG.ABSCOREEXP);
			_, _, HordeBases, HordeScore = string.find(mHordeInfo, LVBM_BGMOD_LANG.ABSCOREEXP);
			if( not AllyBases or not AllyScore or not HordeBases or not HordeScore) then return false; end

			AllyBases	= tonumber( AllyBases );
			AllyScore	= tonumber( AllyScore );
			HordeBases	= tonumber( HordeBases );
			HordeScore	= tonumber( HordeScore );

			return true, AllyBases, AllyScore, HordeBases, HordeScore;
		else return false;
		end
	end,
	["ABFlagTimes"] = {
		[0] = 999999,
		[1] = 12,
		[2] = 9,
		[3] = 6,
		[4] = 3,
		[5] = 1,
	};
	["ABLastTick"] = {
		["Alliance"] = {
			["Score"] = 0,
			["Time"] = 0,
		},
		["Horde"] = {
			["Score"] = 0,
			["Time"] = 0,
		},
	},
	["OnEvent"] = function(event,  arg1)
		----------------
		-- AB WinTime --
		if( GetZoneText() == LVBM_BGMOD_LANG.ABZONE ) then
			local mOk, AllyBases, AllyScore, HordeBases, HordeScore = LVBM.AddOns.Battlegrounds.ABGetScore();

			if( mOk and LVBM.AddOns.Battlegrounds.ABLastTick["Alliance"] ~= AllyScore ) then
				LVBM.AddOns.Battlegrounds.ABLastTick["Alliance"].Score = AllyScore;
				LVBM.AddOns.Battlegrounds.ABLastTick["Alliance"].Time = time();
			end
			if( mOk and LVBM.AddOns.Battlegrounds.ABLastTick["Horde"] ~= HordeScore ) then
				LVBM.AddOns.Battlegrounds.ABLastTick["Horde"].Score = AllyScore;
				LVBM.AddOns.Battlegrounds.ABLastTick["Horde"].Time = time();
			end
		  	if( mOk and AllyScore < 2000 and HordeScore < 2000 and (AllyBases + HordeBases) > 0 ) then
				NextAllyTick = LVBM.AddOns.Battlegrounds.ABFlagTimes[AllyBases] - (time()-LVBM.AddOns.Battlegrounds.ABLastTick["Alliance"].Time);
				NextHordeTick = LVBM.AddOns.Battlegrounds.ABFlagTimes[HordeBases] - (time()-LVBM.AddOns.Battlegrounds.ABLastTick["Horde"].Time);

				AllyTime = (((2000 - (AllyScore + 10)) / 10) * LVBM.AddOns.Battlegrounds.ABFlagTimes[AllyBases]) + NextAllyTick;
				HordeTime = (((2000 - (HordeScore + 10)) / 10) * LVBM.AddOns.Battlegrounds.ABFlagTimes[HordeBases]) + NextHordeTick;
								
				if( AllyTime > HordeTime ) then
					if( LVBM.GetStatusBarTimerTimeLeft("ABWINALLY") ) then
						LVBM.UpdateStatusBarTimer("ABWINALLY", 0, HordeTime, "ABWINHORDE");

					elseif( LVBM.GetStatusBarTimerTimeLeft("ABWINHORDE") ) then
						LVBM.UpdateStatusBarTimer("ABWINHORDE", 0, HordeTime);
					else
						LVBM.StartStatusBarTimer(HordeTime, "ABWINHORDE");
					end
				else
					if( LVBM.GetStatusBarTimerTimeLeft("ABWINHORDE") ) then
						LVBM.UpdateStatusBarTimer("ABWINHORDE", 0, AllyTime, "ABWINALLY");

					elseif( LVBM.GetStatusBarTimerTimeLeft("ABWINALLY") ) then
						LVBM.UpdateStatusBarTimer("ABWINALLY", 0, AllyTime);
					else
						LVBM.StartStatusBarTimer(AllyTime, "ABWINHORDE");
					end
				end
			end
		else
			-- AB TimeLeft
			LVBM.EndStatusBarTimer("ABWINALLY");
			LVBM.EndStatusBarTimer("ABWINHORDE");
		end
		

		-- ---------------- --
		-- Code from Nitram --
		-- ---------------- --
		if ((event == "GOSSIP_SHOW" or event=="QUEST_PROGRESS") and GetZoneText() == LVBM_BGMOD_LANG.AVZONE) then
			local target = UnitName("target");

			if( target == LVBM_BGMOD_LANG.NPC.SMITHREGZAR or target == LVBM_BGMOD_LANG.NPC.MURGOTDEEPFORGE ) then
				-- Open Quest to Smith or Murgot
				if (LVBM.AddOns.Battlegrounds.GetItemCount(LVBM_BGMOD_LANG.ITEM.ARMORSCRAPS) >= 20) then
					SelectGossipAvailableQuest(1);
				end

			elseif( target == LVBM_BGMOD_LANG.NPC.PRIMALISTTHURLOGA) then
				local anzl = LVBM.AddOns.Battlegrounds.GetItemCount(LVBM_BGMOD_LANG.ITEM.SOLDIERSBLOOD);
				if (anzl >= 5) then		SelectGossipAvailableQuest(2);
				elseif (anzl > 0) then		SelectGossipAvailableQuest(1);
				end

			elseif( target == LVBM_BGMOD_LANG.NPC.ARCHDRUIDRENFERAL) then
				local anzl = LVBM.AddOns.Battlegrounds.GetItemCount(LVBM_BGMOD_LANG.ITEM.STORMCRYSTAL);
				if (anzl >= 5) then		SelectGossipAvailableQuest(2);
				elseif (anzl > 0) then		SelectGossipAvailableQuest(1);
				end

			elseif( target == LVBM_BGMOD_LANG.NPC.STORMPIKERAMRIDERCOMMANDER) then	-- Ram Riders
				if (LVBM.AddOns.Battlegrounds.GetItemCount(LVBM_BGMOD_LANG.ITEM.FROSTWOLFHIDE) > 0) then
					SelectGossipAvailableQuest(1);
				end

			elseif( target == LVBM_BGMOD_LANG.NPC.FROSTWOLFWOLFRIDERCOMMANDER) then	-- Wolf Riders
				if (LVBM.AddOns.Battlegrounds.GetItemCount(LVBM_BGMOD_LANG.ITEM.ALTERACRAMHIDE) > 0) then
					SelectGossipAvailableQuest(1);
				end
			end

		end
		if (event=="QUEST_PROGRESS" and GetZoneText() == LVBM_BGMOD_LANG.AVZONE) then
			local target = UnitName("target");

			if (target == LVBM_BGMOD_LANG.NPC.WINGCOMMANDERJEZTOR 
			   and LVBM.AddOns.Battlegrounds.GetItemCount(LVBM_BGMOD_LANG.ITEM.LIEUTENANTSFLESH) == 0) then		return;

			elseif (target == LVBM_BGMOD_LANG.NPC.WINGCOMMANDERGUSE
			   and LVBM.AddOns.Battlegrounds.GetItemCount(LVBM_BGMOD_LANG.ITEM.SOLDIERSFLESH) == 0) then		return;

			elseif (target == LVBM_BGMOD_LANG.NPC.WINGCOMMANDERMULVERICK
			   and LVBM.AddOns.Battlegrounds.GetItemCount(LVBM_BGMOD_LANG.ITEM.COMMANDERSFLESH) == 0) then		return;

			elseif (target == LVBM_BGMOD_LANG.NPC.WINGCOMMANDERVIPORE
			   and LVBM.AddOns.Battlegrounds.GetItemCount(LVBM_BGMOD_LANG.ITEM.LIEUTENANTSMEDAL) == 0) then		return;

			elseif (target == LVBM_BGMOD_LANG.NPC.WINDCOMMANDERSLIDORE
			   and LVBM.AddOns.Battlegrounds.GetItemCount(LVBM_BGMOD_LANG.ITEM.SOLDIERSMEDAL) == 0) then		return;

			elseif (target == LVBM_BGMOD_LANG.NPC.WINGCOMMANDERICHMAN
			   and LVBM.AddOns.Battlegrounds.GetItemCount(LVBM_BGMOD_LANG.ITEM.COMMANDERSMEDAL) == 0) then		return;

		   	end
			CompleteQuest();

		end
		if (event=="QUEST_COMPLETE" and GetZoneText() == LVBM_BGMOD_LANG.AVZONE) then
			local target = UnitName("target");
			for index,  value in LVBM_BGMOD_LANG.NPC do
				if (target == value) then
					GetQuestReward(0);
					LVBM.AddMsg(LVBM_BGMOD_LANG.THANKS);
				end
			end
		end


		-- ----------------- --
		-- Code from LeoLeal --
		-- modded by Nitram  --
		-- ----------------- --
		if (event == "CHAT_MSG_BG_SYSTEM_ALLIANCE") then
			for index, value in ipairs(LVBM_BGMOD_LANG.AB_TARGETS) do
				if string.find(arg1, value) then
					if string.find(arg1, LVBM_BGMOD_LANG["HASASSAULTED"]) or string.find(arg1, LVBM_BGMOD_LANG["CLAIMSTHE"]) then
						--Initiate a timer w/ value as text string for 60 seconds
						LVBM.EndStatusBarTimer("Horde: "..LVBM.Capitalize(LVBM_BGMOD_EN_TARGET_AB[index]));
						LVBM.StartStatusBarTimer(64,  "Alliance: "..LVBM.Capitalize(LVBM_BGMOD_EN_TARGET_AB[index]));

					elseif string.find(arg1, LVBM_BGMOD_LANG["HASDEFENDEDTHE"]) or string.find(arg1, LVBM_BGMOD_LANG["HASTAKENTHE"]) then
						--clear a timer for value
						if string.find(arg1, LVBM_BGMOD_LANG["HASTAKENTHE"]) then
							LVBM.Announce(string.format(LVBM_BGMOD_LANG.ALLI_TAKE_ANNOUNCE, LVBM.Capitalize(LVBM_BGMOD_LANG.AB_TARGETS[index])));
							oldFlashColor=LVBM.Options.FlashColor;
							LVBM.Options.FlashColor = "blue";
							LVBM.AddSpecialWarning("", false, true);
							LVBM.Options.FlashColor = oldFlashColor;
						end
						LVBM.EndStatusBarTimer("Alliance: "..LVBM.Capitalize(LVBM_BGMOD_EN_TARGET_AB[index]));
						LVBM.EndStatusBarTimer("Horde: "..LVBM.Capitalize(LVBM_BGMOD_EN_TARGET_AB[index]));
					end
				end
			end
			
			-- WSG Capture Flag
			if (string.find(arg1, LVBM_BGMOD_LANG["HASCAPTURED"])) then
				oldFlashColor=LVBM.Options.FlashColor;
				LVBM.Options.FlashColor = "blue";
				LVBM.AddSpecialWarning("", false, true);
				LVBM.Options.FlashColor = oldFlashColor;
				LVBM.StartStatusBarTimer(23, "Flag respawn");
			end
			
			--Initial capture of Snowfall for horde
			if string.find(string.lower(arg1), string.lower(LVBM_BGMOD_EN_TARGET_AV[8])) then
				LVBM.EndStatusBarTimer("Horde: "..LVBM_BGMOD_EN_TARGET_AV[8]);
				LVBM.StartStatusBarTimer(303,  "Alliance: "..LVBM_BGMOD_EN_TARGET_AV[8]);
			end
		elseif (event == "CHAT_MSG_BG_SYSTEM_HORDE") then
			for index, value in ipairs(LVBM_BGMOD_LANG.AB_TARGETS) do
				if string.find(arg1, value) then
					if string.find(arg1, LVBM_BGMOD_LANG["HASASSAULTED"]) or string.find(arg1, LVBM_BGMOD_LANG["CLAIMSTHE"]) then
						--Initiate a timer w/ value as text string for 60 seconds
						LVBM.EndStatusBarTimer("Alliance: "..LVBM.Capitalize(LVBM_BGMOD_EN_TARGET_AB[index]));
						LVBM.StartStatusBarTimer(64, "Horde: "..LVBM.Capitalize(LVBM_BGMOD_EN_TARGET_AB[index]));
					elseif string.find(arg1, LVBM_BGMOD_LANG["HASDEFENDEDTHE"]) or string.find(arg1, LVBM_BGMOD_LANG["HASTAKENTHE"]) then
						--clear a timer for value
						if string.find(arg1, LVBM_BGMOD_LANG["HASTAKENTHE"]) then
							LVBM.Announce(string.format(LVBM_BGMOD_LANG.HORDE_TAKE_ANNOUNCE, LVBM.Capitalize(LVBM_BGMOD_LANG.AB_TARGETS[index])));
							oldFlashColor=LVBM.Options.FlashColor;
							LVBM.Options.FlashColor = "red";
							LVBM.AddSpecialWarning("", false, true);
							LVBM.Options.FlashColor = oldFlashColor;
						end
						LVBM.EndStatusBarTimer("Horde: "..LVBM.Capitalize(LVBM_BGMOD_EN_TARGET_AB[index]));
						LVBM.EndStatusBarTimer("Alliance: "..LVBM.Capitalize(LVBM_BGMOD_EN_TARGET_AB[index]));
					end
				end
			end
			
			-- WSG Capture
			if (string.find(arg1,LVBM_BGMOD_LANG["HASCAPTURED"])) then
				oldFlashColor=LVBM.Options.FlashColor;
				LVBM.Options.FlashColor = "red";
				LVBM.AddSpecialWarning("", false, true);
				LVBM.Options.FlashColor = oldFlashColor;
				LVBM.StartStatusBarTimer(23, "Flag respawn");
			end
		
			--Initial capture of Snowfall for horde
			if string.find(string.lower(arg1), string.lower(LVBM_BGMOD_EN_TARGET_AV[8])) then
				LVBM.EndStatusBarTimer("Alliance: "..LVBM_BGMOD_EN_TARGET_AV[8]);
				LVBM.StartStatusBarTimer(303, "Horde: "..LVBM_BGMOD_EN_TARGET_AV[8]);
			end
			
		-- Alterac Valley Specific Yells
		elseif (event == "CHAT_MSG_MONSTER_YELL") then
			for index,  value in ipairs(LVBM_BGMOD_LANG["AV_TARGETS"]) do
				if string.find(string.lower(arg1), string.lower(value)) then

					if string.find(arg1, LVBM_BGMOD_LANG["UNDERATTACK"]) then
						--Initiate a timer w/ value as text string for 300 seconds
						if string.find(arg1, LVBM_BGMOD_LANG["HORDE"]) then 
							LVBM.EndStatusBarTimer("Alliance: "..LVBM_BGMOD_EN_TARGET_AV[index]);
							LVBM.StartStatusBarTimer(303,  "Horde: "..LVBM_BGMOD_EN_TARGET_AV[index]);
						end
						if string.find(arg1, LVBM_BGMOD_LANG["ALLIANCE"]) then
							LVBM.EndStatusBarTimer("Horde: "..LVBM_BGMOD_EN_TARGET_AV[index]);
							LVBM.StartStatusBarTimer(303,  "Alliance: "..LVBM_BGMOD_EN_TARGET_AV[index]);
						end
					elseif string.find(arg1, LVBM_BGMOD_LANG["BYTHE"]) or string.find(arg1, LVBM_BGMOD_LANG["HASTAKENTHE"]) then
						--clear a timer for value
						if string.find(arg1, LVBM_BGMOD_LANG["ALLIANCE"]) then
							LVBM.Announce(string.format(LVBM_BGMOD_LANG.ALLI_TAKE_ANNOUNCE, value));
							oldFlashColor=LVBM.Options.FlashColor;
							LVBM.Options.FlashColor = "blue";
							LVBM.AddSpecialWarning("", false, true);
							LVBM.Options.FlashColor = oldFlashColor;
						end
						if string.find(arg1, LVBM_BGMOD_LANG["HORDE"]) then
							LVBM.Announce(string.format(LVBM_BGMOD_LANG.HORDE_TAKE_ANNOUNCE, value));
							oldFlashColor=LVBM.Options.FlashColor;
							LVBM.Options.FlashColor = "red";
							LVBM.AddSpecialWarning("", false, true);
							LVBM.Options.FlashColor = oldFlashColor;
						end
						LVBM.EndStatusBarTimer("Alliance: "..LVBM_BGMOD_EN_TARGET_AV[index]);
						LVBM.EndStatusBarTimer("Horde: "..LVBM_BGMOD_EN_TARGET_AV[index]);
					end
				end
			end

			--Ivus and Ice Lord
			if string.find(arg1,LVBM_BGMOD_LANG["IVUS"]) then 
				LVBM.StartStatusBarTimer(603,  "Ivus spawn");
			elseif string.find(string.lower(arg1),string.lower(LVBM_BGMOD_LANG["ICEY"])) then
				LVBM.StartStatusBarTimer(603,  "Ice spawn");
			end

		-- Begining Match Timers and End Matches
		elseif (event == "CHAT_MSG_BG_SYSTEM_NEUTRAL") then
			if (arg1 == LVBM_BGMOD_LANG["WSG_START60SEC"]
			 or arg1 == LVBM_BGMOD_LANG["AB_START60SEC"]
			 or arg1 == LVBM_BGMOD_LANG["AV_START60SEC"] ) then
					LVBM.EndStatusBarTimer("Begins");
					LVBM.StartStatusBarTimer(62, "Begins");

			elseif (arg1 == LVBM_BGMOD_LANG["WSG_START30SEC"]
			     or arg1 == LVBM_BGMOD_LANG["AB_START30SEC"]
			     or arg1 == LVBM_BGMOD_LANG["AV_START30SEC"] ) then
					LVBM.EndStatusBarTimer("Begins");
					LVBM.StartStatusBarTimer(32, "Begins");
			end

			-- Resets Timers when BGMatch Ends (Note that this is different than Leaving BG)
			if string.find(arg1, LVBM_BGMOD_LANG["WINS"]) then
				LVBM.EndStatusBarTimer("Begins");
				LVBM.EndStatusBarTimer("Flag respawn");
				for index, value in ipairs(LVBM_BGMOD_LANG.AB_TARGETS) do
					LVBM.EndStatusBarTimer("Horde: "..LVBM.Capitalize(LVBM_BGMOD_EN_TARGET_AB[index]), true);
					LVBM.EndStatusBarTimer("Alliance: "..LVBM.Capitalize(LVBM_BGMOD_EN_TARGET_AB[index]), true);
				end
				for index, value in ipairs(LVBM_BGMOD_EN_TARGET_AV) do
					LVBM.EndStatusBarTimer("Horde: "..LVBM_BGMOD_EN_TARGET_AV[index], true);
					LVBM.EndStatusBarTimer("Alliance: "..LVBM_BGMOD_EN_TARGET_AV[index], true);
				end
			end

		-- Reset Timers when leaving BG
		elseif (event == "PLAYER_ENTERING_WORLD") then
			LVBM.EndStatusBarTimer("Begins");
			LVBM.EndStatusBarTimer("Flag respawn");
			for index, value in ipairs(LVBM_BGMOD_LANG.AB_TARGETS) do
				LVBM.EndStatusBarTimer("Horde: "..LVBM.Capitalize(LVBM_BGMOD_EN_TARGET_AB[index]), true);
				LVBM.EndStatusBarTimer("Alliance: "..LVBM.Capitalize(LVBM_BGMOD_EN_TARGET_AB[index]), true);
			end
			for index, value in ipairs(LVBM_BGMOD_EN_TARGET_AV) do
				LVBM.EndStatusBarTimer("Horde: "..LVBM_BGMOD_EN_TARGET_AV[index], true);
				LVBM.EndStatusBarTimer("Alliance: "..LVBM_BGMOD_EN_TARGET_AV[index], true);
			end
		end
	end, 	
	

};


