
local tvspeaklist = LibStub("LibAddon"):NewAddon("tvspeaklist", "LibEvent", "LibTimer")
local common = LibStub("LibAddon"):GetAddon("common");


local default_db = {
	talking_list = {},				-- 当前正在说话的玩家列表
	icon_num = 0,
	max_speaker_num = 4,
	x_pos= 400,
	y_pos = 260,
};

function tvspeaklist:OnInitialize()
	self:DisableDebug();
	-- self:EnableDebug();
	self:debug("tvspeaklist::OnInitialize");
	self.db = default_db;
	self.wnd_mgr = CEGUI.WindowManager:getSingleton();
	self.root = CEGUI.System:getSingleton():getGUISheet();

	self.partyframe = self.wnd_mgr:getWindow("tvspeakerFrame");
	
	self:RegisterMessages();

end

function tvspeaklist:RegisterMessages()
	self:RegisterEvent("WM_MOVE_TVSPEAKLIST", "on_move_tvspeaklist");
	self:RegisterEvent("WM_SET_TVSPEAKELIST_NUM", "on_set_tvspeaklist_num");
	
	self:RegisterMessage("TEAMVOICE_INIT_PLAYER", "notify_teamvoice_init_player");
	self:RegisterMessage("TEAMVOICE_DELETE_PLAYER", "notify_teamvoice_delete_player");
	
	self:RegisterMessage("TEAMVOICE_PERFECT_PLAYER", "notify_teamvoice_perfect_player");
	self:RegisterMessage("TEAMVOICE_TALKING_PLAYER", "notify_teamvoice_talking_player");
	self:RegisterMessage("TEAMVOICE_LEAVE_PLAYER", "notify_teamvoice_leave_player");
	
	self:RegisterMessage("TEAMVOICE_CLEARDB", "notify_teamvoice_cleardb");

end

function tvspeaklist:notify_teamvoice_init_player(event, player, x, y)
	if player then
		local talking_data = {
			_obj_partyframe = nil,
			_animIcon = nil,
			_obj_nameText = nil,
		};

		player._talking_data = talking_data;
	end
end

function tvspeaklist:notify_teamvoice_delete_player(event, player)
	self:del_playerinfo(player);
end

function tvspeaklist:notify_teamvoice_perfect_player(event, player)
	if (player._talking_data ~= nil) then
		self:add_playerinfo(player);
	end
end

function tvspeaklist:notify_teamvoice_talking_player(event, player, talking)
	if (player._talking_data ~= nil) then
		if talking == 1 then
			self:on_player_talking(player);
		else
			self:on_player_stoptalking(player);
		end
	end
	
	return true;
end

function tvspeaklist:notify_teamvoice_cleardb(event)
	self:debug("clear_db");

	self.db.talking_list = {};
	self.db.icon_num = 0;
	self.db.max_speaker_num = 4;
	self.db.x_pos = 400;
	self.db.y_pos = 260;
end

function tvspeaklist:on_move_tvspeaklist(event, x, y)
	self.db.x_pos = x;
	self.db.y_pos = y;

	self:debug( ("on_move_tvspeaklist x_pos:%s, y_pos:%s"):format(tostring(x), tostring(y)));
	self:fresh_talking_pos();
end

function tvspeaklist:on_set_tvspeaklist_num(event, num)
	self.db.max_speaker_num = num;
	
	self:fresh_talking_pos();
end

function tvspeaklist:on_player_talking(player)

	self:debug( ("on_player_talking name:%s"):format(tostring(player._player_name)));

	local bFind = false;
	for key, talking_info in pairs(self.db.talking_list) do
		if(talking_info._player_name == player._player_name) then
			bFind = true;
			break;
		end
	end

	if(bFind == false) then
		local talking_info = {
			_player_name = player._player_name,
			_talking_data = player._talking_data,
		};
		if(table.getn(self.db.talking_list) >= self.db.max_speaker_num) then
			table.remove(self.db.talking_list, table.maxn(self.db.talking_list));
		end
		table.insert(self.db.talking_list, 1, talking_info);
		
		self:debug( ("on_player_talking add player name:%s"):format(tostring(player._player_name)));
		self:fresh_talking_pos();
	end

end

function tvspeaklist:on_player_stoptalking(player)
	
	for i=1, table.maxn(self.db.talking_list) do  
		if(self.db.talking_list[i]._player_name == player._player_name) then
			table.remove(self.db.talking_list, i);
			if(player._talking_data._obj_nameText ~= nil and player._talking_data._obj_partyframe ~= nil) then
				self:debug( ("on_player_stoptalking player name:%s"):format(tostring(player._player_name)));
				player._talking_data._obj_nameText:hide();
				player._talking_data._obj_partyframe:hide();
			end
			self:fresh_talking_pos();
			break;
		end
	end
	
end

function tvspeaklist:notify_teamvoice_leave_player(event, player)
	self:debug( ("notify_teamvoice_leave_player uid:%s, name:%s."):format(tostring(player.speaker_data._uid), tostring(player.speaker_data._player_name)));

	self:del_playerinfo(player);
end

function tvspeaklist:adjust_show_position(pos, talking_info)
	if(talking_info._obj_nameText ~= nil and talking_info._animIcon ~= nil and talking_info._obj_partyframe ~= nil) then
		local width = talking_info._obj_nameText:getFont():getTextExtent(talking_info._obj_nameText:getText());
		talking_info._obj_nameText:setSize(CEGUI.UVector2( CEGUI.UDim(0, width), CEGUI.UDim(0, 18) ));
		talking_info._animIcon:setPosition(self.db.x_pos-20-width, self.db.y_pos-pos*20);
		talking_info._obj_nameText:show();
		talking_info._obj_partyframe:show();
	end
end

function tvspeaklist:fresh_talking_pos()

	self:debug( ("fresh_talking_pos"));

	for i=1, table.maxn(self.db.talking_list) do
		local talking_data = self.db.talking_list[i]._talking_data;
		if(talking_data ~= nil) then
			self:adjust_show_position(i, talking_data);
		end
	end
	
end
 
function tvspeaklist:del_playerinfo(player)
	if (player._talking_data ~= nil) then
		self:on_player_stoptalking(player);

		if (player._talking_data._animIcon ~= nil) then
			player._talking_data._animIcon:stop();
			player._talking_data._animIcon = nil;
		end
		if player._talking_data._obj_partyframe ~= nil then
			self.wnd_mgr:destroyWindow(player._talking_data._obj_partyframe);
			player._talking_data._obj_partyframe = nil;
		end
		if player._talking_data._obj_nameText ~= nil then
			self.wnd_mgr:destroyWindow(player._talking_data._obj_nameText);
			player._talking_data._obj_nameText = nil;
		end
		
		player._talking_data = nil;
	end
	
end

function tvspeaklist:add_playerinfo(player)
	self.db.icon_num = self.db.icon_num + 1;
	player._talking_data._obj_partyframe = self.wnd_mgr:createWindow("YY/StaticImage", "tvspeakerFrame/icon".. self.db.icon_num);
	if (player._talking_data._obj_partyframe) then
		self.partyframe:addChildWindow(player._talking_data._obj_partyframe);
		player._talking_data._obj_partyframe:setSize(CEGUI.UVector2( CEGUI.UDim(0, 18), CEGUI.UDim(0, 18) )) ;
		player._talking_data._obj_partyframe:hide();
	else
		self:error( ("add_playerinfo player_name:%s, createWindow partyframe Failed!"):format(tostring(player._player_name)));
	end
	
	player._talking_data._obj_nameText = self.wnd_mgr:createWindow("YY/StaticText", "tvspeakerFrame/name".. self.db.icon_num);
	if (player._talking_data._obj_nameText) then
		self.partyframe:addChildWindow(player._talking_data._obj_nameText);
		if player._another_name ~= nil then
			player._talking_data._obj_nameText:setText(player._another_name);
		else
			player._talking_data._obj_nameText:setText(player._player_name);
		end
		player._talking_data._obj_nameText:hide();
	else
		self:error( ("on_player_talking player_name:%s, createWindow nametext Failed!"):format(tostring(player_name)));
	end
	
	player._talking_data._animIcon = tvspeaklist.SpeakerIcon:new(player._talking_data._obj_partyframe, player._talking_data._obj_nameText);
	player._talking_data._animIcon:setNormalImg("set:YYUI image:SpeakIconNormal");
	player._talking_data._animIcon:start();
end

