local HealBotAddonMsgType=nil
local tmpttl=0
local HealBotcAddonIncHeals={}
local HealBotcAddonSummary={}
local HealBotAddonSummaryNoComms={}
local sortorder={}
local AddonName=nil
local AddonEnabled=nil
local hbcommver={}
local hbtmpver={}
local linenum=0
local addon_id=nil
local sender_id=nil
local i,v,x,z=nil,nil,nil,nil

function HealBot_Comms_SendAddonMsg(addon_id, msg, aType, pName)
    if aType==1 then
        SendAddonMessage(addon_id, msg, "BATTLEGROUND" );
    elseif aType==2 then
        SendAddonMessage(addon_id, msg, "RAID" );
    elseif aType==3 then
        SendAddonMessage(addon_id, msg, "PARTY" );
    elseif aType==4 and pName then
        SendAddonMessage(addon_id, msg, "WHISPER", pName );
    end
end

function HealBot_Comms_GetChan(chan)
    if GetChannelName(chan)>0 then
        return GetChannelName(chan);
    else
        return nil;
    end
end

function HealBot_Comms_Info()
    if HealBot_Error:IsVisible() then
        HideUIPanel(HealBot_Error)
        return
    end
    UpdateAddOnCPUUsage()
    UpdateAddOnMemoryUsage()
    HealBotAddonMsgType=HealBot_GetHealBot_AddonMsgType()
    HealBotcAddonSummary=HealBot_RetHealBotAddonSummary()
    HealBotcAddonIncHeals=HealBot_RetHealBotAddonIncHeals()
    hbcommver=HealBot_GetInfo()
    HealBot_Report_Error("Zone="..GetRealZoneText())
    if HealBotAddonMsgType==1 then
        HealBot_Report_Error("AddonComms=BATTLEGROUND")
    elseif HealBotAddonMsgType==2 then
        HealBot_Report_Error("AddonComms=RAID")
    elseif HealBotAddonMsgType==3 then
        HealBot_Report_Error("AddonComms=PARTY")
    elseif HealBotAddonMsgType==4 then
        HealBot_Report_Error("AddonComms=WHISPER")
    end
    HealBot_Report_Error("#Raid="..GetNumRaidMembers().."   #Party="..GetNumPartyMembers())

    for x,_ in pairs(hbtmpver) do
        hbtmpver[x]=nil
    end
    sortorder={}
    for z,x in pairs(HealBotcAddonIncHeals) do
        table.insert(sortorder,z)
    end
    table.sort(sortorder,function (a,b)
        if HealBotcAddonIncHeals[a]>HealBotcAddonIncHeals[b] then return true end
        if HealBotcAddonIncHeals[a]<HealBotcAddonIncHeals[b] then return false end
        return a<b
    end)

    linenum=1
    table.foreach(sortorder, function (index,z)
        tmpttl=HealBotcAddonIncHeals[z] or 0
        _,_,addon_id, sender_id = string.find(z, "(.+) : (.+)")
        if linenum<25 and addon_id and sender_id then
            if addon_id==HealBot_retAddonID() and hbcommver[sender_id] then 
                addon_id=strsub(hbcommver[sender_id],9,-1) or "HealBot" 
                hbtmpver[sender_id]=true
            end
            HealBot_Comms_Print_IncHealsSum(sender_id,addon_id,tmpttl,linenum)
            linenum=linenum+1
        end
    end)

    for x,v in pairs(hbcommver) do
        if not hbtmpver[x] and linenum<21 then
            HealBot_Comms_Print_IncHealsSum(x,strsub(v,9,-1),0,linenum)
            linenum=linenum+1
        end
    end

    linenum=1
    for x,_ in pairs(HealBotAddonSummaryNoComms) do
        HealBotAddonSummaryNoComms[x]=nil;
    end
    sortorder={}
    for z,x in pairs(HealBotcAddonSummary) do
        table.insert(sortorder,z)
    end
    table.sort(sortorder,function (a,b)
        if HealBotcAddonSummary[a]>HealBotcAddonSummary[b] then return true end
        if HealBotcAddonSummary[a]<HealBotcAddonSummary[b] then return false end
        return a<b
    end)
    sortorder={}
    for z,x in pairs(HealBotcAddonSummary) do
        if x>19 then
            HealBotAddonSummaryNoComms[z]=HealBot_Comm_round(GetAddOnCPUUsage(z)/1000, 1)
            table.insert(sortorder,z)
        end
    end
    for i=1, GetNumAddOns() do
        AddonName,_,_,AddonEnabled = GetAddOnInfo(i);
        if AddonEnabled and not HealBotAddonSummaryNoComms[AddonName] and GetAddOnCPUUsage(i)>100 then
            HealBotAddonSummaryNoComms[AddonName]=HealBot_Comm_round(GetAddOnCPUUsage(AddonName)/1000, 1)
            table.insert(sortorder,AddonName)
        end
    end
    table.sort(sortorder,function (a,b)
        if HealBotAddonSummaryNoComms[a]>HealBotAddonSummaryNoComms[b] then return true end
        if HealBotAddonSummaryNoComms[a]<HealBotAddonSummaryNoComms[b] then return false end
        return a<b
    end)
    table.foreach(sortorder, function (index,z)
        tmpttl=HealBotcAddonSummary[z] or 0
        if linenum<35 then 
            HealBot_Comms_Print_AddonSum(z,HealBotAddonSummaryNoComms[z],floor(GetAddOnMemoryUsage(z)),tmpttl,linenum)
            linenum=linenum+1
        end
    end)
end

function HealBot_Comms_Print_IncHealsSum(sender_id,addon_id,HealsCnt,linenum)
    getglobal("HBIncH"..linenum.."Healer"):SetText(sender_id);
    getglobal("HBIncH"..linenum.."Ver"):SetText(addon_id);
    getglobal("HBIncH"..linenum.."Cnt"):SetText(HealsCnt);
end

function HealBot_Comms_Print_AddonSum(Addon,CPU,MEM,Comms,linenum)
    getglobal("HBMod"..linenum.."Name"):SetText(Addon);
    getglobal("HBMod"..linenum.."CPU"):SetText(CPU);
    getglobal("HBMod"..linenum.."Mem"):SetText(MEM);
    getglobal("HBMod"..linenum.."Comm"):SetText(Comms);
end

function HealBot_Comms_TargetInfo()
    if HealBot_Error:IsVisible() then
        HealBot_Errors_Clear()
    end
    HealBot_Report_Error("UnitHealth="..UnitHealth("target").."   UnitHealthMax="..UnitHealthMax("target"))
    HealBot_Report_Error("UnitMana="..UnitMana("target").."   UnitManaMax="..UnitManaMax("target"))
end

local mult = 0
function HealBot_Comm_round(num, idp)
    mult = 10^(idp or 0)
    return math.floor(num * mult + 0.5) / mult
end

local HealBot_MsgUpdateAvail=nil
local hbMajor, hbMinor, hbPatch, hbHealbot = string.split(".", HEALBOT_VERSION)
local tMajor, tMinor, tPatch, tHealbot = 0,0,0,0
local hbVersionChecked = {}
local tNewVer=nil
function HealBot_Comms_CheckVer(pName, version)
    if not hbVersionChecked[pName] then
        tNewVer=nil
        hbVersionChecked[pName]=true
        tMajor, tMinor, tPatch, tHealbot = string.split(".", version)
        if tonumber(tMajor)>tonumber(hbMajor) then 
            tNewVer=true
        elseif tonumber(tMajor)==tonumber(hbMajor) and tonumber(tMinor)>tonumber(hbMinor) then 
            tNewVer=true
        elseif tonumber(tMajor)==tonumber(hbMajor) and tonumber(tMinor)==tonumber(hbMinor) and tonumber(tPatch)>tonumber(hbPatch) then 
            tNewVer=true
        elseif tonumber(tMajor)==tonumber(hbMajor) and tonumber(tMinor)==tonumber(hbMinor) and tonumber(tPatch)==tonumber(hbPatch) and tonumber(tHealbot)>tonumber(hbHealbot) then 
            tNewVer=true
        end
        if tNewVer then
            hbMajor=tMajor
            hbMinor=tMinor
            hbPatch=tPatch
            hbHealbot=tHealbot
            if not HealBot_MsgUpdateAvail then
                HealBot_AddChat("[HealBot]  A newer version is available")
                HealBot_AddChat("[HealBot]  at http://healbot.alturl.com")
            end
            HealBot_MsgUpdateAvail = hbMajor.."."..hbMinor.."."..hbPatch.."."..hbHealbot
        end
    end
end

function HealBot_Comms_ReportVer()
    return HealBot_MsgUpdateAvail
end



  