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

local SpeakerIcon = {
	_imageset = {
		"set:YYUI image:SpeakIcon01",
		"set:YYUI image:SpeakIcon02",
		"set:YYUI image:SpeakIcon03",
	},
	_normalimg = "set:YYUI image:MsgWndLogoSpeak",
	_anim = false,
	_id = 1,
};

tvspeaklist.SpeakerIcon = SpeakerIcon;

function SpeakerIcon:new(icon, name)
	local t = {_icon = icon, _nameUI = name};
	setmetatable(t, {__index = self});
	return t;
end


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


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

function SpeakerIcon:start()
	if (not self._anim) then
		self._anim = true;	
		self:setID(1);
		self._icon:setProperty("Images", self._imageset[1]);
		self._icon:setProperty("HorzFormatting", "RightAligned");
		self._nameUI:setProperty("BackgroundEnabled", "False");
		self._nameUI:setProperty("FrameEnabled", "False");
		self._nameUI:setProperty("HorzFormatting", "RightAligned");
		self:init();
	end
end

function SpeakerIcon:stop()
	self._anim = false;
	self._icon:setProperty("Images", self._normalimg);
	self:destroy();
end

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

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

function SpeakerIcon:setPosition(left, top)
	self._icon:setPosition(CEGUI.UVector2(CEGUI.UDim(0, left),CEGUI.UDim(0, top)));
	self._nameUI:setPosition(CEGUI.UVector2(CEGUI.UDim(0, left+20),CEGUI.UDim(0, top)));
end

function SpeakerIcon:OnUpdate()
	local id, image;
	if (self._anim) then			
		id = (self:getID() == #(self._imageset)) and 1 or (self:getID() +1);
		image = self._imageset[id];
		self._icon:setProperty("Images", image);
		self:setID(id);
	end
end

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

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