local HealBotAddonMsgType=nil
local tmpttl=0
local HealBotcAddonIncHeals={}
local HealBotcAddonSummary={}
local HealBotcommAddonSummary={}
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
local HBclient=nil
local HB_errtext=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
  --  HealBot_AddDebug("addon msg="..msg)
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
    for x,_ in pairs(sortorder) do
        sortorder[x]=nil;
    end
    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<26 and addon_id and sender_id then
            if addon_id==HealBot_IncHeals_retAddonCommsID() and hbcommver[sender_id] then 
                addon_id=hbcommver[sender_id] 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<26 then
            HealBot_Comms_Print_IncHealsSum(x,v,0,linenum)
            linenum=linenum+1
        end
    end

    linenum=1
    for x,_ in pairs(HealBotAddonSummaryNoComms) do
        HealBotAddonSummaryNoComms[x]=nil;
    end
    for x,_ in pairs(sortorder) do
        sortorder[x]=nil;
    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)
        if linenum<39 and HealBotAddonSummaryNoComms[z]>0.4 then 
            HealBot_Comms_Print_AddonCPUSum(z,HealBotAddonSummaryNoComms[z],floor(GetAddOnMemoryUsage(z)),linenum)
            linenum=linenum+1
        end
    end)
    linenum=1
    for x,_ in pairs(sortorder) do
        sortorder[x]=nil;
    end
    for x,_ in pairs(HealBotcommAddonSummary) do
        HealBotcommAddonSummary[x]=nil;
    end
    for z,x in pairs(HealBotcAddonSummary) do
        HealBotcommAddonSummary[z]=HealBot_Comm_round(x/1024,2)
    end
    for z,x in pairs(HealBotcommAddonSummary) do
        table.insert(sortorder,z)
    end
    table.sort(sortorder,function (a,b)
        if HealBotcommAddonSummary[a]>HealBotcommAddonSummary[b] then return true end
        if HealBotcommAddonSummary[a]<HealBotcommAddonSummary[b] then return false end
        return a<b
    end)
    table.foreach(sortorder, function (index,z)
        if linenum<39 and HealBotcommAddonSummary[z]>0.04 then 
            HealBot_Comms_Print_AddonCommsSum(z,HealBotcommAddonSummary[z],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(ceil(HealsCnt));
end

function HealBot_Comms_Print_AddonCPUSum(Addon,CPU,MEM,linenum)
    getglobal("HBMod"..linenum.."Name1"):SetText(Addon);
    getglobal("HBMod"..linenum.."CPU"):SetText(CPU);
    getglobal("HBMod"..linenum.."Mem"):SetText(MEM);
end

function HealBot_Comms_Print_AddonCommsSum(Addon,Comms,linenum)
    getglobal("HBMod"..linenum.."Name2"):SetText(Addon);
    getglobal("HBMod"..linenum.."Comm"):SetText(Comms);
end

function HealBot_Comms_TargetInfo()
    if HealBot_Error:IsVisible() then
        HealBot_Comms_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_CHAT_ADDONID..HEALBOT_CHAT_NEWVERSION1)
                HealBot_AddChat(HEALBOT_CHAT_ADDONID..HEALBOT_CHAT_NEWVERSION2)
            end
            HealBot_MsgUpdateAvail = hbMajor.."."..hbMinor.."."..hbPatch.."."..hbHealbot
        end
        HealBot_Options_ShareSkin_Refresh()
        HealBot_IncHeals_sethbUsers()
    end
end

function HealBot_Comms_ReportVer()
    return HealBot_MsgUpdateAvail
end

function HealBot_Comms_OnShow(self)
    HBclient = getglobal("HealBot_Error_Clientx")
    HBclient:SetText(HEALBOT_WORD_CLIENT.."="..GetLocale())
    HealBot_Error_Versionx:SetText(HEALBOT_WORD_VERSION.."="..HEALBOT_VERSION)
    HealBot_Error_Classx:SetText(HEALBOT_SORTBY_CLASS.."="..HealBot_PlayerClassEN)
end

function HealBot_Comms_OnHide(self)
    HealBot_StopMoving(self);
    HealBot_Comms_Clear()
end


function HealBot_Comms_Clear()
    for x=1,3 do
        HB_errtext = getglobal("HealBot_Error"..x);
        HB_errtext:SetText(" ")
    end
    HealBot_ErrorCntReset();
end

function HealBot_Comms_OnDragStart(self,arg1)
    HealBot_StartMoving(self);
end

function HealBot_Comms_OnDragStop(self)
    HealBot_StopMoving(self);
end

function HealBot_ErrorsIn(msg,id)
    HB_errtext = getglobal("HealBot_Error"..id);
    HB_errtext:SetText(msg)
    if not HealBot_Error:IsVisible() then 
        ShowUIPanel(HealBot_Error)
    end
end



  