KillCount = {};
KillCount.Honorable = 0;
KillCount.Dishonorable = 0;
KillCount.HonorableList = { };
KillCount.DishonorableList = { };
KillCount_LastName = "";
KillCount_Number = 0;
RegisterForSave("KillCount");

function KillCount_Print(msg)
	if (not KillCount.Honorable) then KillCount.Honorable = 0; end
	if (not KillCount.Dishonorable) then KillCount.Dishonorable = 0; end
	KillCount_OnShow();
	if (not Print) then
		DEFAULT_CHAT_FRAME:AddMessage("Honorable kills : "..KillCount.Honorable, 1, 0, 0);
		DEFAULT_CHAT_FRAME:AddMessage("Dishonorable kills : "..KillCount.Dishonorable, 1, 0, 0);
	else
		Print("Honorable kills : "..KillCount.Honorable, 1, 0, 0);
		Print("Dishonorable kills : "..KillCount.Dishonorable, 1, 0, 0);
	end
end

if ( Cosmos_RegisterChatCommand ) then
	Cosmos_RegisterChatCommand (
		"KILLCOUNT", -- Some Unique Group ID
		{"/kill"}, -- The Commands
		KillCount_Print,
		"" -- Description String
	);
else
	SlashCmdList["KILLCOUNT"] = KillCount_Print;
	SLASH_KILLCOUNT1 = "/kill";
end

Cosmos_RegisterChatWatch("KILLCOUNT", {"COMBAT_HOSTILE_DEATH"}, function (type, info, arg1, arg2, arg3, arg4) KillCount_ProcessMessage(arg1); end );

old_UIErrorsFrame_OnEvent = UIErrorsFrame_OnEvent;
function UIErrorsFrame_OnEvent(event, message)
	if (event == "UI_INFO_MESSAGE" and message == ERR_PVP_KILL_HONORABLE) then
		KillCount.Honorable = KillCount.Honorable + 1;
		if (not KillCount_LastName or strlen(KillCount_LastName) == 0) then
			KillCount_Number = KillCount_Number + 1;
			if (KillCount_Number < 5) then
				Cosmos_Schedule(0.2, UIErrorsFrame_OnEvent, "UI_INFO_MESSAGE", ERR_PVP_KILL_HONORABLE);
				return;
			else
				KillCount_Number = 0;
				KillCount_LastName = "Unknown";
			end
		else
			if (type(KillCount.HonorableList[KillCount_LastName]) == "number") then
				KillCount.HonorableList[KillCount_LastName] = KillCount.HonorableList[KillCount_LastName] + 1;
			else
				KillCount.HonorableList[KillCount_LastName] = 1;
			end
		end
		UIErrorsFrame:AddMessage(message.." #"..KillCount.Honorable.." - "..KillCount_LastName, 1.0, 1.0, 0.0, 1.0, UIERRORS_HOLD_TIME);
		KillCount_LastName = "";
		return;
	end
	if (event == "UI_INFO_MESSAGE" and message == ERR_PVP_KILL_DISHONORABLE) then
		KillCount.Dishonorable = KillCount.Dishonorable + 1;
		if (not KillCount_LastName) then 
			KillCount_LastName = "Unknown"; 
		else
			if (type(KillCount.DishonorableList[KillCount_LastName]) == "number") then
				KillCount.DishonorableList[KillCount_LastName] = KillCount.DishonorableList[KillCount_LastName] + 1;
			else
				KillCount.DishonorableList[KillCount_LastName] = 1;
			end
		end
		UIErrorsFrame:AddMessage(message.." #"..KillCount.Dishonorable.." - "..KillCount_LastName, 1.0, 1.0, 0.0, 1.0, UIERRORS_HOLD_TIME);
		KillCount_LastName = "";
		return;
	end
	old_UIErrorsFrame_OnEvent(event, message);
end

function KillCount_OnShow()
	if (not KillCount.Honorable) then KillCount.Honorable = 0; end
	if (not KillCount.Dishonorable) then KillCount.Dishonorable = 0; end

	KillCountFrameHonorableKills:SetText(KillCount.Honorable);
	KillCountFrameDishonorableKills:SetText(KillCount.Dishonorable);
end

function KillCount_ProcessMessage(msg)
	KillCount_LastName = strsub(msg, 1, strlen(msg) - 6);
end