local common = LibStub("LibAddon"):GetAddon("common");
local tvavatarlisttab = LibStub("LibAddon"):GetAddon("tvavatarlisttab")

local AnimIcon = {
	_imageset = {
		"SpeakIcon01",
		"SpeakIcon02",
		"SpeakIcon03",
	},
	_normalimg = "MsgWndLogoSpeak",
	_muteimg = "SpeakIconMute",
	_anim = false,
	_mute = false,
	_id = 1,
};

tvavatarlisttab.AnimIcon = AnimIcon;

function AnimIcon:new(item)
	local t = {_item = item};
	setmetatable(t, {__index = self});
	item:setIconTooltipText(0,"[colour='FF000000']屏蔽声音")
	return t;
end

function AnimIcon:setNormalImg(img)
	self._normalimg = img;
end

function AnimIcon:setMuteImg(img)
	self._muteimg = img;
end

function AnimIcon:setAnimImgSet(set)
	assert(type(set) == "table", "setAnimImgSet #1 arg must be a table value.");
	self._imageset = set;
end

function AnimIcon:start()
	if (not self._anim) then
		self._anim = true;	
		self:setID(1);
		local speakIcon = nil
		if (self._mute) then
			speakIcon = tvavatarlisttab.img_set:getImage( self._muteimg )
		else
			speakIcon = tvavatarlisttab.img_set:getImage( self._imageset[1] )
		end
		if speakIcon ~= nil then
			self._item:modifyIcon( 0, speakIcon, speakIcon:getWidth(), speakIcon:getHeight() )
			tvavatarlisttab.obj_avatar_list:invalidate()
		end
		self:init();
	end
end

function AnimIcon:stop()
	self._anim = false;
	local speakIcon = nil
	if (self._mute) then
		speakIcon = tvavatarlisttab.img_set:getImage( self._muteimg )
	else
		speakIcon = tvavatarlisttab.img_set:getImage( self._normalimg )
	end
	if speakIcon ~= nil then
		self._item:modifyIcon( 0, speakIcon, speakIcon:getWidth(), speakIcon:getHeight() )
		tvavatarlisttab.obj_avatar_list:invalidate()
	end
	self:destroy();
end

function AnimIcon:mute()
	self._mute = true;
	self._item:setIconTooltipText(0,"[colour='FF000000']恢复声音")
	self._item:setTextColours(CEGUI.colour( 98/255, 97/255, 97/255, 1 ))
	local speakIcon = tvavatarlisttab.img_set:getImage( self._muteimg )
	if speakIcon ~= nil then
		self._item:modifyIcon( 0, speakIcon, speakIcon:getWidth(), speakIcon:getHeight() )
		tvavatarlisttab.obj_avatar_list:invalidate()
	end
end

function AnimIcon:demute()
	self._mute = false;
	self._item:setIconTooltipText(0,"[colour='FF000000']屏蔽声音")
	self._item:setTextColours(CEGUI.colour( 201/255, 202/255, 181/255, 1 ))
	if (not self._anim) then
		local speakIcon = tvavatarlisttab.img_set:getImage( self._normalimg )
		if speakIcon ~= nil then
			self._item:modifyIcon( 0, speakIcon, speakIcon:getWidth(), speakIcon:getHeight() )
			tvavatarlisttab.obj_avatar_list:invalidate()
		end
	else
		local speakIcon = tvavatarlisttab.img_set:getImage( self._imageset[3] )
		if speakIcon ~= nil then
			self._item:modifyIcon( 0, speakIcon, speakIcon:getWidth(), speakIcon:getHeight() )
			tvavatarlisttab.obj_avatar_list:invalidate()
		end
	end
end

function AnimIcon:ismute()
	return self._mute;
end

function AnimIcon:setID(id)
	self._id = id;
end

function AnimIcon:getID()
	return self._id;
end

function AnimIcon:OnUpdate()
	local id, image;
	if (self._anim and not self._mute) then			
		id = (self:getID() == #(self._imageset)) and 1 or (self:getID() +1);
		image = self._imageset[id];
		local speakIcon = tvavatarlisttab.img_set:getImage( image )
		if speakIcon and tvavatarlisttab.obj_avatar_list:isVisible() then
			self._item:modifyIcon( 0, speakIcon, speakIcon:getWidth(), speakIcon:getHeight() )
			tvavatarlisttab.obj_avatar_list:invalidate()
		end
		self:setID(id);
	end
end

function AnimIcon:init()
	if (self.channnel_info_timer == nil) then
		self.channnel_info_timer = common:ScheduleRepeatingTimer(AnimIcon.OnUpdate, 500, self)
	end
end

function AnimIcon:destroy()
	if self.channnel_info_timer then
		common:CancelTimer(self.channnel_info_timer)
		self.channnel_info_timer = nil
	end
end
