local addonName, ZLAUTO = ...

local ZL = ZLAUTO.mainFrame
local pruneCrossRealm = ZL.pruneCrossRealm
local MyAddon = ZL.MyAddon
local LibSerialize = LibStub("LibSerialize")
local LibDeflate = LibStub("LibDeflate")
-- 插件启用时的函数
function MyAddon:OnEnable()
    self:RegisterComm("FA VER") --请求检测全团是否有安装插件及插件的版本并且处理反馈回的消息(data.type=request, requestback)
end
local function UpdateCheckNotice()
    if ZL.updateCheckNoticed == nil then
        ZL.Print("有新的版本，请及时到各大插件平台更新。", true)
        ZL.updateCheckNoticed = true
    end
end
function MyAddon:TransmitAddonInfo2(prefix, data, chatType, targetName, prio)
    if data then
        prio = prio or "ALERT"
        local serialized = LibSerialize:Serialize(data)
        local compressed = LibDeflate:CompressDeflate(serialized)
        local encoded = LibDeflate:EncodeForWoWAddonChannel(compressed)
        -- print(Prefix(prefix .. "数据大小：" .. #encoded .. "字节"))
        if targetName then
            chatType = "WHISPER"
        else
            if not chatType then
                chatType = "RAID"
            end
        end
        if chatType == "WHISPER" then
            self:SendCommMessage(prefix, encoded, chatType, targetName, prio)
        else
            self:SendCommMessage(prefix, encoded, chatType, nil, prio)
        end
        serialized = nil
        compressed = nil
        encoded = nil
    end
end
function MyAddon:OnCommReceived(prefix, payload, chatType, sender)
    local decoded = LibDeflate:DecodeForWoWAddonChannel(payload)
    local decompressed = LibDeflate:DecompressDeflate(decoded)
    local success, fdata = LibSerialize:Deserialize(decompressed)
    if (not success) then return end
    sender = pruneCrossRealm(sender)
    if (prefix == "FA VER") then
        if fdata.type == "updateCheck" then
            if sender == UnitName("player") then return end
            local senderVerA, senderVerB, senderVerC = fdata.ver:match("^V(%d+)%.(%d+)%.(%d+)")
            senderVerA = tonumber(senderVerA) or 0
            senderVerB = tonumber(senderVerB) or 0
            senderVerC = tonumber(senderVerC) or 0
            if senderVerA > ZL.addonVersionA then
                UpdateCheckNotice()
            elseif senderVerA == ZL.addonVersionA then
                if senderVerB > ZL.addonVersionB then
                    UpdateCheckNotice()
                elseif senderVerB == ZL.addonVersionB then
                    if senderVerC > ZL.addonVersionC then
                        UpdateCheckNotice()
                    end
                end
            end
        end
    end
end
