local Warsong = DBM:NewBossMod("Warsong", DBM_WARSONG, DBM_BGMOD_LANG["WS_DESCRIPTION"], DBM_OTHER, "Battlegrounds", 5);

Warsong.Version = "2.0";
Warsong.Author = "LeoLeal, Nitram, Tandanu";
Warsong.ExtraGUITab	= "BC Battlegrounds";

Warsong:RegisterEvents(
	"ZONE_CHANGED_NEW_AREA",
	"PLAYER_ENTERING_WORLD",
	"CHAT_MSG_BG_SYSTEM_ALLIANCE",
	"CHAT_MSG_BG_SYSTEM_HORDE",
	"CHAT_MSG_BG_SYSTEM_NEUTRAL",
	"CHAT_MSG_SPELL_PERIODIC_SELF_DAMAGE"
);

Warsong.DropdownMenu = { --I don't use :AddOption() here, because I want to use the battleground boss mod's options table
	{
		variable = "DBM.AddOns.Battlegrounds.Options.ShowInviteTimer", 
		text = DBM_BGMOD_LANG.SHOW_INV_TIMER, 
		func = function() DBM.AddOns.Battlegrounds.Options.ShowInviteTimer = not DBM.AddOns.Battlegrounds.Options.ShowInviteTimer; end, 
	},
	
	{
		variable = "DBM.AddOns.Battlegrounds.Options.ColorByClass", 
		text = DBM_BGMOD_LANG.COLOR_BY_CLASS, 
		func = function() DBM.AddOns.Battlegrounds.Options.ColorByClass = not DBM.AddOns.Battlegrounds.Options.ColorByClass; end, 
	},
};

Warsong:AddOption("ShowFlagCarrier", true, DBM_BGMOD_LANG.WSG_INFOFRAME_INFO, function()
	DBM:GetMod("Warsong").Options.ShowFlagCarrier = not DBM:GetMod("Warsong").Options.ShowFlagCarrier;
	if DBM:GetMod("Warsong").Options.ShowFlagCarrier and GetRealZoneText() == DBM_WARSONG then
		DBM:GetMod("Warsong"):HideFlagCarrier();
	else
		DBM:GetMod("Warsong"):HideFlagCarrier();
	end	
end);

function Warsong:ShowFlagCarrier()
	if AlwaysUpFrame1DynamicIconButton and AlwaysUpFrame2DynamicIconButton then
		if not self.FlagCarrierFrame1 then
			self.FlagCarrierFrame1 = CreateFrame("Frame", nil, AlwaysUpFrame1DynamicIconButton);
			self.FlagCarrierFrame1:SetHeight(10);
			self.FlagCarrierFrame1:SetWidth(100);
			self.FlagCarrierFrame1:SetPoint("LEFT", "AlwaysUpFrame1DynamicIconButton", "RIGHT", 4, 0)
			self.FlagCarrierFrame1Text = self.FlagCarrierFrame1:CreateFontString(nil, nil, "GameFontNormalSmall");
			self.FlagCarrierFrame1Text:SetAllPoints(self.FlagCarrierFrame1);
			self.FlagCarrierFrame1Text:SetJustifyH("LEFT");
		end
		if not self.FlagCarrierFrame2 then
			self.FlagCarrierFrame2 = CreateFrame("Frame", nil, AlwaysUpFrame2DynamicIconButton);
			self.FlagCarrierFrame2:SetHeight(10);
			self.FlagCarrierFrame2:SetWidth(100);
			self.FlagCarrierFrame2:SetPoint("LEFT", "AlwaysUpFrame2DynamicIconButton", "RIGHT", 4, 0)
			self.FlagCarrierFrame2Text= self.FlagCarrierFrame2:CreateFontString(nil, nil, "GameFontNormalSmall");
			self.FlagCarrierFrame2Text:SetAllPoints(self.FlagCarrierFrame2);
			self.FlagCarrierFrame2Text:SetJustifyH("LEFT");
		end
		self.FlagCarrierFrame1:Show();		
		self.FlagCarrierFrame2:Show();
	end
end

function Warsong:HideFlagCarrier()
	if self.FlagCarrierFrame1 and self.FlagCarrierFrame2 then
		self.FlagCarrierFrame1:Hide();
		self.FlagCarrierFrame2:Hide();
	end
end

function Warsong:OnEvent()
	if (event == "ZONE_CHANGED_NEW_AREA" or event == "PLAYER_ENTERING_WORLD") and GetRealZoneText() == DBM_WARSONG and self.Options.ShowFlagCarrier then
		self:ShowFlagCarrier();
		self.FlagCarrierFrame1Text:SetText("");
		self.FlagCarrierFrame2Text:SetText("");
		
	elseif (event == "ZONE_CHANGED_NEW_AREA" or event == "PLAYER_ENTERING_WORLD") and GetRealZoneText() ~= DBM_WARSONG then
		self:HideFlagCarrier();	
		
	elseif event == "CHAT_MSG_SPELL_PERIODIC_SELF_DAMAGE" then --speed boots
		if arg1 and string.find(arg1, DBM_BGMOD_LANG.WSG_BOOTS_EXPR) then
			self:StartStatusBarTimer(10, "Speed Boots", "Interface\\Icons\\Spell_Fire_BurningSpeed", true); --respawn time varies, ~3 minutes
		end
		
	elseif event == "CHAT_MSG_BG_SYSTEM_NEUTRAL" then --"game starts in.." timers
		if arg1 == DBM_BGMOD_LANG.WSG_START60SEC then
			self:StartStatusBarTimer(62, "Begins");
		elseif arg1 == DBM_BGMOD_LANG.WSG_START30SEC then
			if not self:GetStatusBarTimerTimeLeft("Begins") then
				self:StartStatusBarTimer(62, "Begins");
			end
			self:UpdateStatusBarTimer("Begins", 31, 62);
		end
	
	elseif (event == "CHAT_MSG_BG_SYSTEM_ALLIANCE" or event == "CHAT_MSG_BG_SYSTEM_HORDE") and self.Options.ShowFlagCarrier then
		if self.FlagCarrierFrame1 and self.FlagCarrierFrame2 then
			if string.find(arg1, DBM_BGMOD_LANG.WSG_FLAG_PICKUP) then
				local _, _, sArg1, sArg2 =  string.find(arg1, DBM_BGMOD_LANG.WSG_FLAG_PICKUP);
				local mSide, mNick;
				if( GetLocale() == "deDE" ) then
					mSide = sArg2; mNick = sArg1;
				else
					mSide = sArg1; mNick = sArg2;
				end
				
				if mSide == DBM_BGMOD_LANG.ALLIANCE then
					self.FlagCarrierFrame2Text:SetText(mNick);
					self.FlagCarrierFrame2:Show();
				elseif mSide == DBM_BGMOD_LANG.HORDE then
					self.FlagCarrierFrame1Text:SetText(mNick);
					self.FlagCarrierFrame1:Show();
				end
				
			elseif string.find(arg1, DBM_BGMOD_LANG.WSG_FLAG_RETURN) then
				local _, _, mSide, mNick =  string.find(arg1, DBM_BGMOD_LANG.WSG_FLAG_RETURN);
				if mSide == DBM_BGMOD_LANG.ALLIANCE then
					self.FlagCarrierFrame2:Hide();
				elseif mSide == DBM_BGMOD_LANG.HORDE then
					self.FlagCarrierFrame1:Hide();
				end
				
			elseif string.find(arg1, DBM_BGMOD_LANG.WSG_HASCAPTURED) then
				local oldFlashColor = DBM.Options.FlashColor;
				
				if event == "CHAT_MSG_BG_SYSTEM_ALLIANCE" then
					DBM.Options.FlashColor = "blue";
				elseif event == "CHAT_MSG_BG_SYSTEM_HORDE" then
					DBM.Options.FlashColor = "red";
				end
				
				self:AddSpecialWarning("", true);
				DBM.Options.FlashColor = oldFlashColor;
				self:StartStatusBarTimer(23, "Flag respawn", nil, true);
				
				self.FlagCarrierFrame2:Hide();
				self.FlagCarrierFrame1:Hide();
			end
		end
	end
end

