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

common.const = {}		-- 全局常量
common.interface = {}	        -- 全局函数(各插件用add_interface自己注册)
common.global = {}		-- 全局变量(各插件用add_globale自己注册)
common.util = {}			-- 公共方法, 类似原内置YY的common

-- 15分钟
local HEART_BEATE_INTERVAL = 15 * 60 *1000
local DOYY_STIMER = nil
local DOTEAMVOICE_STIMER = nil

function common:OnAddonLoaded(loaded_name)
	self:debug("OnAddonLoaded " .. loaded_name or "nil")
end

function common:OnInitialize()
	self:debug("@@@:OnInitialize()")
--	self:print("@@@:OnInitialize()")	
	self:registerProtocolEvents()
--	self:DisableDebug()	
end



function common:DoTeamVoice()
	if DOTEAMVOICE_STIMER == nil then
		self:ScheduleTimer("on_teamvoice_heart_beate", 100)
		DOTEAMVOICE_STIMER = self:ScheduleRepeatingTimer("on_teamvoice_heart_beate", HEART_BEATE_INTERVAL)
	end
end

function common:registerProtocolEvents()
	self:print("registerProtocolEvents")
	self:RegisterEvent("WM_PRODUCT_COMMIT_IDX", "on_commit_idx")
	self:RegisterEvent("WM_PRODUCT_COMMIT_SVRID", "on_commit_svrid")
	--self:RegisterEvent("WM_SHOWINTERNALYY", "on_show_internalyy")
	--self:RegisterEvent("WM_SHOW_TEAMVOICE_UI", "on_show_teamvoice")
end

function common:on_commit_idx(event, idx)
	-- 厂商ID无效或者已赋值厂商ID与IDX不等跳出
	if idx == 0 or idx == -1 or
		(self.global["g_game_id"] ~= -1 and self.global["g_game_id"] ~= idx) then
		self:print("return")
		return
	end

	self.global["g_game_id"] = idx
	self:print("on_commit_idx: " .. self.global["g_game_id"] or "nil")
	self:SendMessage("RECEIVE_PRODUCT_ID")
	self.global["g_result_info"] = 1
end

function common:on_commit_svrid(event, svrid)
	self.global["g_game_server_id"] = svrid
	self:print("on_commit_svrid: " .. self.global["g_game_server_id"] or "nil")
	self.global["g_result_info"] = 1
end

function common:on_show_internalyy(event, bSet)
	if bSet == true then
		self:DoYY();
	end
end

function common:on_show_teamvoice(event, show)
	if show == true then
		self:DoTeamVoice();
	end
end

-- 心跳统计
function common:on_yy_heart_beate()
	self.hiido:DoYYHeartBeat(common.global["g_cur_channel_shortID"], 0)
end

function common:on_teamvoice_heart_beate()
	self.hiido:DominiYYHeartBeat(0, 0)
end