local HealBot_ClassColR = {};
local HealBot_ClassColG = {};
local HealBot_ClassColB = {};
local class=nil
local HealBot_UnitStatus={};
local HealBot_PlayerDead=false;
local HealBot_CheckGroup=0;
local HealBot_Enabled={};
local HealBot_PetMaxH={};
local HealBot_PetMaxHcnt1={};
local HealBot_PetMaxHcnt2={};
local HealBot_AttribStatus={};
local HealBot_UnitRange={}
local HealBot_UnitRangeSpell={}
local HealBot_UnitRanger={}
local HealBot_UnitRangeg={}
local HealBot_UnitRangeb={}
local HealBot_UnitRangea={}
local HealBot_UnitRangeitr={}
local HealBot_UnitRangeitg={}
local HealBot_UnitRangeitb={}
local HealBot_UnitRangeotr={}
local HealBot_UnitRangeotg={}
local HealBot_UnitRangeotb={}
local HealBot_UnitRangeota={}
local HealBot_curUnitHealth={}
local HealBot_UnitBarUpdate={}
local HealBot_TrackBars={}
local HealBot_ResetBars={}
local HealBot_UnitOffline={}
local HealBot_ResetAttribs=nil
local ceil=ceil;
local b=nil
local btHBbarText=nil
local bttextlen=0
local bthlthdelta=0
local floor=floor
local strsub=strsub
local HealBot_ButtonStore={}
local HealBot_hSpell=HEALBOT_HEAVY_NETHERWEAVE_BANDAGE
local HealBot_bSpell=HEALBOT_HEAVY_NETHERWEAVE_BANDAGE
local HealBot_dSpell=HEALBOT_HEAVY_NETHERWEAVE_BANDAGE
local HealBot_rSpell=HEALBOT_HEAVY_NETHERWEAVE_BANDAGE

local HealBot_ButtonArray1={}
local HealBot_ButtonArray2={}
local HealBot_SmallArray=nil
local HealBot_ButtonArray=1
local HealBot_Aggro={}
local HealBot_AggroBar4={}
local HealBot_AggroBarA=0.8
local HealBot_AggroBarAup=true
local HealBot_MyTargets={}
local HB_Action_Timer1 = 2000
local HB_Action_Timer2 = 2000

local HealBot_Unit_Bar1={}
local HealBot_Unit_Bar2={}
local HealBot_Unit_Bar3={}
local HealBot_Unit_Bar4={}

local rangeSpell=nil
local sID=nil
local sName=nil
local uHealIn=nil
local uHlth=nil
local uMaxHlth=nil
local uName=nil
local xButton=nil
local xUnit=nil
local xGUID=nil
local x=nil
local y=nil
local vUnit=nil
local HealBot_FrameMoving=nil
local LSM = LibStub("LibSharedMedia-3.0")
local HealBot_UnitThreat={}

-- Register Default HealBot Media
for i = 1, #HealBot_Default_Textures do
    LSM:Register("statusbar", HealBot_Default_Textures[i].name, HealBot_Default_Textures[i].file)
end
for i = 1, #HealBot_Default_Sounds do
    LSM:Register("sound", HealBot_Default_Sounds[i].name, HealBot_Default_Sounds[i].file)
end

local barName=nil
local barTemp=nil
local HealBot_prevUnitThreat={}
local Healbot_tempUnitThreat=0
local UnitDebuffStatus={}
function HealBot_Action_UpdateAggro(unit,status,threatStatus,hbGUID)
    uName=HealBot_UnitName[hbGUID]
    barName=HealBot_Unit_Bar4[unit]
    if not barName or not uName then 
        return 
    end    
    if not status and (HealBot_Aggro[hbGUID] or UnitDebuffStatus[hbGUID]) then HealBot_Action_ResetUnitStatus(unit) end
    if status and not HealBot_Aggro[hbGUID] and not UnitDebuffStatus[hbGUID] then HealBot_Action_ResetUnitStatus(unit) end
    if HealBot_Config.CDCshownAB==1 and UnitDebuffStatus[hbGUID] then
        if not status then
            HealBot_Aggro[hbGUID]="True"
        elseif status=="debuff" then
            if not HealBot_Aggro[hbGUID] then
                HealBot_Aggro[hbGUID]="True"
            end
        end
        HealBot_UnitThreat[hbGUID]=UnitDebuffStatus[hbGUID]
        status=true
    else
        HealBot_UnitThreat[hbGUID]=threatStatus
        HealBot_Aggro[hbGUID]=status
    end
    if not status then
        if HealBot_AggroBar4[hbGUID] then
            barName=HealBot_AggroBar4[hbGUID]
            barName:SetStatusBarColor(1,0,0,0)
--            barName:SetValue(0)
            HealBot_AggroBar4[hbGUID]=nil
        end
    elseif Healbot_Config_Skins.ShowAggroBars[Healbot_Config_Skins.Current_Skin]==1 then
        if not HealBot_AggroBar4[hbGUID] then
            HealBot_AggroBar4[hbGUID]=barName
--            barName:SetValue(100)
--            HealBot_Action_RefreshButtons(hbGUID)
        elseif HealBot_AggroBar4[hbGUID]~=barName then
            barTemp=HealBot_AggroBar4[hbGUID]
            barTemp:SetStatusBarColor(1,0,0,0)
            HealBot_AggroBar4[hbGUID]=barName
--            HealBot_Action_RefreshButtons(hbGUID)
        end
    end
end

function HealBot_Action_SetVAggro(hbGUID, status)
    if status and hbGUID then
        HealBot_Aggro[hbGUID]=true
    elseif hbGUID then
        HealBot_Aggro[hbGUID]=nil
    end
end

function HealBot_Action_retAggro(hbGUID)
    return HealBot_Aggro[hbGUID]
end

function HealBot_Action_EndAggro()
    for hbGUID,_ in pairs(HealBot_Aggro) do
        if HealBot_Action_retAggro(hbGUID) and HealBot_UnitID[hbGUID] then
            HealBot_Action_UpdateAggro(HealBot_UnitID[hbGUID],false,nil,hbGUID)
        end
    end
end

function HealBot_Action_SetUnitDebuffStatus(hbGUID,status)
    if not status then
        UnitDebuffStatus[hbGUID]=nil
    else
        UnitDebuffStatus[hbGUID]=status
    end
end

function HealBot_Action_ClearUnitDebuffStatus(hbGUID)
    if hbGUID then
        UnitDebuffStatus[hbGUID]=nil
    else
        for x,_ in pairs(UnitDebuffStatus) do
            UnitDebuffStatus[x]=nil;
        end
    end
end

function HealBot_Action_RetUnitThreat(hbGUID)
    return HealBot_UnitThreat[hbGUID] or -1
end

function HealBot_Action_RetHealBot_UnitStatus(unit)
    return HealBot_UnitStatus[unit] or -9
end

function HealBot_Action_SetrSpell()
    HealBot_hSpell=HealBot_GetBandageType()
    HealBot_bSpell=HealBot_GetBandageType()
    HealBot_dSpell=HealBot_GetBandageType()
    HealBot_rSpell=HealBot_GetBandageType()
    if strsub(HealBot_PlayerClassEN,1,4)=="DRUI" then
        HealBot_hSpell=HEALBOT_REJUVENATION
        HealBot_bSpell=HEALBOT_MARK_OF_THE_WILD
        HealBot_dSpell=HEALBOT_CURE_POISON
        HealBot_rSpell=HEALBOT_REVIVE
    elseif strsub(HealBot_PlayerClassEN,1,4)=="HUNT" then
        HealBot_hSpell=HEALBOT_MENDPET
    elseif strsub(HealBot_PlayerClassEN,1,4)=="MAGE" then
        HealBot_bSpell=HEALBOT_ARCANE_INTELLECT
        HealBot_dSpell=HEALBOT_REMOVE_LESSER_CURSE
    elseif strsub(HealBot_PlayerClassEN,1,4)=="PALA" then
        HealBot_hSpell=HEALBOT_HOLY_LIGHT
        HealBot_bSpell=HEALBOT_BLESSING_OF_MIGHT
        HealBot_dSpell=HEALBOT_PURIFY
        HealBot_rSpell=HEALBOT_REDEMPTION
    elseif strsub(HealBot_PlayerClassEN,1,4)=="PRIE" then
        HealBot_hSpell=HEALBOT_LESSER_HEAL
        HealBot_bSpell=HEALBOT_POWER_WORD_FORTITUDE
        HealBot_dSpell=HEALBOT_CURE_DISEASE
        HealBot_rSpell=HEALBOT_RESURRECTION
    elseif strsub(HealBot_PlayerClassEN,1,4)=="SHAM" then
        HealBot_hSpell=HEALBOT_HEALING_WAVE
        HealBot_bSpell=HEALBOT_EARTH_SHIELD
        HealBot_dSpell=HEALBOT_CURE_DISEASE
        HealBot_rSpell=HEALBOT_ANCESTRALSPIRIT
    elseif strsub(HealBot_PlayerClassEN,1,4)=="WARL" then
        HealBot_bSpell=HEALBOT_UNENDING_BREATH
        HealBot_hSpell=HEALBOT_HEALTH_FUNNEL
    elseif strsub(HealBot_PlayerClassEN,1,4)=="WARR" then
        if HealBot_GetSpellId(HEALBOT_VIGILANCE) then
            HealBot_hSpell=HEALBOT_VIGILANCE
            HealBot_bSpell=HEALBOT_VIGILANCE
        end
    end
--    HealBot_SetrSpells(HealBot_hSpell,HealBot_bSpell,HealBot_dSpell,HealBot_rSpell)
end

function HealBot_GetBandageType()
local level = UnitLevel("player")
local bandage = GetItemInfo(14530); -- Heavy Runecloth
    if level > 70 then
        bandage = GetItemInfo(34722); -- Heavy Frostweave
    elseif level > 60 then
        bandage = GetItemInfo(21991); -- Heavy Netherweave
    end
    return bandage
end

local hcr=nil
local hcg=nil
local hcb=nil
local hca=nil
local hcta=nil
local hcpct=nil
local hrpct=100
function HealBot_HealthColor(unit,hlth,maxhlth,tooltipcol,hbGUID,UnitDead,Member_Buff,Member_Debuff,healin)
    if UnitDead then
        hcpct=0
        hrpct=0
    else
        hcpct = hlth+healin
        if hcpct<maxhlth then
            hcpct=hcpct/maxhlth
        else
            hcpct=1;
        end
        if maxhlth == 0 then
            hrpct = 100;
        else
            hrpct=floor((hlth/maxhlth)*100)
        end
    end
  
    if not tooltipcol then
        HealBot_UnitStatus[unit]=1
        if UnitDead then
            HealBot_UnitStatus[unit]=8
        elseif Member_Debuff and UnitIsConnected(unit) then
            if HealBot_Config.CDCshownHB==1 then
                return HealBot_Config.CDCBarColour[Member_Debuff].R,
                       HealBot_Config.CDCBarColour[Member_Debuff].G,
                       HealBot_Config.CDCBarColour[Member_Debuff].B,
                       Healbot_Config_Skins.bareora[Healbot_Config_Skins.Current_Skin],
                       hrpct,
                       Healbot_Config_Skins.btextenabledcola[Healbot_Config_Skins.Current_Skin]
            end
        elseif Member_Buff and UnitIsConnected(unit) then
            hcr,hcg,hcb=HealBot_Options_RetBuffRGB(Member_Buff)
            return hcr,
                   hcg,
                   hcb,
                   Healbot_Config_Skins.bareora[Healbot_Config_Skins.Current_Skin],
                   hrpct,
                   Healbot_Config_Skins.btextenabledcola[Healbot_Config_Skins.Current_Skin]
        elseif hlth>maxhlth*HealBot_Config.AlertLevel and healin==0 and not HealBot_Aggro[hbGUID] then
            vUnit=HealBot_retIsInVehicle(unit)
            if vUnit then
                x,y=HealBot_VehicleHealth(vUnit)
                if x>y*HealBot_Config.AlertLevel and not HealBot_Aggro[HealBot_UnitGUID(vUnit)] then
                    HealBot_UnitStatus[unit]=0;
                end
            else
                HealBot_UnitStatus[unit]=0;
            end
        end
    end

    hcr,hcg,hcb = 1.0, 1.0, 0.0;
    if HealBot_UnitStatus[unit]~=0 then --(hcpct<=HealBot_Config.AlertLevel or HealBot_Aggro[hbGUID]) then -- 
        hca=Healbot_Config_Skins.bareora[Healbot_Config_Skins.Current_Skin]
        hcta=Healbot_Config_Skins.btextenabledcola[Healbot_Config_Skins.Current_Skin]
    else
        hca=Healbot_Config_Skins.bardisa[Healbot_Config_Skins.Current_Skin]
        hcta=Healbot_Config_Skins.btextdisbledcola[Healbot_Config_Skins.Current_Skin]
    end
  
    if (Healbot_Config_Skins.SetBarClassColour[Healbot_Config_Skins.Current_Skin] == 1) and not tooltipcol then
        if (Healbot_Config_Skins.SetBarCustomColour[Healbot_Config_Skins.Current_Skin] == 1) then
            hcr,hcg,hcb = HealBot_Action_ClassColour(hbGUID, unit)
        else
            hcr=Healbot_Config_Skins.barcolr[Healbot_Config_Skins.Current_Skin]
            hcg=Healbot_Config_Skins.barcolg[Healbot_Config_Skins.Current_Skin]
            hcb=Healbot_Config_Skins.barcolb[Healbot_Config_Skins.Current_Skin]
        end
    else
        if hcpct>=0.98 then 
            hcr = 0.0
        elseif hcpct<0.98 and hcpct>=0.65 then 
            hcr=2.94-(hcpct*3)
        elseif hcpct<=0.64 and hcpct>0.31 then 
            hcg=(hcpct-0.31)*3
        elseif hcpct<=0.31 then 
            hcg = 0.0 
        end
    end

    return hcr,hcg,hcb,hca,hrpct,hcta
end

function HealBot_Action_HealthBar(button)
    if not button then return end
    barName = button:GetName();
    return getglobal(barName.."Bar");
end

function HealBot_Action_HealthBar2(button)
    if not button then return end
    barName = button:GetName();
    return getglobal(barName.."Bar2");
end

function HealBot_Action_HealthBar3(button)
    if not button then return end
    barName = button:GetName();
    return getglobal(barName.."Bar3");
end

function HealBot_Action_HealthBar4(button)
    if not button then return end
    barName = button:GetName();
    return getglobal(barName.."Bar4");
end

function HealBot_Action_ShouldHealSome(hbGUID)
    if HealBot_Enabled[hbGUID] then 
        return true
    else
        for xUnit,_ in pairs(HealBot_Unit_Button) do
            if HealBot_Enabled[HealBot_UnitGUID(xUnit)] then
                return true
            end
        end
    end
end

function HealBot_MayHeal(unit, unitName)
    if unitName then
        if unit ~= 'target' then 
            return true 
        end
        if UnitCanAttack("player",unit) then 
            return false 
        end
        return true;
    else
        return false;
    end
end

function HealBot_Action_RefreshBar3(unit)
    HealBot_Action_SetBar3Value(HealBot_Unit_Button[unit]);
end

function HealBot_Action_SetBar3Value(button)
    if not button then return end
    barName = HealBot_Unit_Bar3[button.unit]
    if UnitManaMax(button.unit)==0 then
        x=100
    else
        x=UnitManaMax(button.unit)
    end
    y=floor((UnitMana(button.unit)/x)*100)
    hcr,hcg,hcb=HealBot_Action_GetManaBarCol(button.unit)
    barName:SetValue(y);
    if HealBot_UnitStatus[button.unit]==0 or HealBot_UnitInRange(HealBot_hSpell, button.unit)<1 then
        barName:SetStatusBarColor(hcr,hcg,hcb,Healbot_Config_Skins.bardisa[Healbot_Config_Skins.Current_Skin])
    else
        barName:SetStatusBarColor(hcr,hcg,hcb,1)
    end
end

function HealBot_Action_GetManaBarCol(unit)
    z=UnitPowerType(unit);
    if z==0 then
        return 0,0,1
    elseif z==1 then
        return 1,0,0
    elseif z==4 then
        return 0,1,1
    elseif z==6 then
        return 0,0.8,1
    end
    return 1,1,0
end

function HealBot_CorrectPetHealth(unit,hlth,maxhlth,hbGUID)
    if not hbGUID then return maxhlth end
    if not HealBot_PetMaxH[hbGUID] then
        HealBot_PetMaxH[hbGUID]=hlth;
    elseif hlth>HealBot_PetMaxH[hbGUID] then
        HealBot_PetMaxH[hbGUID]=hlth;
    elseif not HealBot_IsFighting then
        if not HealBot_PetMaxHcnt1[hbGUID] then
            HealBot_PetMaxHcnt1[hbGUID]=1
            HealBot_PetMaxHcnt2[hbGUID]=HealBot_PetMaxH[hbGUID];
        else
            if HealBot_PetMaxHcnt2[hbGUID]~=hlth then
                HealBot_PetMaxHcnt2[hbGUID]=hlth;
                HealBot_PetMaxHcnt1[hbGUID]=1;
            else
                HealBot_PetMaxHcnt1[hbGUID]=HealBot_PetMaxHcnt1[hbGUID]+1;
                if HealBot_PetMaxHcnt1[hbGUID]>20 then
                    HealBot_PetMaxH[hbGUID]=HealBot_PetMaxHcnt2[hbGUID];
                    HealBot_PetMaxHcnt1[hbGUID]=1;
                end
            end
        end
    end
    return HealBot_PetMaxH[hbGUID]
end

local ebubar=nil
local ebubar2=nil
local ebuicon8=nil
local ebusr=nil
local ebusg=nil
local ebusb=nil
local ebusa=nil
local ebur=nil
local ebug=nil
local ebub=nil
local ebua=nil
local ebpct=1
local ebipct=0
local ebtext=nil
local ebub2=nil
local ebufastenable=nil
local ebuProcessThis=nil
local ebuUnitDead=nil
local ebuHealBot_UnitDebuff=nil
local ebuHealBot_UnitBuff=nil
local ebuUnit_BuffRange=false
local ebuUnit_HealRange=false
function HealBot_Action_EnableButton(button, hbGUID)

    ebUnit=button.unit
    uName=UnitName(ebUnit)
--    if not uName then return end

    if not uName then 
        uName=HEALBOT_WORDS_UNKNOWN
    end

    ebubar = HealBot_Unit_Bar1[ebUnit]
    ebubar2 = HealBot_Unit_Bar2[ebUnit]
    ebuicon8 = getglobal(ebubar:GetName().."Icon8");
    uHlth,uMaxHlth=HealBot_UnitHealth(hbGUID, ebUnit)
    HealBot_UnitRangeSpell[ebUnit]=HealBot_hSpell

    if uName~=HEALBOT_WORDS_UNKNOWN then
    
        ebuUnitDead = UnitIsDeadOrGhost(ebUnit)
        ebuHealBot_UnitDebuff=HealBot_UnitDebuff[hbGUID]
        ebuHealBot_UnitBuff=HealBot_UnitBuff[hbGUID]
        uHealIn = HealBot_RetHealsIn(hbGUID)
    
        if ebuUnitDead then
            if UnitIsFeignDeath(ebUnit) then
                ebuUnitDead = nil
            else
                if uHealIn>0 then
                    uHealIn=0
                end
                if HealBot_Aggro[hbGUID] then
                    HealBot_Action_SetUnitDebuffStatus(hbGUID)
                    HealBot_Action_UpdateAggro(ebUnit,false,nil,hbGUID)
                    HealBot_Aggro[hbGUID]=nil
                end
                if HealBot_UnitDebuff[hbGUID] then
                    HealBot_UnitDebuff[hbGUID]=nil
                end
                if HealBot_HoT_Active_Button[hbGUID] then HealBot_HoT_RemoveIcon(hbGUID) end
            end
            if uName==HealBot_PlayerName and not HealBot_PlayerDead then
                HealBot_Action_ResetActiveUnitStatus()
                HealBot_PlayerDead=true
            end
        elseif uName==HealBot_PlayerName and HealBot_PlayerDead then
            HealBot_Action_ResetActiveUnitStatus()
            HealBot_PlayerDead=false
        end

        if uHealIn>0 then
            ebipct = uHlth+uHealIn
            if ebipct<uMaxHlth then
                ebipct=ebipct/uMaxHlth
            else
                ebipct=1;
            end
            ebipct=floor(ebipct*100)
            ebubar2:SetValue(ebipct);
        elseif ebubar2:GetValue()>0 then
            ebubar2:SetValue(0)
        end    
    
        ebur,ebug,ebub,ebua,ebpct,ebusa = HealBot_HealthColor(ebUnit,uHlth,uMaxHlth,false,hbGUID,ebuUnitDead,ebuHealBot_UnitBuff,ebuHealBot_UnitDebuff,uHealIn)
        if Healbot_Config_Skins.SetClassColourText[Healbot_Config_Skins.Current_Skin]==1 then
            ebusr,ebusg,ebusb = HealBot_Action_ClassColour(hbGUID, ebUnit);
        elseif ebuHealBot_UnitDebuff then
            ebusr=Healbot_Config_Skins.btextcursecolr[Healbot_Config_Skins.Current_Skin];
            ebusg=Healbot_Config_Skins.btextcursecolg[Healbot_Config_Skins.Current_Skin];
            ebusb=Healbot_Config_Skins.btextcursecolb[Healbot_Config_Skins.Current_Skin];
        else
            ebusr=Healbot_Config_Skins.btextenabledcolr[Healbot_Config_Skins.Current_Skin] or 0;
            ebusg=Healbot_Config_Skins.btextenabledcolg[Healbot_Config_Skins.Current_Skin] or 0;
            ebusb=Healbot_Config_Skins.btextenabledcolb[Healbot_Config_Skins.Current_Skin] or 0;
        end
    
        if HealBot_UnitInRange(HealBot_bSpell, ebUnit)==1 then
            ebuUnit_BuffRange=true
            ebuUnit_HealRange=true
        elseif HealBot_UnitInRange(HealBot_hSpell, ebUnit)==1 then
            ebuUnit_BuffRange=false
            ebuUnit_HealRange=true
        else
            ebuUnit_BuffRange=false
            ebuUnit_HealRange=false
        end

        if uMaxHlth<1 then 
            uHlth=1 
        end
        if uHlth>uMaxHlth then
            uMaxHlth=HealBot_CorrectPetHealth(ebUnit,uHlth,uMaxHlth,hbGUID)
        end
  
        if Healbot_Config_Skins.UseFluidBars[Healbot_Config_Skins.Current_Skin]==0 then
            if ebuUnitDead then
                ebubar:SetValue(0)
            else
                ebubar:SetValue(ebpct)
            end
        else
            if HealBot_curUnitHealth[ebubar]~=ebpct then
                if ebuUnitDead then
                    ebubar:SetValue(0)
                else
                    HealBot_UnitBarUpdate[ebubar]=HealBot_curUnitHealth[ebubar]
                end
            end
            HealBot_curUnitHealth[ebubar]=ebpct
        end
    
        ebuProcessThis=true
        ebufastenable=false
        if HealBot_Config.ProtectPvP==1 then
            if UnitIsPVP(ebUnit) and not UnitIsPVP("player") then 
                ebuProcessThis=false
            end
        end
        if not ebuUnitDead and not HealBot_PlayerDead and ebuProcessThis then
            if ebuHealBot_UnitDebuff then
                HealBot_UnitRangeSpell[ebUnit]=HealBot_dSpell
                if ebuUnit_BuffRange then ebufastenable=true end
            elseif ebuHealBot_UnitBuff then
                HealBot_UnitRangeSpell[ebUnit]=HealBot_bSpell
                if ebuUnit_BuffRange then ebufastenable=true end
            end
            if not ebufastenable and (uHlth<=(uMaxHlth*HealBot_Config.AlertLevel) or HealBot_Aggro[hbGUID] or HealBot_MyTargets[hbGUID]) and ebuUnit_HealRange then
                ebufastenable=true
                HealBot_UnitRangeSpell[ebUnit]=HealBot_hSpell
            end
        end
        if ebufastenable then
            ebusa = Healbot_Config_Skins.btextenabledcola[Healbot_Config_Skins.Current_Skin];
            HealBot_Enabled[hbGUID]=true
            ebubar:SetStatusBarColor(ebur,ebug,ebub,Healbot_Config_Skins.Barcola[Healbot_Config_Skins.Current_Skin]);
            ebubar2:SetStatusBarColor(ebur,ebug,ebub,Healbot_Config_Skins.BarcolaInHeal[Healbot_Config_Skins.Current_Skin]);
            if Healbot_Config_Skins.ShowClassOnBar[Healbot_Config_Skins.Current_Skin]==1 and Healbot_Config_Skins.ShowClassType[Healbot_Config_Skins.Current_Skin]==1 then
                ebuicon8:SetAlpha(Healbot_Config_Skins.Barcola[Healbot_Config_Skins.Current_Skin]);
            end
            HealBot_UnitRangeitr[ebUnit]=ebusr
            HealBot_UnitRangeitg[ebUnit]=ebusg
            HealBot_UnitRangeitb[ebUnit]=ebusb
            HealBot_UnitRangeotr[ebUnit]=Healbot_Config_Skins.btextdisbledcolr[Healbot_Config_Skins.Current_Skin]
            HealBot_UnitRangeotg[ebUnit]=Healbot_Config_Skins.btextdisbledcolg[Healbot_Config_Skins.Current_Skin]
            HealBot_UnitRangeotb[ebUnit]=Healbot_Config_Skins.btextdisbledcolb[Healbot_Config_Skins.Current_Skin]
            if ebUnit==HealBot_Action_DisableTooltipUnit then
                HealBot_Action_TooltipUnit = ebUnit;
                HealBot_Action_DisableTooltipUnit = nil;
            end
            if ebUnit==HealBot_Action_TooltipUnit then
                HealBot_Action_RefreshTooltip(ebUnit,"Enabled")
            end
        else
            HealBot_Enabled[hbGUID]=false
            if Healbot_Config_Skins.SetClassColourText[Healbot_Config_Skins.Current_Skin]==0 then
                ebusr=Healbot_Config_Skins.btextdisbledcolr[Healbot_Config_Skins.Current_Skin]
                ebusg=Healbot_Config_Skins.btextdisbledcolg[Healbot_Config_Skins.Current_Skin]
                ebusb=Healbot_Config_Skins.btextdisbledcolb[Healbot_Config_Skins.Current_Skin]
            end
            if HealBot_RetHealBot_Ressing(hbGUID) then
                if ebuUnitDead then
                    ebusr=0.2
                    ebusg=1.0
                    ebusb=0.2
                    ebusa=1
                else
                    HealBot_UnsetHealBot_Ressing(hbGUID)
                end
                HealBot_UnitRangeitr[ebUnit]=0.2
                HealBot_UnitRangeitg[ebUnit]=1
                HealBot_UnitRangeitb[ebUnit]=0.2
                HealBot_UnitRangeotr[ebUnit]=0.2
                HealBot_UnitRangeotg[ebUnit]=1
                HealBot_UnitRangeotb[ebUnit]=0.2
            elseif ebuUnitDead and uName~=HealBot_PlayerName then
                if HealBot_rSpell then
                    HealBot_UnitRangeSpell[ebUnit]=HealBot_rSpell
                    if ebuUnit_BuffRange and not UnitIsGhost(ebUnit) then
                        ebusr=0;
                        ebusg=0;
                        ebusb=0;
                        ebusa=1;
                    else
                        ebusr=0.7;
                        ebusg=0.7;
                        ebusb=0.7;
                        ebusa=1;
                    end
                    if not UnitIsGhost(ebUnit) then
                        HealBot_UnitRangeitr[ebUnit]=0
                        HealBot_UnitRangeitg[ebUnit]=0
                        HealBot_UnitRangeitb[ebUnit]=0
                    else
                        HealBot_UnitRangeitr[ebUnit]=0.7
                        HealBot_UnitRangeitg[ebUnit]=0.7
                        HealBot_UnitRangeitb[ebUnit]=0.7
                    end
                    HealBot_UnitRangeotr[ebUnit]=0.7
                    HealBot_UnitRangeotg[ebUnit]=0.7
                    HealBot_UnitRangeotb[ebUnit]=0.7
                end
            else
                HealBot_UnitRangeitr[ebUnit]=ebusr
                HealBot_UnitRangeitg[ebUnit]=ebusg
                HealBot_UnitRangeitb[ebUnit]=ebusb
                HealBot_UnitRangeotr[ebUnit]=ebusr
                HealBot_UnitRangeotg[ebUnit]=ebusg
                HealBot_UnitRangeotb[ebUnit]=ebusb
                --ebua=Healbot_Config_Skins.bardisa[Healbot_Config_Skins.Current_Skin]
            end
            if UnitIsVisible(ebUnit) or HealBot_Config.NotVisibleDisable==0 then
                ebubar:SetStatusBarColor(ebur,ebug,ebub,ebua);
                ebubar2:SetStatusBarColor(ebur,ebug,ebub,ebua);
                if Healbot_Config_Skins.ShowClassOnBar[Healbot_Config_Skins.Current_Skin]==1 and Healbot_Config_Skins.ShowClassType[Healbot_Config_Skins.Current_Skin]==1 then
                    ebuicon8:SetAlpha(ebua);
                end
            else
                ebubar:SetStatusBarColor(ebur,ebug,ebub,Healbot_Config_Skins.bardisa[Healbot_Config_Skins.Current_Skin]);
                ebubar2:SetStatusBarColor(ebur,ebug,ebub,Healbot_Config_Skins.bardisa[Healbot_Config_Skins.Current_Skin]);
                if Healbot_Config_Skins.ShowClassOnBar[Healbot_Config_Skins.Current_Skin]==1 and Healbot_Config_Skins.ShowClassType[Healbot_Config_Skins.Current_Skin]==1 then
                    ebuicon8:SetAlpha(Healbot_Config_Skins.bardisa[Healbot_Config_Skins.Current_Skin]);
                end
            end
            if not HealBot_IsFighting and HealBot_Config.EnableHealthy==0 then
                if ebUnit==HealBot_Action_TooltipUnit then
                    HealBot_Action_TooltipUnit = nil;
                    HealBot_Action_DisableTooltipUnit = ebUnit;
                end
                if ebUnit==HealBot_Action_DisableTooltipUnit then
                    HealBot_Action_RefreshTooltip(ebUnit,"Disabled")
                end
            end
        end
        HealBot_UnitRanger[ebUnit]=ebur
        HealBot_UnitRangeg[ebUnit]=ebug
        HealBot_UnitRangeb[ebUnit]=ebub
        HealBot_UnitRangea[ebUnit]=ebua
        HealBot_UnitRangeota[ebUnit]=ebusa
    elseif Delay_RecalcParty<3 then
        Delay_RecalcParty=3
    end
  
    ebubar.txt = getglobal(ebubar:GetName().."_text");
    ebtext=HealBot_Action_HBText(uHlth,uMaxHlth,uName,ebUnit,uHealIn, hbGUID)
    ebubar.txt:SetText(ebtext);
    if UnitIsVisible(ebUnit) or HealBot_Config.NotVisibleDisable==0 then
        ebubar.txt:SetTextColor(ebusr,ebusg,ebusb,ebusa);
    else
        ebubar.txt:SetTextColor(ebusr,ebusg,ebusb,Healbot_Config_Skins.btextdisbledcola[Healbot_Config_Skins.Current_Skin]);
    end
  
    if Healbot_Config_Skins.bar2size[Healbot_Config_Skins.Current_Skin]>0 then 
        HealBot_Action_SetBar3Value(HealBot_Unit_Button[ebUnit]);
    end

   HealBot_UnitRange[ebUnit]=HealBot_UnitInRange(HealBot_UnitRangeSpell[ebUnit], ebUnit) 
end

local hpPerc=nil
local r,g,b=nil,nil,nil
function HealBot_Action_HBText(hlth,maxhlth,unitName,unit,healin, hbGUID)
    btHBbarText=" "
    if Healbot_Config_Skins.ShowClassOnBar[Healbot_Config_Skins.Current_Skin]==1 and Healbot_Config_Skins.ShowClassType[Healbot_Config_Skins.Current_Skin]==2 and UnitClass(unit) then
        if Healbot_Config_Skins.ShowNameOnBar[Healbot_Config_Skins.Current_Skin]==1 then
            uName=UnitClass(unit)..":"..unitName;
        else
            uName=UnitClass(unit);
        end
    elseif Healbot_Config_Skins.ShowNameOnBar[Healbot_Config_Skins.Current_Skin]==1 then
        uName=unitName;
    else
        uName=nil
    end
    
    bttextlen = floor(5+(((Healbot_Config_Skins.bwidth[Healbot_Config_Skins.Current_Skin]*1.88)/Healbot_Config_Skins.btextheight[Healbot_Config_Skins.Current_Skin])-(Healbot_Config_Skins.btextheight[Healbot_Config_Skins.Current_Skin]/2)))-2
    if Healbot_Config_Skins.ShowHealthOnBar[Healbot_Config_Skins.Current_Skin]==1 and maxhlth then
        if Healbot_Config_Skins.BarHealthType[Healbot_Config_Skins.Current_Skin]==1 then
            if Healbot_Config_Skins.BarHealthIncHeals[Healbot_Config_Skins.Current_Skin]==1 then
                healin = HealBot_RetHealsIn(hbGUID)
                if Healbot_Config_Skins.BarHealthSepHeals[Healbot_Config_Skins.Current_Skin]==0 then
                    bthlthdelta=(hlth+healin)-maxhlth;
                else
                    bthlthdelta=hlth-maxhlth;
                end
            else
                bthlthdelta=hlth-maxhlth;
            end
            if bthlthdelta>0 then
                btHBbarText=btHBbarText.." +"..bthlthdelta;
            else
                btHBbarText=btHBbarText.."("..bthlthdelta..")";
            end
        else
            if Healbot_Config_Skins.BarHealthIncHeals[Healbot_Config_Skins.Current_Skin]==1 then
                healin = HealBot_RetHealsIn(hbGUID);
                if Healbot_Config_Skins.BarHealthSepHeals[Healbot_Config_Skins.Current_Skin]==0 then
                    btHBbarText=btHBbarText.."("..floor(((hlth+healin)/maxhlth)*100).."%)"
                else
                    btHBbarText=btHBbarText.."("..floor((hlth/maxhlth)*100).."%)"
                end
            else
                btHBbarText=btHBbarText.."("..floor((hlth/maxhlth)*100).."%)"
            end
        end
        if Healbot_Config_Skins.BarHealthSepHeals[Healbot_Config_Skins.Current_Skin]==1 and healin>0 then
            btHBbarText=btHBbarText.." +"..healin
        end
    end
    
    if HealBot_Action_UnitIsOffline(hbGUID) and uName then
        uName=HEALBOT_DISCONNECTED_TEXT.." "..uName;
    end    -- added by Diacono of Ursin
    if HealBot_Aggro[hbGUID] and HealBot_Aggro[hbGUID]~="True" and Healbot_Config_Skins.ShowAggroText[Healbot_Config_Skins.Current_Skin]==1 and uName then
        uName=">> "..uName.." <<"
    end
    vUnit=HealBot_retIsInVehicle(unit)
    if vUnit then
        x,y=HealBot_VehicleHealth(vUnit)
        hpPerc = floor((x/y)*100)
        if hpPerc < 0 then 
            hpPerc = 0
        end
        bthlthdelta="  "..string.format("|cff%s%d%%|r", HealBot_PercentToHexColor(hpPerc), hpPerc)
        if bttextlen>9 then 
            y=bttextlen-7
        else
            y=3
        end
        if UnitName(vUnit) and string.utf8len(UnitName(vUnit))>y then
            bthlthdelta = string.utf8sub(UnitName(vUnit),1,y).. '..'..bthlthdelta
        elseif UnitExists(vUnit) then
            bthlthdelta = UnitName(vUnit)..bthlthdelta
        else
            HealBot_CheckAllUnitVehicle(unit)
            if HealBot_retIsInVehicle(unit) then
                bthlthdelta = HEALBOT_VEHICLE..bthlthdelta
            end
        end
    end
    if Healbot_Config_Skins.DoubleText[Healbot_Config_Skins.Current_Skin]==0 then
        bttextlen=bttextlen-string.utf8len(btHBbarText)
        if bttextlen<1 then 
            bttextlen=1
        end
        if uName and string.utf8len(uName)>bttextlen then
            btHBbarText = string.utf8sub(uName,1,bttextlen) .. '..'..btHBbarText;
        elseif uName then
            btHBbarText = uName..btHBbarText;
        end
        if vUnit then
            btHBbarText = btHBbarText.."\n".." "..bthlthdelta
        end
    else
        if bttextlen<1 then 
            bttextlen=1
        end
        if uName and string.utf8len(uName)>bttextlen then
            btHBbarText = string.utf8sub(uName,1,bttextlen) .. '..'.."\n"..btHBbarText;
        elseif uName then
            btHBbarText = uName.."\n"..btHBbarText;
        end
        if vUnit then
            btHBbarText = btHBbarText.." "..bthlthdelta
        end
    end
    return btHBbarText;
end

function HealBot_PercentToHexColor(percent)
    percent = percent/100
    r,g,b = 2-percent*2,1,0
    if percent <= 0 then
        r,g,b = 1,0,0
    elseif percent <= 0.5 then
        r,g,b = 1,percent*2,0
    elseif percent >= 1 then
        r,g,b = 0,1,0
    end 
    return string.format("%02x%02x%02x",r*255,g*255,b*255)
end

function HealBot_Action_UnitIsOffline(hbGUID,preset) -- added by Diacono of Ursin
    x = nil;
    if not HealBot_UnitID[hbGUID] then return end
    if preset then
        HealBot_UnitOffline[hbGUID]=preset
    else
        if HealBot_UnitOffline[hbGUID] then
            if time() - HealBot_UnitOffline[hbGUID] <= 2 then
                x = true;
            end
        end
        if not UnitIsConnected(HealBot_UnitID[hbGUID]) or x then
            if not HealBot_UnitOffline[hbGUID] then
                HealBot_UnitOffline[hbGUID] = time();
            elseif HealBot_UnitOffline[hbGUID] == -1 then
                HealBot_UnitOffline[hbGUID] = nil;
            end
        else
            HealBot_UnitOffline[hbGUID] = nil;
        end
        return HealBot_UnitOffline[hbGUID]
    end
end

function HealBot_Action_retUnitOffline(hbGUID)
    return HealBot_UnitOffline[hbGUID]
end

function HealBot_Action_RefreshButton(button, hbGUID)
    if not button or not hbGUID then return end
--  if type(button)~="table" then DEFAULT_CHAT_FRAME:AddMessage("***** "..type(button)) end
    HealBot_Action_EnableButton(button, hbGUID)
    if HealBot_UnitName[HealBot_UnitGUID("target")] and HealBot_UnitGUID("target")==hbGUID and button.unit~="target" and HealBot_Unit_Button["target"] then
        HealBot_Action_EnableButton(HealBot_Unit_Button["target"], hbGUID)
    end
end

function HealBot_Action_ResetSkin()
local bwidth = Healbot_Config_Skins.bwidth[Healbot_Config_Skins.Current_Skin];
local bheight=Healbot_Config_Skins.bheight[Healbot_Config_Skins.Current_Skin];
local br=Healbot_Config_Skins.headbarcolr[Healbot_Config_Skins.Current_Skin];  
local bg=Healbot_Config_Skins.headbarcolg[Healbot_Config_Skins.Current_Skin];
local bb=Healbot_Config_Skins.headbarcolb[Healbot_Config_Skins.Current_Skin];
local ba=Healbot_Config_Skins.headbarcola[Healbot_Config_Skins.Current_Skin];
local sr=Healbot_Config_Skins.headtxtcolr[Healbot_Config_Skins.Current_Skin];  
local sg=Healbot_Config_Skins.headtxtcolg[Healbot_Config_Skins.Current_Skin];
local sb=Healbot_Config_Skins.headtxtcolb[Healbot_Config_Skins.Current_Skin];
local sa=Healbot_Config_Skins.headtxtcola[Healbot_Config_Skins.Current_Skin];
local btexture=Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin];
local btextheight=Healbot_Config_Skins.btextheight[Healbot_Config_Skins.Current_Skin]; 
local b,bar,bar2,bar3,bar4,icon1,icon2,icon3,icon4,icon5,icon6,icon7,icon8,icon9,icon1t,icon2t,icon3t,icon4t,icon1ta,icon2ta,icon3ta,icon4ta,barScale,h,hwidth,hheight
local iScale = (bheight*Healbot_Config_Skins.IconScale[Healbot_Config_Skins.Current_Skin])-2
local itScale=ceil(iScale*Healbot_Config_Skins.IconTextScale[Healbot_Config_Skins.Current_Skin])
local abSize = Healbot_Config_Skins.AggroBarSize[Healbot_Config_Skins.Current_Skin] or 2
local abtSize = {[0]=1,[1]=1,[2]=1,[3]=2,[4]=2,[5]=2,[6]=3,[7]=3,[8]=3,[9]=3,[10]=4,[11]=4,[12]=4,[13]=4,[14]=4,[15]=5}
    HealBot_Tooltip:SetBackdropColor(0,0,0,HealBot_Config.ttalpha)
  
    if Healbot_Config_Skins.ShowAggro[Healbot_Config_Skins.Current_Skin]==0 or Healbot_Config_Skins.ShowAggroBars[Healbot_Config_Skins.Current_Skin]==0 then abSize=0 end
  
    for x=1,51 do
        b=getglobal("HealBot_Action_HealUnit"..x);
        bar = HealBot_Action_HealthBar(b);
        bar2 = HealBot_Action_HealthBar2(b);
        bar3 = HealBot_Action_HealthBar3(b);
        bar4 = HealBot_Action_HealthBar4(b)
        icon1 = getglobal(bar:GetName().."Icon1");
        icon2 = getglobal(bar:GetName().."Icon2");
        icon3 = getglobal(bar:GetName().."Icon3");
        icon4 = getglobal(bar:GetName().."Icon4");
        icon5 = getglobal(bar:GetName().."Icon5");
        icon6 = getglobal(bar:GetName().."Icon6");
        icon7 = getglobal(bar:GetName().."Icon7");
        icon8 = getglobal(bar:GetName().."Icon8");
        icon9 = getglobal(bar:GetName().."Icon9");
        icon1t = getglobal(bar:GetName().."Count1");
        icon2t = getglobal(bar:GetName().."Count2");
        icon3t = getglobal(bar:GetName().."Count3"); 
        icon4t = getglobal(bar:GetName().."Count4"); 
        icon5t = getglobal(bar:GetName().."Count5"); 
        icon6t = getglobal(bar:GetName().."Count6"); 
        icon7t = getglobal(bar:GetName().."Count7"); 
        icon1ta = getglobal(bar:GetName().."Count1a");
        icon2ta = getglobal(bar:GetName().."Count2a");
        icon3ta = getglobal(bar:GetName().."Count3a"); 
        icon4ta = getglobal(bar:GetName().."Count4a"); 
        icon5ta = getglobal(bar:GetName().."Count5a"); 
        icon6ta = getglobal(bar:GetName().."Count6a"); 
        icon7ta = getglobal(bar:GetName().."Count7a"); 
        HealBot_Panel_SetBarArrays(b)
        bar.txt = getglobal(bar:GetName().."_text");
        bar:SetHeight(bheight);
        bar:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
        bar.txt:SetFont(LSM:Fetch('font',Healbot_Config_Skins.btextfont[Healbot_Config_Skins.Current_Skin]),btextheight,"");
        if Healbot_Config_Skins.TextAlignment[Healbot_Config_Skins.Current_Skin]==1 then
            bar.txt:SetPoint("LEFT",bar,"LEFT",4,0)
        elseif Healbot_Config_Skins.TextAlignment[Healbot_Config_Skins.Current_Skin]==2 then
            bar.txt:SetPoint("CENTER",bar,"CENTER")
        else
            bar.txt:SetPoint("RIGHT",bar,"RIGHT",-4,0)
        end
    --bar.txt:SetTextHeight(btextheight);
        bar2:SetHeight(bheight);
        bar2:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
        bar3:SetHeight(Healbot_Config_Skins.bar2size[Healbot_Config_Skins.Current_Skin]);
        bar3:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
        bar4:ClearAllPoints();
        bar4:SetPoint("TOPLEFT",bar,"TOPLEFT",0,abSize);
        bar4:SetPoint("TOPRIGHT",bar,"TOPRIGHT",0,abSize);
        if Healbot_Config_Skins.bar2size[Healbot_Config_Skins.Current_Skin]==0 then
            bar4:SetHeight(bheight+(abSize*2))
        else
            bar4:SetHeight(Healbot_Config_Skins.bar2size[Healbot_Config_Skins.Current_Skin]+bheight+(abSize*2))
        end
        bar4:SetStatusBarTexture('Interface\\Addons\\HealBot\\Images\\aggro'..abtSize[abSize]..'.tga')
        bar4:SetStatusBarColor(1,0,0,0)
        bar4:SetMinMaxValues(0,1)
        bar4:SetValue(1)
        b:SetHeight(bheight);  
        b:Enable();
        b.guid="init"
        icon1:SetHeight(iScale);
        icon1:SetWidth(iScale);
        icon2:SetHeight(iScale);
        icon2:SetWidth(iScale);
        icon3:SetHeight(iScale);
        icon3:SetWidth(iScale);
        icon4:SetHeight(iScale);
        icon4:SetWidth(iScale);
        icon5:SetHeight(iScale);
        icon5:SetWidth(iScale);
        icon6:SetHeight(iScale);
        icon6:SetWidth(iScale);
        icon7:SetHeight(iScale);
        icon7:SetWidth(iScale);
        icon8:SetHeight(iScale);
        icon8:SetWidth(iScale);
        icon9:SetHeight(iScale);
        icon9:SetWidth(iScale);
        icon1t:SetTextHeight(itScale)
        icon1ta:SetTextHeight(itScale)
        icon2t:SetTextHeight(itScale)
        icon2ta:SetTextHeight(itScale)
        icon3t:SetTextHeight(itScale)
        icon3ta:SetTextHeight(itScale)
        icon4t:SetTextHeight(itScale)
        icon4ta:SetTextHeight(itScale)
        icon5t:SetTextHeight(itScale)
        icon5ta:SetTextHeight(itScale)
        icon6t:SetTextHeight(itScale)
        icon6ta:SetTextHeight(itScale)
        icon7t:SetTextHeight(itScale)
        icon7ta:SetTextHeight(itScale)
        bar:SetMinMaxValues(0,100)
        bar2:SetMinMaxValues(0,100);
        bar3:SetMinMaxValues(0,100)
        bar2:SetValue(0);
        bar:SetStatusBarColor(0,1,0,Healbot_Config_Skins.bardisa[Healbot_Config_Skins.Current_Skin]);
        bar2:SetStatusBarColor(0,1,0,0);
        if Healbot_Config_Skins.ShowClassOnBar[Healbot_Config_Skins.Current_Skin]==1 and Healbot_Config_Skins.ShowClassType[Healbot_Config_Skins.Current_Skin]==1 then
            icon8:SetTexture([[Interface\AddOns\HealBot\Images\icon_class.tga]]);
        else
            icon8:SetTexCoord(0,1,0,1);
            icon8:SetAlpha(0);
        end
        if Healbot_Config_Skins.HoTonBar[Healbot_Config_Skins.Current_Skin]==1 then
            HealBot_Panel_SetMultiColHoToffset(0)
            if Healbot_Config_Skins.HoTposBar[Healbot_Config_Skins.Current_Skin]==1 then
                icon1:ClearAllPoints();
                icon1:SetPoint("BOTTOMLEFT",b,"BOTTOMLEFT",1,0);
                icon2:ClearAllPoints();
                icon2:SetPoint("BOTTOMLEFT",icon1,"BOTTOMRIGHT",1,0);
                icon3:ClearAllPoints();
                icon3:SetPoint("BOTTOMLEFT",icon2,"BOTTOMRIGHT",1,0);
                icon4:ClearAllPoints();
                icon4:SetPoint("BOTTOMLEFT",icon3,"BOTTOMRIGHT",1,0);
                icon5:ClearAllPoints();
                icon5:SetPoint("BOTTOMLEFT",icon4,"BOTTOMRIGHT",1,0);
                icon6:ClearAllPoints();
                icon6:SetPoint("BOTTOMLEFT",icon5,"BOTTOMRIGHT",1,0);
                icon7:ClearAllPoints();
                icon7:SetPoint("BOTTOMLEFT",icon6,"BOTTOMRIGHT",1,0);
                icon8:ClearAllPoints();
                icon8:SetPoint("BOTTOMRIGHT",b,"BOTTOMRIGHT",-1,0);
                icon9:ClearAllPoints();
                icon9:SetPoint("BOTTOMRIGHT",icon8,"BOTTOMLEFT",-1,0);
                icon1t:ClearAllPoints();
                icon1t:SetPoint("BOTTOMLEFT",icon1,"BOTTOMLEFT",-2,0);
                icon1ta:ClearAllPoints();
                icon1ta:SetPoint("TOPRIGHT",icon1,"TOPRIGHT",4,0);
                icon2t:ClearAllPoints();
                icon2t:SetPoint("BOTTOMLEFT",icon2,"BOTTOMLEFT",-2,0);
                icon2ta:ClearAllPoints();
                icon2ta:SetPoint("TOPRIGHT",icon2,"TOPRIGHT",4,0);
                icon3t:ClearAllPoints();
                icon3t:SetPoint("BOTTOMLEFT",icon3,"BOTTOMLEFT",-2,0); 
                icon3ta:ClearAllPoints();
                icon3ta:SetPoint("TOPRIGHT",icon3,"TOPRIGHT",4,0);
                icon4t:ClearAllPoints();
                icon4t:SetPoint("BOTTOMLEFT",icon4,"BOTTOMLEFT",-2,0); 
                icon4ta:ClearAllPoints();
                icon4ta:SetPoint("TOPRIGHT",icon4,"TOPRIGHT",4,0);
                icon5t:ClearAllPoints();
                icon5t:SetPoint("BOTTOMLEFT",icon5,"BOTTOMLEFT",-2,0); 
                icon5ta:ClearAllPoints();
                icon5ta:SetPoint("TOPRIGHT",icon5,"TOPRIGHT",4,0);
                icon6t:ClearAllPoints();
                icon6t:SetPoint("BOTTOMLEFT",icon6,"BOTTOMLEFT",-2,0); 
                icon6ta:ClearAllPoints();
                icon6ta:SetPoint("TOPRIGHT",icon6,"TOPRIGHT",4,0);
                icon7t:ClearAllPoints();
                icon7t:SetPoint("BOTTOMLEFT",icon7,"BOTTOMLEFT",-2,0); 
                icon7ta:ClearAllPoints();
                icon7ta:SetPoint("TOPRIGHT",icon7,"TOPRIGHT",4,0);
            else
                icon1:ClearAllPoints();
                icon1:SetPoint("BOTTOMRIGHT",b,"BOTTOMRIGHT",-1,0);
                icon2:ClearAllPoints();
                icon2:SetPoint("BOTTOMRIGHT",icon1,"BOTTOMLEFT",-1,0);
                icon3:ClearAllPoints();
                icon3:SetPoint("BOTTOMRIGHT",icon2,"BOTTOMLEFT",-1,0);
                icon4:ClearAllPoints();
                icon4:SetPoint("BOTTOMRIGHT",icon3,"BOTTOMLEFT",-1,0);
                icon5:ClearAllPoints();
                icon5:SetPoint("BOTTOMRIGHT",icon4,"BOTTOMLEFT",-1,0);
                icon6:ClearAllPoints();
                icon6:SetPoint("BOTTOMRIGHT",icon5,"BOTTOMLEFT",-1,0);
                icon7:ClearAllPoints();
                icon7:SetPoint("BOTTOMRIGHT",icon6,"BOTTOMLEFT",-1,0);
                icon8:ClearAllPoints();
                icon8:SetPoint("BOTTOMLEFT",b,"BOTTOMLEFT",1,0);
                icon9:ClearAllPoints();
                icon9:SetPoint("BOTTOMLEFT",icon8,"BOTTOMRIGHT",1,0);
                icon1t:ClearAllPoints();
                icon1t:SetPoint("BOTTOMRIGHT",icon1,"BOTTOMRIGHT",4,0);
                icon1ta:ClearAllPoints();
                icon1ta:SetPoint("TOPLEFT",icon1,"TOPLEFT",-2,0);
                icon2t:ClearAllPoints();
                icon2t:SetPoint("BOTTOMRIGHT",icon2,"BOTTOMRIGHT",4,0);
                icon2ta:ClearAllPoints();
                icon2ta:SetPoint("TOPLEFT",icon2,"TOPLEFT",-2,0);
                icon3t:ClearAllPoints();
                icon3t:SetPoint("BOTTOMRIGHT",icon3,"BOTTOMRIGHT",4,0); 
                icon3ta:ClearAllPoints();
                icon3ta:SetPoint("TOPLEFT",icon3,"TOPLEFT",-2,0);
                icon4t:ClearAllPoints();
                icon4t:SetPoint("BOTTOMRIGHT",icon4,"BOTTOMRIGHT",4,0); 
                icon4ta:ClearAllPoints();
                icon4ta:SetPoint("TOPLEFT",icon4,"TOPLEFT",-2,0);
                icon5t:ClearAllPoints();
                icon5t:SetPoint("BOTTOMRIGHT",icon5,"BOTTOMRIGHT",4,0); 
                icon5ta:ClearAllPoints();
                icon5ta:SetPoint("TOPLEFT",icon5,"TOPLEFT",-2,0);
                icon6t:ClearAllPoints();
                icon6t:SetPoint("BOTTOMRIGHT",icon6,"BOTTOMRIGHT",4,0); 
                icon6ta:ClearAllPoints();
                icon6ta:SetPoint("TOPLEFT",icon6,"TOPLEFT",-2,0);
                icon7t:ClearAllPoints();
                icon7t:SetPoint("BOTTOMRIGHT",icon7,"BOTTOMRIGHT",4,0); 
                icon7ta:ClearAllPoints();
                icon7ta:SetPoint("TOPLEFT",icon7,"TOPLEFT",-2,0);
            end
        else
            HealBot_Panel_SetMultiColHoToffset((iScale+1)*5)
            if Healbot_Config_Skins.HoTposBar[Healbot_Config_Skins.Current_Skin]==1 then
                icon1:ClearAllPoints();
                icon1:SetPoint("BOTTOMRIGHT",b,"BOTTOMLEFT",-1,0);
                icon2:ClearAllPoints();
                icon2:SetPoint("BOTTOMRIGHT",icon1,"BOTTOMLEFT",-2,0);
                icon3:ClearAllPoints();
                icon3:SetPoint("BOTTOMRIGHT",icon2,"BOTTOMLEFT",-2,0);
                icon4:ClearAllPoints();
                icon4:SetPoint("BOTTOMRIGHT",icon3,"BOTTOMLEFT",-2,0);
                icon5:ClearAllPoints();
                icon5:SetPoint("BOTTOMRIGHT",icon4,"BOTTOMLEFT",-2,0);
                icon6:ClearAllPoints();
                icon6:SetPoint("BOTTOMRIGHT",icon5,"BOTTOMLEFT",-2,0);
                icon7:ClearAllPoints();
                icon7:SetPoint("BOTTOMRIGHT",icon6,"BOTTOMLEFT",-2,0);
                icon8:ClearAllPoints();
                icon8:SetPoint("BOTTOMLEFT",b,"BOTTOMRIGHT",2,0);
                icon9:ClearAllPoints();
                icon9:SetPoint("BOTTOMLEFT",icon8,"BOTTOMRIGHT",2,0);
                icon1t:ClearAllPoints();
                icon1t:SetPoint("BOTTOMRIGHT",icon1,"BOTTOMRIGHT",4,0);
                icon1ta:ClearAllPoints();
                icon1ta:SetPoint("TOPLEFT",icon1,"TOPLEFT",-2,0);
                icon2t:ClearAllPoints();
                icon2t:SetPoint("BOTTOMRIGHT",icon2,"BOTTOMRIGHT",4,0);
                icon2ta:ClearAllPoints();
                icon2ta:SetPoint("TOPLEFT",icon2,"TOPLEFT",-2,0);
                icon3t:ClearAllPoints();
                icon3t:SetPoint("BOTTOMRIGHT",icon3,"BOTTOMRIGHT",4,0); 
                icon3ta:ClearAllPoints();
                icon3ta:SetPoint("TOPLEFT",icon3,"TOPLEFT",-2,0);
                icon4t:ClearAllPoints();
                icon4t:SetPoint("BOTTOMRIGHT",icon4,"BOTTOMRIGHT",4,0); 
                icon4ta:ClearAllPoints();
                icon4ta:SetPoint("TOPLEFT",icon4,"TOPLEFT",-2,0);
                icon5t:ClearAllPoints();
                icon5t:SetPoint("BOTTOMRIGHT",icon5,"BOTTOMRIGHT",4,0); 
                icon5ta:ClearAllPoints();
                icon5ta:SetPoint("TOPLEFT",icon5,"TOPLEFT",-2,0);
                icon6t:ClearAllPoints();
                icon6t:SetPoint("BOTTOMRIGHT",icon6,"BOTTOMRIGHT",4,0); 
                icon6ta:ClearAllPoints();
                icon6ta:SetPoint("TOPLEFT",icon6,"TOPLEFT",-2,0);
                icon7t:ClearAllPoints();
                icon7t:SetPoint("BOTTOMRIGHT",icon7,"BOTTOMRIGHT",4,0); 
                icon7ta:ClearAllPoints();
                icon7ta:SetPoint("TOPLEFT",icon7,"TOPLEFT",-2,0);
            else
                icon1:ClearAllPoints();
                icon1:SetPoint("BOTTOMLEFT",b,"BOTTOMRIGHT",2,0);
                icon2:ClearAllPoints();
                icon2:SetPoint("BOTTOMLEFT",icon1,"BOTTOMRIGHT",2,0);
                icon3:ClearAllPoints();
                icon3:SetPoint("BOTTOMLEFT",icon2,"BOTTOMRIGHT",2,0);
                icon4:ClearAllPoints();
                icon4:SetPoint("BOTTOMLEFT",icon3,"BOTTOMRIGHT",2,0);
                icon5:ClearAllPoints();
                icon5:SetPoint("BOTTOMLEFT",icon4,"BOTTOMRIGHT",2,0);
                icon6:ClearAllPoints();
                icon6:SetPoint("BOTTOMLEFT",icon5,"BOTTOMRIGHT",2,0);
                icon7:ClearAllPoints();
                icon7:SetPoint("BOTTOMLEFT",icon6,"BOTTOMRIGHT",2,0);
                icon8:ClearAllPoints();
                icon8:SetPoint("BOTTOMRIGHT",b,"BOTTOMLEFT",-1,0);
                icon9:ClearAllPoints();
                icon9:SetPoint("BOTTOMRIGHT",icon8,"BOTTOMLEFT",-2,0);
                icon1t:ClearAllPoints();
                icon1t:SetPoint("BOTTOMLEFT",icon1,"BOTTOMLEFT",-2,0);
                icon1ta:ClearAllPoints();
                icon1ta:SetPoint("TOPRIGHT",icon1,"TOPRIGHT",4,0);
                icon2t:ClearAllPoints();
                icon2t:SetPoint("BOTTOMLEFT",icon2,"BOTTOMLEFT",-2,0);
                icon2ta:ClearAllPoints();
                icon2ta:SetPoint("TOPRIGHT",icon2,"TOPRIGHT",4,0);
                icon3t:ClearAllPoints();
                icon3t:SetPoint("BOTTOMLEFT",icon3,"BOTTOMLEFT",-2,0); 
                icon3ta:ClearAllPoints();
                icon3ta:SetPoint("TOPRIGHT",icon3,"TOPRIGHT",4,0);
                icon4t:ClearAllPoints();
                icon4t:SetPoint("BOTTOMLEFT",icon4,"BOTTOMLEFT",-2,0); 
                icon4ta:ClearAllPoints();
                icon4ta:SetPoint("TOPRIGHT",icon4,"TOPRIGHT",4,0);
                icon5t:ClearAllPoints();
                icon5t:SetPoint("BOTTOMLEFT",icon5,"BOTTOMLEFT",-2,0); 
                icon5ta:ClearAllPoints();
                icon5ta:SetPoint("TOPRIGHT",icon5,"TOPRIGHT",4,0);
                icon6t:ClearAllPoints();
                icon6t:SetPoint("BOTTOMLEFT",icon6,"BOTTOMLEFT",-2,0); 
                icon6ta:ClearAllPoints();
                icon6ta:SetPoint("TOPRIGHT",icon6,"TOPRIGHT",4,0);
                icon7t:ClearAllPoints();
                icon7t:SetPoint("BOTTOMLEFT",icon7,"BOTTOMLEFT",-2,0); 
                icon7ta:ClearAllPoints();
                icon7ta:SetPoint("TOPRIGHT",icon7,"TOPRIGHT",4,0);
            end
        end

        if Healbot_Config_Skins.bar2size[Healbot_Config_Skins.Current_Skin]==0 then
            bar3:SetHeight(1);
            bar3:SetValue(0);
            bar3:SetStatusBarColor(0,0,0,0)
        end
        if Healbot_Config_Skins.ShowAggroBars[Healbot_Config_Skins.Current_Skin]==0 then
            bar4:SetMinMaxValues(0,100)
            bar4:SetValue(0)
            bar4:SetStatusBarColor(0,0,0,0)
        end
        barScale = bar:GetScale();
        bar:SetScale(barScale + 0.01);
        bar:SetScale(barScale);
        barScale = bar2:GetScale();
        bar2:SetScale(barScale + 0.01);
        bar2:SetScale(barScale);
        barScale = bar3:GetScale();
        bar3:SetScale(barScale + 0.01);
        bar3:SetScale(barScale);
        barScale = bar4:GetScale();
        bar4:SetScale(barScale + 0.01);
        bar4:SetScale(barScale);
    end
    for x=1,15 do
        h=getglobal("HealBot_Action_Header"..x);
        bar = HealBot_Action_HealthBar(h);
        hwidth = bwidth*Healbot_Config_Skins.headwidth[Healbot_Config_Skins.Current_Skin]
        hheight = ceil(bheight*0.8)
        HealBot_Panel_SetHeadArrays(h)
        h:SetHeight(hheight);
        h:SetWidth(hwidth);
        bar:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.headtexture[Healbot_Config_Skins.Current_Skin]));
        bar:SetMinMaxValues(0,100);
        bar:SetValue(100);
        bar:SetStatusBarColor(br,bg,bb,ba);
        bar:SetHeight(hheight);
        bar.txt = getglobal(bar:GetName().."_text");
        bar.txt:SetFont(LSM:Fetch('font',Healbot_Config_Skins.headtextfont[Healbot_Config_Skins.Current_Skin]),Healbot_Config_Skins.headtextheight[Healbot_Config_Skins.Current_Skin],"");
        bar.txt:SetTextColor(sr,sg,sb,sa);
        h:Disable();
        bar:SetScale(barScale + 0.01);
        bar:SetScale(barScale);
        barScale = bar2:GetScale();
    end
    bar = HealBot_Action_HealthBar(HealBot_Action_OptionsButton);
    bar:SetStatusBarColor(0.1,0.1,0.4,0);
    bar.txt = getglobal(bar:GetName().."_text");
    bar.txt:SetTextColor(0.8,0.8,0.2,0.85);
    bar.txt:SetText(HEALBOT_ACTION_OPTIONS);
    HealBot_Action_SetAddHeight()

    HealBot_DiseaseColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_MagicColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_PoisonColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_CurseColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_CustomColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_EnTextColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_EnTextColorpickin:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_DisTextColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_DebTextColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_HeadBarColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.headtexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_HeadTextColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.headtexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_Buff1Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_Buff2Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_Buff3Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_Buff4Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_Buff5Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_Buff6Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_Buff7Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_Buff8Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_Buff9Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_Buff10Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_BarCustomColour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[Healbot_Config_Skins.Current_Skin]));
    HealBot_SetSkinColours()
    HealBot_CheckFrame()

    if Delay_RecalcParty<2 then 
        Delay_RecalcParty=2; 
    end
end

function HealBot_Action_RefreshButtons(hbGUID)
    if hbGUID then
        HealBot_Action_RefreshButton(HealBot_Unit_Button[HealBot_UnitID[hbGUID]], hbGUID)
    else
        if HealBot_ButtonArray==1 then
            for xUnit,xButton in pairs(HealBot_ButtonArray1) do
                HealBot_Action_CheckRange(xUnit, xButton)
            end
            HealBot_ButtonArray=2
        else
            for xUnit,xButton in pairs(HealBot_ButtonArray2) do
                HealBot_Action_CheckRange(xUnit, xButton)
            end
            HealBot_ButtonArray=1
        end
    end
end

local uRange=0
local db1=nil
local db2=nil
function HealBot_Action_CheckRange(unit, button)
    if not HealBot_UnitStatus[unit] then return end
    if HealBot_UnitStatus[unit]>0 then
        uRange=HealBot_UnitInRange(HealBot_UnitRangeSpell[unit] or HealBot_hSpell, unit)
        if unit~="player" then
            if HealBot_UnitStatus[unit]==8 then
                if UnitHealth(unit)>1 then 
                    HealBot_OnEvent_UnitHealth(nil,unit,UnitHealth(unit),UnitHealthMax(unit)) 
                end
            end
        end
        if HealBot_UnitRange[unit]==-2 then
       --     HealBot_AddDebug("HealBot_UnitRange[unit]==-2 unit="..unit)
            HealBot_Action_RefreshButton(button, HealBot_UnitGUID(unit))
        elseif uRange~=HealBot_UnitRange[unit] then
            ebubar = HealBot_Unit_Bar1[unit]
            ebubar.txt=getglobal(ebubar:GetName().."_text");
            ebuicon8 = getglobal(ebubar:GetName().."Icon8");
            HealBot_UnitRange[unit]=uRange
            if uRange==1 and not HealBot_PlayerDead then
                ebubar:SetStatusBarColor(HealBot_UnitRanger[unit],HealBot_UnitRangeg[unit],HealBot_UnitRangeb[unit],Healbot_Config_Skins.Barcola[Healbot_Config_Skins.Current_Skin])
                ebubar.txt:SetTextColor(HealBot_UnitRangeitr[unit],HealBot_UnitRangeitg[unit],HealBot_UnitRangeitb[unit],Healbot_Config_Skins.btextenabledcola[Healbot_Config_Skins.Current_Skin]);
                if Healbot_Config_Skins.ShowClassOnBar[Healbot_Config_Skins.Current_Skin]==1 and Healbot_Config_Skins.ShowClassType[Healbot_Config_Skins.Current_Skin]==1 then
                    ebuicon8:SetAlpha(Healbot_Config_Skins.Barcola[Healbot_Config_Skins.Current_Skin]);
                end
            elseif uRange==0 or HealBot_Config.NotVisibleDisable==0 then
                --ebubar:SetStatusBarColor(HealBot_UnitRanger[unit],HealBot_UnitRangeg[unit],HealBot_UnitRangeb[unit],Healbot_Config_Skins.bardisa[Healbot_Config_Skins.Current_Skin])
                ebubar:SetStatusBarColor(HealBot_UnitRanger[unit],HealBot_UnitRangeg[unit],HealBot_UnitRangeb[unit],HealBot_UnitRangea[unit])
                ebubar.txt:SetTextColor(HealBot_UnitRangeotr[unit],HealBot_UnitRangeotg[unit],HealBot_UnitRangeotb[unit],HealBot_UnitRangeota[unit]);
                if Healbot_Config_Skins.ShowClassOnBar[Healbot_Config_Skins.Current_Skin]==1 and Healbot_Config_Skins.ShowClassType[Healbot_Config_Skins.Current_Skin]==1 then
                    ebuicon8:SetAlpha(HealBot_UnitRangea[unit]);
                end
            else
                ebubar:SetStatusBarColor(HealBot_UnitRanger[unit],HealBot_UnitRangeg[unit],HealBot_UnitRangeb[unit],Healbot_Config_Skins.bardisa[Healbot_Config_Skins.Current_Skin])
                ebubar.txt:SetTextColor(HealBot_UnitRangeotr[unit],HealBot_UnitRangeotg[unit],HealBot_UnitRangeotb[unit],Healbot_Config_Skins.btextdisbledcola[Healbot_Config_Skins.Current_Skin]);
                if Healbot_Config_Skins.ShowClassOnBar[Healbot_Config_Skins.Current_Skin]==1 and Healbot_Config_Skins.ShowClassType[Healbot_Config_Skins.Current_Skin]==1 then
                    ebuicon8:SetAlpha(Healbot_Config_Skins.bardisa[Healbot_Config_Skins.Current_Skin]);
                end
            end
            if Healbot_Config_Skins.bar2size[Healbot_Config_Skins.Current_Skin]>0 then 
                HealBot_Action_SetBar3Value(HealBot_Unit_Button[unit]);
            end
        end
    end
end

function HealBot_Action_ResetUnitStatus(unit)
    if unit then
        HealBot_UnitStatus[unit]=1;
        HealBot_UnitRange[unit]=-2
    else
        for x,_ in pairs(HealBot_Unit_Button) do
            HealBot_UnitStatus[x]=1;
            HealBot_UnitRange[x]=-2
        end
    end
end

function HealBot_Action_ResetActiveUnitStatus()
    for x,_ in pairs(HealBot_Unit_Button) do
        if HealBot_UnitStatus[x]>0 or UnitHealth(x)<2 then
            HealBot_UnitRange[x]=-2
        end
    end
end

local shb=nil
local shbar=nil
function HealBot_Action_SetHealButton(index,unit,hbGUID)
    if UnitClass(unit) then
        if not HealBot_Unit_Button[unit] then
            if not HealBot_ButtonStore[1] then
                return nil
            else
                shb=HealBot_ButtonStore[1]
                table.remove(HealBot_ButtonStore,1)
                HealBot_Unit_Button[unit]="init"
            end
        else
            shb=HealBot_Unit_Button[unit]
        end
        if not HealBot_UnitSpec[hbGUID] then
            HealBot_UnitID[hbGUID]="init"
            HealBot_Unit_Button[unit]="init"
            HealBot_UnitSpec[hbGUID] = " "
        end
        if HealBot_UnitID[hbGUID]~=unit then
            HealBot_CheckAllBuffs(hbGUID)
            HealBot_CheckAllDebuffs(hbGUID)
            HealBot_UnitID[hbGUID]=unit
        end
        if HealBot_Unit_Button[unit]~=shb or not shb.index then
            shb.unit=unit
            shb.guid=hbGUID
            shb:SetAttribute("unit", unit);
            if not shb.index or HealBot_Config.FastAttrib==0 then 
                HealBot_Action_SetAllButtonAttribs(shb,"Enabled")
            else
                HealBot_Action_SetMacroButtonAttribs(shb)
            end
            shb.index=index
            HealBot_Unit_Button[unit]=shb
            HealBot_UnitStatus[unit]=9
            HealBot_Unit_Bar1[unit]=HealBot_Action_HealthBar(shb)
            HealBot_Unit_Bar2[unit]=HealBot_Action_HealthBar2(shb)
            HealBot_Unit_Bar3[unit]=HealBot_Action_HealthBar3(shb)
            HealBot_Unit_Bar4[unit]=HealBot_Action_HealthBar4(shb)
            shbar=HealBot_Unit_Bar1[unit]
            HealBot_UnitBarUpdate[shbar]=100
            HealBot_curUnitHealth[shbar]=100
            HealBot_UnitRangeSpell[unit]=HealBot_hSpell
            if not HealBot_ClassColR[hbGUID] then 
                HealBot_Action_ClassColour(hbGUID, unit) 
                HealBot_Enabled[hbGUID]=false
            end
        elseif hbGUID~=shb.guid then
            shb.guid=hbGUID
            HealBot_UnitStatus[unit]=9
        end
    else
        return nil
    end
    HealBot_TrackBars[shb]=nil
    return shb
end

function HealBot_Action_SetTargetHealButton(unit,hbGUID)
    if UnitClass(unit) then
        shb=getglobal("HealBot_Action_HealUnit51");
        if not HealBot_UnitSpec[hbGUID] then
            HealBot_Unit_Button[unit]="init"
            HealBot_UnitSpec[hbGUID] = " "
        end
        if HealBot_Unit_Button[unit]~=shb or not shb.index then
            shb.unit=unit
            shb.guid=hbGUID
            shb:SetAttribute("unit", unit);
            if HealBot_Config.TargetBarNormalMode==0 then      
                if not shb.index or HealBot_Config.FastAttrib==0 then 
                    HealBot_Action_SetAllButtonAttribs(shb,"Enabled")
                else
                    HealBot_Action_SetMacroButtonAttribs(shb)
                end
            elseif shb.index then
                HealBot_Action_SetAllButtonAttribs(shb,nil)
            end
            shb.index=51
            HealBot_Unit_Button[unit]=shb
            HealBot_UnitStatus[unit]=9
            HealBot_Unit_Bar1[unit]=HealBot_Action_HealthBar(shb)
            HealBot_Unit_Bar2[unit]=HealBot_Action_HealthBar2(shb)
            HealBot_Unit_Bar3[unit]=HealBot_Action_HealthBar3(shb)
            HealBot_Unit_Bar4[unit]=HealBot_Action_HealthBar4(shb)
            shbar=HealBot_Unit_Bar1[unit]
            HealBot_UnitBarUpdate[shbar]=100
            HealBot_curUnitHealth[shbar]=100
            HealBot_UnitRangeSpell[unit]=HealBot_hSpell
        elseif hbGUID~=shb.guid then
            shb.guid=hbGUID
            HealBot_UnitStatus[unit]=9
        end
        if not HealBot_UnitID[hbGUID] then
            HealBot_UnitID[hbGUID]=unit
            HealBot_CheckAllBuffs(hbGUID)
            HealBot_CheckAllDebuffs(hbGUID)
        end
    else
        return nil
    end
    HealBot_TrackBars[shb]=nil
    return shb
end

local HealBotButtonMacroAttribs={}
function HealBot_Action_SetAllAttribs()
 --   HealBot_AddDebug("In HealBot_Action_SetAllAttribs")
    HealBot_ResetAttribs=true
end

function HealBot_Action_SetAllBars()
    HB_Action_Timer1 = 2000
    HealBot_Action_ResethbInitButtons()
    for x=1,51 do
        b=getglobal("HealBot_Action_HealUnit"..x)
        HealBot_Panel_SetBarArrays(b)
        b:Hide()
    end
    for x,_ in pairs(HealBot_Unit_Button) do
        HealBot_Unit_Button[x]=nil
    end 
end

local HB_button,HB_prefix=nil
local HB_combo_prefix=nil
local HealBot_Keys_List = {"","Shift","Ctrl","Alt"}
function HealBot_Action_SetAllButtonAttribs(button,status)
    for x=1,5 do
        if x==1 then 
            HB_button="Left";
        elseif x==2 then 
            HB_button="Right";
        elseif x==3 then 
            HB_button="Middle";
        elseif x==4 then 
            HB_button="Button4";
        elseif x==5 then 
            HB_button="Button5";
        end
    
        for y=1, getn(HealBot_Keys_List), 1 do
            HealBot_Action_SetButtonAttrib(button,HB_button,HealBot_Keys_List[y],status,x)
        end
    end
end

--local mUnit=nil
local mText=nil
local mId=nil
local mName=nil
local mPrefix=nil
function HealBot_Action_SetMacroButtonAttribs(button)
    for mPrefix,mName in pairs(HealBotButtonMacroAttribs) do
        HealBot_Action_SetMacroButtonAttrib(button,mPrefix,mName)
    end
end

function HealBot_Action_SetMacroButtonAttrib(button,mPrefix,mName)
    if strlen(mPrefix)>1 then
        HB_prefix=strsub(mPrefix,1,strlen(mPrefix)-1)
        j=strsub(mPrefix,strlen(mPrefix),1)
    else
        HB_prefix=""
        j=mPrefix
    end
    mId=GetMacroIndexByName(mName)
    if mId ~= 0 then
        _,_,mText=GetMacroInfo(mId)
--       mUnit = button.unit
--       HealBot_AddDebug("Set Macro "..mName.." on unit "..button.unit.." mPrefix="..mPrefix)
        if HealBot_UnitPet(button.unit) then
            mText=string.gsub(mText,"hbtargetpet",HealBot_UnitPet(button.unit))
        end
        mText=string.gsub(mText,"hbtarget",button.unit)
        button:SetAttribute(HB_prefix.."helpbutton"..j, nil);
        button:SetAttribute(HB_prefix.."type"..j,"macro")
        button:SetAttribute(HB_prefix.."macrotext"..j, mText)
    else
        HealBot_Action_SetAllButtonAttribs(button,"Enabled")
    end
end

local showmenu=nil
local setDropdown=nil
local partyNo=nil
local unitPrefPartyID=nil
function HealBot_Action_SetButtonAttrib(button,bbutton,bkey,status,j)
 
    if strlen(bkey)>1 then
        HB_prefix = strlower(bkey).."-"
    else
        HB_prefix = "";
    end
    if HealBot_Config.UniqueSpellComboToSpec==1 then
        HB_combo_prefix = bkey..bbutton..HealBot_Config.CurrentSpec;
    else
        HB_combo_prefix = bkey..bbutton;
    end
    if status=="Enabled" then
        sName = HealBot_Action_AttribSpellPattern(HB_combo_prefix)
    elseif status=="Disabled" then
        sName = HealBot_Action_AttribDisSpellPattern(HB_combo_prefix)
    else
        sName=nil;
    end
    if sName then
        if strlower(sName)==strlower(HEALBOT_DISABLED_TARGET) then
            button:SetAttribute(HB_prefix.."helpbutton"..j, "target"..j);
            button:SetAttribute(HB_prefix.."type"..j, "target")
            button:SetAttribute(HB_prefix.."type-target"..j, "target")
        elseif strlower(sName)==strlower(HEALBOT_FOCUS) then
            button:SetAttribute(HB_prefix.."helpbutton"..j, "focus"..j);
            button:SetAttribute(HB_prefix.."type"..j, "focus")
            button:SetAttribute(HB_prefix.."type-focus"..j, "focus")
        elseif strlower(sName)==strlower(HEALBOT_ASSIST) then
            button:SetAttribute(HB_prefix.."helpbutton"..j, "assist"..j);
            button:SetAttribute(HB_prefix.."type"..j, "assist")
            button:SetAttribute(HB_prefix.."type-assist"..j, "assist")
        elseif strlower(sName)==strlower(HEALBOT_STOP) then
            button:SetAttribute(HB_prefix.."helpbutton"..j, nil);
            button:SetAttribute(HB_prefix.."type"..j, "macro")
            button:SetAttribute(HB_prefix.."macrotext"..j, "/stopcasting")
        elseif strsub(strlower(sName),1,4)==strlower(HEALBOT_TELL) then
            mText='/script local n=UnitName("hbtarget");SendChatMessage("hbMSG","WHISPER",nil,n)'
            mText=string.gsub(mText,"hbtarget",button.unit)
            mText=string.gsub(mText,"hbMSG", strtrim(strsub(sName,5)))
            button:SetAttribute(HB_prefix.."helpbutton"..j, nil);
            button:SetAttribute(HB_prefix.."type"..j, "macro")
            button:SetAttribute(HB_prefix.."macrotext"..j, mText)
        elseif strlower(sName)==strlower(HEALBOT_MENU) then
            button:SetAttribute(HB_prefix.."helpbutton"..j, nil);
            button:SetAttribute(HB_prefix.."type"..j, "showmenu")
            showmenu = function()
                if button.unit=="player" then
                    setDropdown=PlayerFrameDropDown
                elseif button.unit=="target" then
                    setDropdown=TargetFrameDropDown
                elseif button.unit=="pet" then
                    setDropdown=PetFrameDropDown
                else
                    unitPrefPartyID=HealBot_Action_UnitID(button.unit)
                    partyNo = tonumber(unitPrefPartyID:match('party(%d+)')) or 0
                    if partyNo > 0 then
                        setDropdown = _G['PartyMemberFrame' .. partyNo .. 'DropDown']
                    else
                        partyNo = tonumber(unitPrefPartyID:match('raid(%d+)')) or 0
                        if partyNo == 0 then
                            partyNo=button.index
                        end
                        FriendsDropDown.name = UnitName(unitPrefPartyID);    
                        FriendsDropDown.id = partyNo;    
                        FriendsDropDown.unit = unitPrefPartyID;    
                        FriendsDropDown.initialize = RaidFrameDropDown_Initialize;    
                        FriendsDropDown.displayMode = "MENU";    
                        setDropdown=FriendsDropDown
                    end
                end
                ToggleDropDownMenu(1, nil, setDropdown, "cursor", 0, 0)  
            end
            button.showmenu = showmenu 
        elseif strlower(sName)==strlower(HEALBOT_MAINTANK) then
            button:SetAttribute(HB_prefix.."helpbutton"..j, nil);
            button:SetAttribute(HB_prefix.."type"..j, "maintank")
            button:SetAttribute(HB_prefix.."type-maintank"..j, "toggle")
        elseif strlower(sName)==strlower(HEALBOT_MAINASSIST) then
            button:SetAttribute(HB_prefix.."helpbutton"..j, nil);
            button:SetAttribute(HB_prefix.."type"..j, "mainassist")
            button:SetAttribute(HB_prefix.."type-mainassist"..j, "toggle")
        else
            sID=HealBot_GetSpellId(sName)
            if sID then
                button:SetAttribute(HB_prefix.."helpbutton"..j, "heal"..j);
                button:SetAttribute(HB_prefix.."type-heal"..j, "spell");
                button:SetAttribute(HB_prefix.."spell-heal"..j, sName);
            else
                mId=GetMacroIndexByName(sName)
                if mId ~= 0 then
                    _,_,mText=GetMacroInfo(mId)
         --        mUnit = button.unit
                    if HealBot_UnitPet(button.unit) then
                        mText=string.gsub(mText,"hbtargetpet",HealBot_UnitPet(button.unit))
                    end
                    mText=string.gsub(mText,"hbtarget",button.unit)
                    button:SetAttribute(HB_prefix.."helpbutton"..j, nil);
                    button:SetAttribute(HB_prefix.."type"..j,"macro")
                    button:SetAttribute(HB_prefix.."macrotext"..j, mText)
                    if status=="Enabled" then
                        HealBotButtonMacroAttribs[HB_prefix..j]=sName
                    end
                else
                    button:SetAttribute(HB_prefix.."helpbutton"..j, "item"..j);
                    button:SetAttribute(HB_prefix.."type-item"..j, "item");
                    button:SetAttribute(HB_prefix.."item-item"..j, sName);
                end
            end
        end
    else
        button:SetAttribute(HB_prefix.."helpbutton"..j, nil);
    end
end

function HealBot_Action_UnitID(unit)
    if strsub(unit,1,4)=="raid" then
        if UnitIsUnit(unit,"party1") then
            unit="party1"
        elseif UnitIsUnit(unit,"party2") then
            unit="party2"
        elseif UnitIsUnit(unit,"party3") then
            unit="party3"
        elseif UnitIsUnit(unit,"party4") then
            unit="party4"
        end
    end
    return unit
end

local combos=nil
function HealBot_Action_AttribSpellPattern(HB_combo_prefix)
    combos = HealBot_Config.EnabledKeyCombo
    if not combos then 
        return nil 
    end
    return combos[HB_combo_prefix]
end

function HealBot_Action_AttribDisSpellPattern(HB_combo_prefix)
    combos = HealBot_Config.DisabledKeyCombo
    if not combos then 
        return nil 
    end
    return combos[HB_combo_prefix]
end

local hbInitButtons=false
function HealBot_Action_ResethbInitButtons()
    hbInitButtons=false
    for x,_ in pairs(HealBot_ButtonStore) do
        HealBot_ButtonStore[x]=nil;
    end
end

local uCnt=0
function HealBot_Action_PartyChanged(HealBot_PreCombat)
    if HealBot_Config.DisableHealBot==1 then return end
    if HealBot_IsFighting and not InCombatLockdown() then 
        HealBot_PreCombat=true
        HealBot_IsFighting=nil
    end
    if not HealBot_IsFighting and HealBot_PlayerGUID then
        if HealBot_FrameMoving and HealBot_PreCombat then
            HealBot_Action_OnDragStop(self)
        elseif HealBot_FrameMoving then
            Delay_RecalcParty=1
            return
        end
  
        if not HealBot_PreCombat and HealBot_ResetAttribs then
            for j=1,51 do
                b=getglobal("HealBot_Action_HealUnit"..j)
                b.index=nil
            end
            for x,_ in pairs(HealBot_UnitID) do
                HealBot_UnitID[x]=nil;
            end
            for x,_ in pairs(HealBotButtonMacroAttribs) do
                HealBotButtonMacroAttribs[x]=nil;
            end
            HealBot_ResetAttribs=nil
        end

        for unit,_ in pairs(HealBot_ResetBars) do
            if HealBot_Unit_Button[unit] then 
                HealBot_Unit_Button[unit]=nil 
            end
            HealBot_ResetBars[unit]=nil
        end 
    
        for unit,button in pairs(HealBot_Unit_Button) do
            HealBot_TrackBars[button]=unit
        end 
    
        if not hbInitButtons then
            for x=1,50 do
                b=getglobal("HealBot_Action_HealUnit"..x);
                HealBot_Action_InsButtonStore(b)
        --        table.insert(HealBot_ButtonStore,b);
            end
            hbInitButtons=true
        end
        if HealBot_PreCombat then 
            HealBot_EnteringCombat()
        end
  
        if Healbot_Config_Skins.ShowHeader[Healbot_Config_Skins.Current_Skin]==1 then
            HealBot_Panel_PartyChanged(true)
        else
            HealBot_Panel_PartyChanged(false)
        end
        uCnt=0
        HealBot_ButtonArray=1
        for x,_ in pairs(HealBot_ButtonArray1) do
            HealBot_ButtonArray1[x]=nil;
        end
        for x,_ in pairs(HealBot_ButtonArray2) do
            HealBot_ButtonArray2[x]=nil;
        end
    
        for unit,button in pairs(HealBot_Unit_Button) do
            uCnt=uCnt+1
            b=HealBot_Unit_Button[unit]
            if Healbot_Config_Skins.bar2size[Healbot_Config_Skins.Current_Skin]>0 then 
                HealBot_Action_RefreshBar3(unit) 
            end
            if HealBot_ButtonArray==1 then
                HealBot_ButtonArray1[unit]=HealBot_Unit_Button[unit]
                HealBot_ButtonArray=2
            else
                HealBot_ButtonArray2[unit]=HealBot_Unit_Button[unit]
                HealBot_ButtonArray=1
            end
            if HealBot_UnitStatus[unit]==9 then
                HealBot_Action_ResetUnitStatus(unit)
            end
            if uCnt==1 then
                HealBot_SmallArray=unit
            end 
        end
        if uCnt==1 then
            HealBot_ButtonArray2[HealBot_SmallArray]=HealBot_Unit_Button[unit]
        end

        for button,unit in pairs(HealBot_TrackBars) do
            if not HealBot_Unit_Button[unit] or HealBot_Unit_Button[unit]==button then
                if UnitExists(unit) then
                    if Delay_RecalcParty<3 then 
                        Delay_RecalcParty=3 
                    end
                    HealBot_ResetBars[unit]=true
--                      HealBot_AddDebug("removed bar - set Delay_RecalcParty=2 : unit="..unit)
                else
                    HealBot_Unit_Button[unit]=nil
                end
            end
            HealBot_Action_InsButtonStore(button)
--              HealBot_AddDebug("removed bar : unit="..unit)
            HealBot_TrackBars[button]=nil
        end

    else
        Delay_RecalcParty=3
    end
end

function HealBot_Action_InsButtonStore(b)
    table.insert(HealBot_ButtonStore,b)
    HealBot_HoT_RemoveIconButton(b)
    if b.index then 
        b.index=nil 
    end
    HealBot_Panel_SetBarArrays(b)
end

function HealBot_Action_Reset()
    HealBot_Config.AutoClose=0
    HealBot_Config.PanelAnchorY=-1
    HealBot_Config.PanelAnchorX=-1
    HealBot_Action:ClearAllPoints();
    HealBot_Action:SetPoint("CENTER","UIParent","CENTER",0,0);
    HealBot_Action_PartyChanged()
    if HealBot_Config.DisableHealBot==0 then
        HealBot_Action:Show()
    else
        HealBot_Action:Hide()
    end
end

function HealBot_Action_ClassColour(hbGUID, unit)
    if HealBot_ClassColR[hbGUID] then
        return HealBot_ClassColR[hbGUID],HealBot_ClassColG[hbGUID],HealBot_ClassColB[hbGUID]
    elseif unit and hbGUID and HealBot_UnitGUID(unit)~=hbGUID then
        return 0.78,0.61,0.43
    elseif not hbGUID then
        return 0.78,0.61,0.43
    end
    class=nil
    if HealBot_UnitID[hbGUID] then
        _,class=UnitClass(HealBot_UnitID[hbGUID])
    elseif unit and UnitClass(unit) then
        _,class=UnitClass(unit)
    end
    if class ~= nil then
        if strsub(class,1,4)=="DRUI" then
            HealBot_ClassColR[hbGUID],HealBot_ClassColG[hbGUID],HealBot_ClassColB[hbGUID] = 1.0,0.49,0.04
        elseif strsub(class,1,4)=="HUNT" then
            HealBot_ClassColR[hbGUID],HealBot_ClassColG[hbGUID],HealBot_ClassColB[hbGUID] = 0.67,0.83,0.45
        elseif strsub(class,1,4)=="MAGE" then
            HealBot_ClassColR[hbGUID],HealBot_ClassColG[hbGUID],HealBot_ClassColB[hbGUID] = 0.41,0.8,0.94
        elseif strsub(class,1,4)=="PALA" then
            HealBot_ClassColR[hbGUID],HealBot_ClassColG[hbGUID],HealBot_ClassColB[hbGUID] = 0.96,0.55,0.73
        elseif strsub(class,1,4)=="PRIE" then
            HealBot_ClassColR[hbGUID],HealBot_ClassColG[hbGUID],HealBot_ClassColB[hbGUID] = 1.0,1.0,1.0
        elseif strsub(class,1,4)=="ROGU" then
            HealBot_ClassColR[hbGUID],HealBot_ClassColG[hbGUID],HealBot_ClassColB[hbGUID] = 1.0,0.96,0.41
        elseif strsub(class,1,4)=="SHAM" then
            HealBot_ClassColR[hbGUID],HealBot_ClassColG[hbGUID],HealBot_ClassColB[hbGUID] = 0.14,0.35,1.0
        elseif strsub(class,1,4)=="WARL" then
            HealBot_ClassColR[hbGUID],HealBot_ClassColG[hbGUID],HealBot_ClassColB[hbGUID] = 0.58,0.51,0.79
        elseif strsub(class,1,4)=="DEAT" then
            HealBot_ClassColR[hbGUID],HealBot_ClassColG[hbGUID],HealBot_ClassColB[hbGUID] = 0.78,0.04,0.04
        else
            HealBot_ClassColR[hbGUID],HealBot_ClassColG[hbGUID],HealBot_ClassColB[hbGUID] = 0.78,0.61,0.43
        end
    else
        HealBot_ClassColR[hbGUID],HealBot_ClassColG[hbGUID],HealBot_ClassColB[hbGUID] = 0.78,0.61,0.43
    end
    return HealBot_ClassColR[hbGUID],HealBot_ClassColG[hbGUID],HealBot_ClassColB[hbGUID]
end

function HealBot_Action_ShowTooltip(unit)
    if HealBot_Config.ShowTooltip==0 then return end
    HealBot_Action_TooltipUnit = unit;
    HealBot_Action_DisableTooltipUnit = nil;
    HealBot_Action_RefreshTooltip(unit,"Enabled");
end

function HealBot_Action_ShowDisabledTooltip(unit)
    if HealBot_Config.ShowTooltip==0 then return end
    HealBot_Action_TooltipUnit = nil;
    HealBot_Action_DisableTooltipUnit = unit;
    HealBot_Action_RefreshTooltip(unit,"Disabled");
end

function HealBot_Action_HideTooltip(self)
    HealBot_Action_TooltipUnit = nil;
    HealBot_Action_DisableTooltipUnit = nil;
    HealBot_Action_HideTooltipFrame()
end

function HealBot_Action_HideTooltipFrame()
    HealBot_Tooltip:Hide();
end

function HealBot_Action_Refresh(hbGUID)
    if HealBot_Config.DisableHealBot==1 then return end
    if HealBot_PlayerDead then
        if HealBot_Config.AutoClose==1 and HealBot_Config.ActionVisible~=0 and not HealBot_IsFighting then
            HideUIPanel(HealBot_Action); 
        else
            HealBot_Action_RefreshButtons(hbGUID);
        end
        return;
    end
    HealBot_Action_RefreshButtons(hbGUID);
    if not HealBot_IsFighting then
        if HealBot_Config.ActionVisible==0 then
            if HealBot_Action_ShouldHealSome(hbGUID) then
                ShowUIPanel(HealBot_Action)
            end
        elseif HealBot_Config.AutoClose==1 then 
            if not HealBot_Action_ShouldHealSome(hbGUID) then
                HideUIPanel(HealBot_Action);
            end
        end
    end
end

function HealBot_Action_SpellPattern(button,state)
    if state and state=="Disabled" then
        combos = HealBot_Config.DisabledKeyCombo
    else
        combos = HealBot_Config.EnabledKeyCombo
    end
    if not combos then return nil end
    x = button;
    if IsAltKeyDown() then x = "Alt"..x end
    if IsControlKeyDown() then x = "Ctrl"..x end
    if IsShiftKeyDown() then x = "Shift"..x end
    if HealBot_Config.UniqueSpellComboToSpec==1 then
        x=x..HealBot_Config.CurrentSpec
    end
    return combos[x]
end

function HealBot_Action_HealUnit_OnEnter(self)
    if not self.unit then return; end
    if HealBot_IsFighting then
        if HealBot_Config.DisableToolTipInCombat==0 then
            HealBot_Action_ShowTooltip(self.unit);
        else
            HealBot_Action_TooltipUnit = self.unit
        end
    elseif HealBot_Config.UseTargetCombatStatus==1 and UnitAffectingCombat(self.unit)==1 then
        HealBot_Action_ShowTooltip(self.unit);
    elseif HealBot_Enabled[HealBot_UnitGUID(self.unit)] or HealBot_Config.EnableHealthy==1 then 
        HealBot_Action_ShowTooltip(self.unit);
    else
        if not HealBot_PlayerDead then
            HealBot_Action_ShowDisabledTooltip(self.unit);
        end
    end
end

function HealBot_Action_HealUnit_OnLeave(self)
    HealBot_Action_HideTooltip(self);
end


function HealBot_Action_OptionsButton_OnClick(self)
    HealBot_TogglePanel(HealBot_Options);
end

function HealBot_Action_OnLoad(self)
--  Nothing to do here
end

local HealBot_Action_Timer1,HealBot_Action_Timer2 = -4,-4

function HealBot_Action_Set_Timers(override)
    if HealBot_Config.DisableHealBot==0 and Healbot_Config_Skins.UseFluidBars[Healbot_Config_Skins.Current_Skin]==1 then
        if not override then
            HB_Action_Timer1 = 0.05
        else
            HB_Action_Timer1 = 0.5
        end
    else
        HB_Action_Timer1 = 2
    end
    if HealBot_Config.DisableHealBot==0 and Healbot_Config_Skins.ShowAggroBars[Healbot_Config_Skins.Current_Skin]==1 and Healbot_Config_Skins.ShowAggro[Healbot_Config_Skins.Current_Skin]==1 then
        if not override then
            HB_Action_Timer2 = 0.05
        else
            HB_Action_Timer2 = 0.5
        end
    else
        HB_Action_Timer2 = 2
    end
end

local HealBot_AggroBarColr = {[0]=1, [1]=0.8, [2]=1, [3]=1, [5]=1, [6]=1, [7]=1, [8]=1, [9]=1}
local HealBot_AggroBarColg = {[0]=0.0, [1]=0.8, [2]=0, [3]=0, [5]=1, [6]=1, [7]=1, [8]=1, [9]=1}
local HealBot_AggroBarColb = {[0]=0.0, [1]=0.0, [2]=0, [3]=0, [5]=1, [6]=1, [7]=1, [8]=1, [9]=1}
local HealBot_AggroUnitThreat=1
local HealBot_HasAggro=false

function HealBot_Action_SetDebuffAggroCols()
    HealBot_AggroBarColr[5]=HealBot_Config.CDCBarColour[HEALBOT_DISEASE_en].R
    HealBot_AggroBarColg[5]=HealBot_Config.CDCBarColour[HEALBOT_DISEASE_en].G
    HealBot_AggroBarColb[5]=HealBot_Config.CDCBarColour[HEALBOT_DISEASE_en].B 
    HealBot_AggroBarColr[6]=HealBot_Config.CDCBarColour[HEALBOT_MAGIC_en].R
    HealBot_AggroBarColg[6]=HealBot_Config.CDCBarColour[HEALBOT_MAGIC_en].G
    HealBot_AggroBarColb[6]=HealBot_Config.CDCBarColour[HEALBOT_MAGIC_en].B 
    HealBot_AggroBarColr[7]=HealBot_Config.CDCBarColour[HEALBOT_POISON_en].R
    HealBot_AggroBarColg[7]=HealBot_Config.CDCBarColour[HEALBOT_POISON_en].G
    HealBot_AggroBarColb[7]=HealBot_Config.CDCBarColour[HEALBOT_POISON_en].B 
    HealBot_AggroBarColr[8]=HealBot_Config.CDCBarColour[HEALBOT_CURSE_en].R
    HealBot_AggroBarColg[8]=HealBot_Config.CDCBarColour[HEALBOT_CURSE_en].G
    HealBot_AggroBarColb[8]=HealBot_Config.CDCBarColour[HEALBOT_CURSE_en].B 
    HealBot_AggroBarColr[9]=HealBot_Config.CDCBarColour[HEALBOT_CUSTOM_en].R
    HealBot_AggroBarColg[9]=HealBot_Config.CDCBarColour[HEALBOT_CUSTOM_en].G
    HealBot_AggroBarColb[9]=HealBot_Config.CDCBarColour[HEALBOT_CUSTOM_en].B
end

function HealBot_Action_OnUpdate(self,arg1)
    HealBot_Action_Timer1 = HealBot_Action_Timer1+arg1
    HealBot_Action_Timer2 = HealBot_Action_Timer2+arg1
    if HealBot_Action_Timer1>HB_Action_Timer1 then
        HealBot_Action_Timer1 = 0
        for ebubar,value in pairs(HealBot_UnitBarUpdate) do 
            if value>HealBot_curUnitHealth[ebubar] then
                if value-Healbot_Config_Skins.BarFreq[Healbot_Config_Skins.Current_Skin]<=HealBot_curUnitHealth[ebubar] then
                    HealBot_UnitBarUpdate[ebubar]=HealBot_curUnitHealth[ebubar]
                else
                    HealBot_UnitBarUpdate[ebubar]=value-Healbot_Config_Skins.BarFreq[Healbot_Config_Skins.Current_Skin]
                end
            elseif value+Healbot_Config_Skins.BarFreq[Healbot_Config_Skins.Current_Skin]>=HealBot_curUnitHealth[ebubar] then
                HealBot_UnitBarUpdate[ebubar]=HealBot_curUnitHealth[ebubar]
            else
                HealBot_UnitBarUpdate[ebubar]=value+Healbot_Config_Skins.BarFreq[Healbot_Config_Skins.Current_Skin]
            end
            ebubar:SetValue(HealBot_UnitBarUpdate[ebubar])
            if HealBot_UnitBarUpdate[ebubar]==HealBot_curUnitHealth[ebubar] then
                HealBot_UnitBarUpdate[ebubar]=nil
            end
        end
    end
    if HealBot_Action_Timer2>HB_Action_Timer2 then
        HealBot_Action_Timer2 = 0
        for xGUID,bar4 in pairs(HealBot_AggroBar4) do
            HealBot_AggroUnitThreat=HealBot_UnitThreat[xGUID] or 2
            bar4:SetStatusBarColor(HealBot_AggroBarColr[HealBot_AggroUnitThreat],HealBot_AggroBarColg[HealBot_AggroUnitThreat],HealBot_AggroBarColb[HealBot_AggroUnitThreat],HealBot_AggroBarA)
        end
        if HealBot_AggroBarAup then
            if HealBot_AggroBarA>=1 then
                HealBot_AggroBarAup=false
                HealBot_AggroBarA=HealBot_AggroBarA-0.05
            else
                HealBot_AggroBarA=HealBot_AggroBarA+0.05
            end
        else
            if HealBot_AggroBarA<=0.45 then
                HealBot_AggroBarAup=true
                HealBot_AggroBarA=HealBot_AggroBarA+0.05
            else
                HealBot_AggroBarA=HealBot_AggroBarA-0.05
            end
        end
    end
end

function HealBot_Action_OnShow(self)
    if HealBot_Config.PanelSounds==1 then
        PlaySound("igAbilityOpen");
    end
    HealBot_Config.ActionVisible = 1
    HealBot_Action:SetBackdropColor(
    Healbot_Config_Skins.backcolr[Healbot_Config_Skins.Current_Skin],
    Healbot_Config_Skins.backcolg[Healbot_Config_Skins.Current_Skin],
    Healbot_Config_Skins.backcolb[Healbot_Config_Skins.Current_Skin], 
    Healbot_Config_Skins.backcola[Healbot_Config_Skins.Current_Skin]);
    HealBot_Action:SetBackdropBorderColor(
    Healbot_Config_Skins.borcolr[Healbot_Config_Skins.Current_Skin],
    Healbot_Config_Skins.borcolg[Healbot_Config_Skins.Current_Skin],
    Healbot_Config_Skins.borcolb[Healbot_Config_Skins.Current_Skin],
    Healbot_Config_Skins.borcola[Healbot_Config_Skins.Current_Skin]);
end

function HealBot_Action_OnHide(self)
    HealBot_StopMoving(self,true);
    HealBot_Config.ActionVisible = 0
end

function HealBot_Action_OnMouseDown(self,button)
    if button~="RightButton" then
        if HealBot_Config.ActionLocked==0 then
            HealBot_FrameMoving=true
            HealBot_StartMoving(self);
        end
    end
end

function HealBot_Action_OnMouseUp(self,button)
    if button~="RightButton" then
        HealBot_StopMoving(self,true);
        HealBot_FrameMoving=nil
    elseif not HealBot_IsFighting and HealBot_Config.RightButtonOptions~=0 then
        HealBot_Action_OptionsButton_OnClick();
    end
end

function HealBot_Action_OnDragStart(self,button)
    if HealBot_Config.ActionLocked==0 then
        HealBot_FrameMoving=true
        HealBot_StartMoving(self);
    end
end

function HealBot_Action_OnDragStop(self)
 --   HealBot_StopMoving(self,true);
    HealBot_FrameMoving=nil
end

local usedSmartCast=nil
local ModKey=nil
local abutton=nil
local aj=nil
function HealBot_Action_PreClick(self,button)
    HealBot_UpdTargetUnitID(self.unit)
    xGUID=HealBot_UnitGUID(self.unit)
    usedSmartCast=false;
    ModKey=""
    if IsControlKeyDown() then 
        ModKey="Ctrl"
    elseif IsAltKeyDown() then 
        ModKey="Alt"
    elseif IsShiftKeyDown() then 
        ModKey="Shift" 
    end
    if button=="LeftButton" then 
        abutton="Left"
        aj=1
    elseif button=="RightButton" then 
        abutton="Right"
        aj=2
    elseif button=="MiddleButton" then 
        abutton="Middle"
        aj=3
    elseif button=="Button4" then 
        abutton="Button4"
        aj=4
    elseif button=="Button5" then 
        abutton="Button5"
        aj=5
    end
    if self.unit=="target" and HealBot_Config.TargetBarNormalMode==1 then
        if button=="RightButton" then
            if HealBot_UnitID[xGUID] then
                HealBot_Panel_ToggelHealTarget(xGUID)
                if Delay_RecalcParty<3 then 
                    Delay_RecalcParty=3 
                end
                if HealBot_Config.ShowTooltip==1 then 
                    HealBot_Action_RefreshTargetTooltip(xGUID, self.unit) 
                end
            end
        elseif button=="LeftButton" and HealBot_Config.SmartCast==1 and not IsModifierKeyDown() then
            HealBot_Action_UseSmartCast(self,xGUID)
        end
    elseif IsControlKeyDown() and IsAltKeyDown() and (button=="LeftButton" or button=="MiddleButton" or button=="RightButton") then
        if button=="LeftButton" then
            if HealBot_MyTargets[xGUID] then
                HealBot_MyTargets[xGUID]=nil
            else
                HealBot_MyTargets[xGUID]=true
            end
            HealBot_Action_ResetUnitStatus(HealBot_UnitID[xGUID])
        elseif button=="RightButton" then
            HealBot_Panel_ToggelHealTarget(xGUID)
            if Delay_RecalcParty<3 then 
                Delay_RecalcParty=3 
            end
        elseif xGUID and xGUID~=HealBot_PlayerGUID and button=="MiddleButton" then
            HealBot_Panel_AddBlackList(xGUID)
            if Delay_RecalcParty<3 then 
                Delay_RecalcParty=3 
            end
        end
    elseif not HealBot_IsFighting then
        if HealBot_Config.UseTargetCombatStatus==1 then
            if UnitAffectingCombat(self.unit)==1 then 
                return
            end
        end
        if HealBot_Config.ProtectPvP==1 then
            if UnitIsPVP(self.unit) and not UnitIsPVP("player") then 
                HealBot_Action_SetButtonAttrib(self,abutton,ModKey,"nil",aj)
                usedSmartCast=true;
            end
        end
        if button=="LeftButton" and HealBot_Config.SmartCast==1 and not IsModifierKeyDown() then
            HealBot_Action_UseSmartCast(self,xGUID)
        end
        if not HealBot_Enabled[xGUID] and HealBot_Config.EnableHealthy==0 and not usedSmartCast then
            HealBot_Action_SetButtonAttrib(self,abutton,ModKey,"Disabled",aj)
            usedSmartCast=true;
        end
    end
end

function HealBot_Action_UseSmartCast(bp,hbGUID)
    sName=HealBot_Action_SmartCast(hbGUID);
    if sName then
        sID=HealBot_GetSpellId(sName)
        if sID then
            if HealBot_UnitInRange(sName, bp.unit)==1 or hbGUID==HealBot_PlayerGUID then
                bp:SetAttribute("helpbutton1", "heal1");
                bp:SetAttribute("type-heal1", "spell");
                bp:SetAttribute("spell-heal1", sName);
            end
        else
            mId=GetMacroIndexByName(sName)
            if mId ~= 0 then
                _,_,mText=GetMacroInfo(mId)
     --        mUnit = bp.unit
                mText=string.gsub(mText,"hbtargetpet",HealBot_UnitPet(button.unit))
                mText=string.gsub(mText,"hbtarget",bp.unit)
                bp:SetAttribute("type1","macro")
                bp:SetAttribute("macrotext1", mText)
            else
                bp:SetAttribute("helpbutton1", "item1");
                bp:SetAttribute("type-item1", "item");
                bp:SetAttribute("item-item1", sName);
            end
        end
        usedSmartCast=true;
        HealBot_Check_WeaponBuffs(sName)
    end
end

function HealBot_Action_PostClick(self,button)
    if usedSmartCast then
        if self.unit=="target" then
            if aj==1 then
                self:SetAttribute(HB_prefix.."helpbutton"..aj, "target"..aj);
                self:SetAttribute(HB_prefix.."type"..aj, "target")
                self:SetAttribute(HB_prefix.."type-target"..aj, "target")
            end
        else
            HealBot_Action_SetButtonAttrib(self,abutton,ModKey,"Enabled",aj)
        end
    end
end

function HealBot_Action_RetMyTarget(hbGUID)
    return HealBot_MyTargets[hbGUID]
end

local scuHlth, scuMaxHlth, scuHealsIn = nil,nil,nil
function HealBot_Action_SmartCast(hbGUID)
    sName=nil
    rangeSpell=HealBot_hSpell
    if HealBot_PlayerDead or not HealBot_UnitID[hbGUID] then return nil; end
  
    if HealBot_Config.SmartCastRes==1 and UnitIsDead(HealBot_UnitID[hbGUID]) and not UnitIsGhost(HealBot_UnitID[hbGUID]) then
        sName=HealBot_Init_retSmartCast_Res();
        rangeSpell=HealBot_rSpell
    elseif HealBot_UnitDebuff[hbGUID] and HealBot_Config.SmartCastDebuff==1 then
        sName=HealBot_DebuffSpell[HealBot_UnitDebuff[hbGUID]];
        rangeSpell=HealBot_dSpell
    elseif HealBot_UnitBuff[hbGUID] and HealBot_Config.SmartCastBuff==1 then
        sName=HealBot_UnitBuff[hbGUID];
        rangeSpell=HealBot_bSpell
    elseif HealBot_Config.SmartCastHeal==1 then
        scuHealsIn = HealBot_RetHealsIn(hbGUID);
        scuHlth, scuMaxHlth = HealBot_UnitHealth(hbGUID, HealBot_UnitID[hbGUID]);
        x = scuMaxHlth-(scuHlth+scuHealsIn);
        if x>200 then
            sName=HealBot_SmartCast(hbGUID,x)
        end
    end
    if sName and hbGUID~=HealBot_PlayerGUID then
        if HealBot_UnitInRange(rangeSpell, HealBot_UnitID[hbGUID])~=1 then return nil; end
    end
    return sName;
end

function HealBot_Action_RetHealBot_ClassCol(hbGUID)
    return HealBot_Action_ClassColour(hbGUID)
end

function HealBot_Action_LocalArrays(hbGUID)
    unitName=HealBot_UnitName[hbGUID]
    if HealBot_Unit_Button["target"] and unitName=="target" then
        HealBot_Unit_Button["target"]=nil
    end
    if HealBot_ClassColR[hbGUID] then
        HealBot_Enabled[hbGUID]=nil
        HealBot_ClassColR[hbGUID]=nil
        HealBot_ClassColG[hbGUID]=nil
        HealBot_ClassColB[hbGUID]=nil
        if HealBot_UnitOffline[hbGUID] then HealBot_UnitOffline[hbGUID]=nil end
        if HealBot_AggroBar4[hbGUID] then HealBot_AggroBar4[hbGUID]=nil end
        if HealBot_Aggro[hbGUID] then HealBot_Aggro[hbGUID]=nil end
        HealBot_ClearLocalArr(hbGUID)
    end
end

