local i=nil
local HB_mana=nil
local HB_cast=nil
local HB_HealsMin=nil
local HB_HealsMax=nil
local HB_HealsExt=nil
local HB_duration=nil
local HB_range=nil
local HB_shield=nil
local HB_channel=nil
local tmpText=nil
local line=nil
local tmpTest=nil
local tmpTest2=nil
local hbHealsMin=nil
local hbHealsMax=nil
local spell=nil
local spellrank=nil
local line1=nil
local line2=nil
local line3=nil
local SmartCast_Res=nil;
local HealBot_Spec = {}
local TempSkins = {}
local tonumber=tonumber
local strfind=strfind
local floor=floor
local strsub=strsub

function HealBot_Init_retSmartCast_Res()
    return SmartCast_Res
end

function HealBot_Init_SetSpec()
HealBot_Spec = {
    ["DRUI"] = { [1] = HEALBOT_BALANCE,       [2] = HEALBOT_FERAL,        [3] = HEALBOT_RESTORATION, },
    ["MAGE"] = { [1] = HEALBOT_ARCANE,        [2] = HEALBOT_FIRE,         [3] = HEALBOT_FROST,       },
    ["PRIE"] = { [1] = HEALBOT_DISCIPLINE,    [2] = HEALBOT_HOLY,         [3] = HEALBOT_SHADOW,      },
    ["ROGU"] = { [1] = HEALBOT_ASSASSINATION, [2] = HEALBOT_COMBAT,       [3] = HEALBOT_SUBTLETY,    },
    ["WARR"] = { [1] = HEALBOT_ARMS,          [2] = HEALBOT_FURY,         [3] = HEALBOT_PROTECTION,  },
    ["HUNT"] = { [1] = HEALBOT_BEASTMASTERY,  [2] = HEALBOT_MARKSMANSHIP, [3] = HEALBOT_SURVIVAL,    },
    ["PALA"] = { [1] = HEALBOT_HOLY,          [2] = HEALBOT_PROTECTION,   [3] = HEALBOT_RETRIBUTION, },
    ["SHAM"] = { [1] = HEALBOT_ELEMENTAL,     [2] = HEALBOT_ENHANCEMENT,  [3] = HEALBOT_SHAMAN_RESTORATION, },
    ["WARL"] = { [1] = HEALBOT_AFFLICTION,    [2] = HEALBOT_DEMONOLOGY,   [3] = HEALBOT_DESTRUCTION, },
    ["DEAT"] = { [1] = HEALBOT_BLOOD,         [2] = HEALBOT_FROST,        [3] = HEALBOT_UNHOLY, },
    }
end

function HealBot_Init_retSpec(class,tab)
    if HealBot_Spec[class] and HealBot_Spec[class][tab] then
        return HealBot_Spec[class][tab]
    end
    return nil
end

function HealBot_InitGetSpellData(spell, id, class, spellname)

    if ( not spell ) then return end
  
    HB_cast=nil
    HB_mana=nil
    HB_range=nil
    HB_HealsMin=nil
    HB_HealsMax=nil
    HB_HealsExt=nil
    HB_duration=nil
    HB_shield=nil
    HB_channel=nil
    
   -- name, rank, icon, cost, isFunnel, powerType, castTime, minRange, maxRange 
    _, _, _, HB_mana, _, _, HB_cast, _, HB_range = GetSpellInfo(spell)

    if HB_cast then HB_cast=HB_cast/1000 end
   
    HealBot_TooltipInit();
    HealBot_ScanTooltip:SetSpellBookItem( id, BOOKTYPE_SPELL );
    tmpText = _G["HealBot_ScanTooltipTextLeft2"];
    if (tmpText:GetText()) and not HB_mana then
        line = tmpText:GetText();
        tmpTest,tmpTest,HB_mana = strfind(line, HB_TOOLTIP_MANA ); 
    end

    tmpText = _G["HealBot_ScanTooltipTextRight2"];
    if (tmpText:GetText()) and not HB_range then
        line = tmpText:GetText();
        tmpTest,tmpTest,HB_range = strfind(line, HB_TOOLTIP_RANGE ); 
    else
        HB_range="30";
    end  

    tmpText = _G["HealBot_ScanTooltipTextLeft3"];
    if (tmpText:GetText()) then
        line = tmpText:GetText();
        if ( line == HB_TOOLTIP_INSTANT_CAST ) then
            HB_cast = 0;
        elseif line == HB_TOOLTIP_CHANNELED then
            HB_cast = 0;
        elseif ( tmpText ) then
            tmpTest,tmpTest,HB_cast = strfind(line, HB_TOOLTIP_CAST_TIME ); 
        end
    end  

    tmpText = _G["HealBot_ScanTooltipTextLeft4"];
    tmpTest = nil;
    if (tmpText:GetText()) then
        line = tmpText:GetText();
        if strsub(class,1,4)=="PRIE" then
            if spellname==HEALBOT_POWER_WORD_SHIELD then
                tmpTest,tmpTest,HB_HealsMin,HB_shield = strfind(line, HB_SPELL_PATTERN_SHIELD );    
                HB_HealsExt=0;
                HB_duration = 30;
                HB_HealsMax=HB_HealsMin;
            elseif spellname==HEALBOT_RENEW then
                tmpTest,tmpTest,HB_HealsExt,tmpTest,HB_duration = strfind(line, HB_SPELL_PATTERN_RENEW1 );  
                HB_HealsMin=0;
                HB_HealsMax=0;
                if ( HB_HealsExt == nil ) then
                    tmpTest,tmpTest,HB_HealsExt,HB_duration = strfind(line, HB_SPELL_PATTERN_RENEW );
                end
                if ( HB_HealsExt == nil ) then
                    tmpTest,tmpTest,HB_duration,HB_HealsExt = strfind(line, HB_SPELL_PATTERN_RENEW2 );
                end
                if ( HB_HealsExt == nil ) then
                    tmpTest,tmpTest,HB_duration,HB_HealsExt = strfind(line, HB_SPELL_PATTERN_RENEW3 );
                end
            elseif spellname==HEALBOT_LESSER_HEAL then
                tmpTest,HB_HealsMin,HB_HealsMax = HealBot_Generic_Patten(line,HB_SPELL_PATTERN_LESSER_HEAL); 
            elseif spellname==HEALBOT_GREATER_HEAL then
                tmpTest,HB_HealsMin,HB_HealsMax = HealBot_Generic_Patten(line, HB_SPELL_PATTERN_GREATER_HEAL ); 
            elseif spellname==HEALBOT_FLASH_HEAL then
                tmpTest,HB_HealsMin,HB_HealsMax = HealBot_Generic_Patten(line, HB_SPELL_PATTERN_FLASH_HEAL ); 
            elseif spellname==HEALBOT_HEAL then
                tmpTest,HB_HealsMin,HB_HealsMax = HealBot_Generic_Patten(line, HB_SPELL_PATTERN_HEAL ); 
            end
        elseif strsub(class,1,4)=="DRUI" then
            if spellname==HEALBOT_REGROWTH then
                tmpTest,tmpTest,HB_HealsMin,HB_HealsMax,HB_HealsExt = strfind(line, HB_SPELL_PATTERN_REGROWTH );
                if ( tmpTest == nil ) then
                    tmpTest,tmpTest,HB_HealsMin,HB_HealsMax,tmpTest,HB_HealsExt = strfind(line, HB_SPELL_PATTERN_REGROWTH1 );
                end
            elseif spellname==HEALBOT_REJUVENATION then
                tmpTest,tmpTest,HB_HealsExt,HB_duration = strfind(line, HB_SPELL_PATTERN_REJUVENATION );  
                HB_HealsMin=0;
                HB_HealsMax=0;
                if ( HB_HealsExt == nil ) then
                    tmpTest,tmpTest,HB_HealsExt,tmpTest,HB_duration = strfind(line, HB_SPELL_PATTERN_REJUVENATION1 );
                end
                if ( HB_HealsExt == nil ) then
                    tmpTest,tmpTest,HB_duration,HB_HealsExt = strfind(line, HB_SPELL_PATTERN_REJUVENATION2 );
                end
            elseif spellname==HEALBOT_HEALING_TOUCH then
                tmpTest,HB_HealsMin,HB_HealsMax = HealBot_Generic_Patten(line, HB_SPELL_PATTERN_HEALING_TOUCH ); 
            end
        elseif strsub(class,1,4)=="PALA" then
            if spellname==HEALBOT_HOLY_LIGHT then
                tmpTest,HB_HealsMin,HB_HealsMax = HealBot_Generic_Patten(line, HB_SPELL_PATTERN_HOLY_LIGHT ); 
            elseif spellname==HEALBOT_FLASH_OF_LIGHT then
                tmpTest,HB_HealsMin,HB_HealsMax = HealBot_Generic_Patten(line, HB_SPELL_PATTERN_FLASH_OF_LIGHT ); 
            end
        elseif strsub(class,1,4)=="SHAM" then
            if spellname==HEALBOT_HEALING_WAVE then
                tmpTest,HB_HealsMin,HB_HealsMax = HealBot_Generic_Patten(line, HB_SPELL_PATTERN_HEALING_WAVE ); 
            elseif spellname==HEALBOT_GREATER_HEALING_WAVE then
                tmpTest,HB_HealsMin,HB_HealsMax = HealBot_Generic_Patten(line, HB_SPELL_PATTERN_GREATER_HEALING_WAVE ); 
            end
        end
    end  


    if HB_cast then
        HealBot_Spells[spell].CastTime=tonumber(HB_cast);
    end
    if HB_mana then
        HealBot_Spells[spell].Mana=tonumber(HB_mana);
    end
    if HB_range then
        HealBot_Spells[spell].Range=tonumber(HB_range);
    end
    if HB_HealsMin then
        HealBot_Spells[spell].HealsMin=tonumber(HB_HealsMin);
    end
    if HB_HealsMax then
        HealBot_Spells[spell].HealsMax=tonumber(HB_HealsMax);
    end
    if HB_HealsExt then
        HealBot_Spells[spell].HealsExt=tonumber(HB_HealsExt);
    end
    if HB_duration and tonumber(HB_duration)<31 then
        HealBot_Spells[spell].Duration=tonumber(HB_duration);
    end
    if HB_shield then
        HealBot_Spells[spell].Shield=tonumber(HB_shield);
    end
    if HB_channel then
        HealBot_Spells[spell].Channel=tonumber(HB_channel);
    end
    HealBot_InitClearSpellNils(spell)
end

function HealBot_InitClearSpellNils(spell)
    if not HealBot_Spells[spell].CastTime then
        HealBot_Spells[spell].CastTime=0;
        HealBot_Spells[spell].Cast=0;
    end
    if not HealBot_Spells[spell].Cast then 
        HealBot_Spells[spell].Cast=0;
    end
    if not HealBot_Spells[spell].Mana then
        HealBot_Spells[spell].Mana=10*UnitLevel("player");
    end
    if not HealBot_Spells[spell].Range then
        HealBot_Spells[spell].Range=40;
    end
    if not HealBot_Spells[spell].HealsMin then
        HealBot_Spells[spell].HealsMin=0;
    end
    if not HealBot_Spells[spell].HealsMax then
        HealBot_Spells[spell].HealsMax=0;
    end
    if not HealBot_Spells[spell].HealsExt then
        HealBot_Spells[spell].HealsExt = 0;
    end
    if not HealBot_Spells[spell].Channel then
        HealBot_Spells[spell].Channel = HealBot_Spells[spell].CastTime;
    end
    if not HealBot_Spells[spell].Duration then
        HealBot_Spells[spell].Duration = 0;
    end
    if not HealBot_Spells[spell].Target then
        HealBot_Spells[spell].Target = {"player","party","pet"};
    end
    if not HealBot_Spells[spell].Level then
        HealBot_Spells[spell].Level = 1;
    end

    if not HealBot_Spells[spell].RealHealing then
        HealBot_Spells[spell].RealHealing=0;
    end
    HealBot_Spells[spell].HealsCast = (HealBot_Spells[spell].HealsMin+HealBot_Spells[spell].HealsMax)/2;
    HealBot_Spells[spell].HealsDur = floor((HealBot_Spells[spell].HealsCast+HealBot_Spells[spell].HealsExt) + HealBot_Spells[spell].RealHealing);
end

function HealBot_Generic_Patten(matchStr,matchPattern)
    tmpTest2,tmpTest2,hbHealsMin,hbHealsMax = strfind(matchStr, matchPattern ); 
    return tmpTest2,hbHealsMin,hbHealsMax;
end

function HealBot_FindSpellRangeCast(id)

    if ( not id ) then return; end
  
    spell,spellrank = HealBot_GetSpellName(id);
    if spellrank then 
        spell=spell.."("..spellrank..")"; 
    end
  
    HealBot_TooltipInit();
    HealBot_ScanTooltip:SetSpellBookItem( id, BOOKTYPE_SPELL );
 
    if HealBot_ScanTooltipTextLeft2:GetText() then
        line1=HealBot_ScanTooltipTextLeft2:GetText();
    end
    if HealBot_ScanTooltipTextRight2:GetText() then
        line2 = HealBot_ScanTooltipTextRight2:GetText()
    end
    if HealBot_ScanTooltipTextLeft3:GetText() then
        line3 = HealBot_ScanTooltipTextLeft3:GetText();
    end
  
    if line1 then
        tmpTest,tmpTest,HB_mana = strfind(line1, HB_TOOLTIP_MANA ); 
    end

    if line2 then
        tmpTest,tmpTest,HB_range = strfind(line2, HB_TOOLTIP_RANGE ); 
    end  

    if line3 then
        if ( line3 == HB_TOOLTIP_INSTANT_CAST ) then
            HB_cast = 0;
        elseif line3 == HB_TOOLTIP_CHANNELED then
            HB_cast = 0;
        elseif ( line3 ) then
            tmpTest,tmpTest,HB_cast = strfind(line3, HB_TOOLTIP_CAST_TIME ); 
        end
    end  

    HealBot_OtherSpells[spell] = {spell = {}};
    if not HB_cast then
        HealBot_OtherSpells[spell].CastTime=0;
    else
        HealBot_OtherSpells[spell].CastTime=tonumber(HB_cast);
    end
    if not HB_mana then
        HealBot_OtherSpells[spell].Mana=10*UnitLevel("player");
    else
        HealBot_OtherSpells[spell].Mana=tonumber(HB_mana);
    end
    if not HB_range then
        HealBot_OtherSpells[spell].Range=40;
    else
        HealBot_OtherSpells[spell].Range=tonumber(HB_range);
    end
end

function HealBot_Init_Spells_Defaults(class)

--  if strsub(class,1,4)==HealBot_Class_En[HEALBOT_PALADIN] then
-- PALADIN
    HealBot_Spells = {
        [HEALBOT_HOLY_LIGHT] = {
            CastTime = 2.5, Cast = 2.5, Mana =  35, HealsMin =   50, HealsMax =   60, Level = 1 },

        [HEALBOT_FLASH_OF_LIGHT] = {
            CastTime = 1.5, Cast = 1.5, Duration = 15, Mana =  35, HealsMin =   81, HealsMax =   93, Level = 20 },
    
        [HEALBOT_WORD_OF_GLORY] = {
            CastTime = 0, Cast = 0, Duration = 15, Mana =  35, HealsMin =   81, HealsMax =   93, Level = 20 },
         
        [HEALBOT_DIVINE_LIGHT] = {
            CastTime = 3, Cast = 3, Mana =  35, HealsMin =   50, HealsMax =   60, Level = 1 },        
        
        [HEALBOT_HOLY_RADIANCE] = {
            CastTime = 0, Cast = 0, Duration = 8, Mana = 200, HealsMin = 0, HealsMax = 0, HealsExt = 351, Level = 30, Buff = HEALBOT_HOLY_RADIANCE }, 

        [HEALBOT_LIGHT_OF_DAWN] = {
            CastTime = 0, Cast = 0, Duration = 15, Mana =  35, HealsMin =   81, HealsMax =   93, Level = 20 },
        
--    };
--  end

--  if strsub(class,1,4)==HealBot_Class_En[HEALBOT_DRUID] then
-- DRUID
--    HealBot_Spells = {

        [HEALBOT_REJUVENATION] = { 
            CastTime = 0, Cast = 0, Duration = 12, Mana =  25, HealsMin = 0, HealsMax = 0, HealsExt =   32, Level =  4, Buff = HEALBOT_REJUVENATION },

        [HEALBOT_HEALING_TOUCH] = {
            CastTime = 1.5, Cast = 1.5, Mana =  25, HealsMin =   37, HealsMax =   51, Level  = 1 },
 
        [HEALBOT_NOURISH] = {
            CastTime = 1.5, Cast = 1.5, Mana = 1400, HealsMin = 1883, HealsMax = 2187, Level = 80 },
 
        [HEALBOT_REGROWTH] = {
            CastTime = 2, Cast = 2.0, Duration = 21, Mana =  80, HealsMin =   84, HealsMax =   98, HealsExt =   98, Level = 12, Buff = HEALBOT_REGROWTH },
    
        [HEALBOT_LIFEBLOOM] = {
            CastTime = 0, Cast = 0, Duration = 7, Mana = 220, HealsMin = 600, HealsMax = 600, HealsExt = 224, Level = 64, Buff = HEALBOT_LIFEBLOOM },

        [HEALBOT_WILD_GROWTH] = {
            CastTime = 0, Cast = 0, Duration = 7, Mana = 200, HealsMin = 0, HealsMax = 0, HealsExt = 350, Level = 60, Buff = HEALBOT_WILD_GROWTH }, 

        [HEALBOT_TRANQUILITY] = {
            CastTime = 0, Cast = 0, Duration = 8, Mana = 200, HealsMin = 0, HealsMax = 0, HealsExt = 351, Level = 30, Buff = HEALBOT_TRANQUILITY },    
    
--    };
--  end

--  if strsub(class,1,4)==HealBot_Class_En[HEALBOT_PRIEST] then
-- PRIEST
--    HealBot_Spells = {

        [HEALBOT_HEAL] = {
            CastTime = 3.0, Cast = 3.0, Mana = 155, HealsMin =  295, HealsMax =  341, Level = 16 }, 

        [HEALBOT_GREATER_HEAL] = {
            CastTime = 3.0, Cast = 3.0, Mana =  370, HealsMin = 899, HealsMax = 1013, Level = 40 }, 
    
        [HEALBOT_BINDING_HEAL] = {
            CastTime = 1.5, Cast = 1.5, Mana =  705, HealsMin = 1042, HealsMax = 1338, Level = 64 }, 
    
        [HEALBOT_PRAYER_OF_MENDING] = {
            CastTime = 0, Cast = 0, Mana =  390, HealsMin = 800, HealsMax = 800, Level = 68, 
            Buff=HEALBOT_PRAYER_OF_MENDING, NoBonus=true, Duration = 30}, 
    
        [HEALBOT_PRAYER_OF_HEALING] = {
            CastTime = 3.0, Cast = 3.0, Mana =  410, HealsMin = 301, HealsMax = 321, Level = 30 }, 

        [HEALBOT_PENANCE] = {
            CastTime = 0, Cast = 0, Duration = 2, Mana =  400, HealsMin = 0, HealsMax = 0, HealsExt =   2130, Level =  60, Buff = HEALBOT_PENANCE }, 
            
        [HEALBOT_RENEW] = {
            CastTime = 0, Cast = 0, Duration = 15, Mana =  30, HealsMin = 0, HealsMax = 0, HealsExt =   45, Level =  8, Buff = HEALBOT_RENEW }, 
    
        [HEALBOT_FLASH_HEAL] = {
            CastTime = 1.5, Cast = 1.5, Mana = 125, HealsMin = 193, HealsMax = 237, Level = 20 }, 
    
        [HEALBOT_POWER_WORD_SHIELD] = {
            CastTime = 0, Cast = 0, Shield = 30, Mana =  45, HealsMin =  44, HealsMax =  44, Level =  6,
            Buff= HEALBOT_POWER_WORD_SHIELD, Duration = 30  }, 

--    };
--  end

--  if strsub(class,1,4)==HealBot_Class_En[HEALBOT_SHAMAN] then
-- SHAMAN
--    HealBot_Spells = {

        [HEALBOT_HEALING_WAVE] = {
             CastTime = 1.5, Cast = 1.5, Mana =  25, HealsMin =   34, HealsMax =   44, Level =  1 }, 

        [HEALBOT_GREATER_HEALING_WAVE] = {
             CastTime = 3, Cast = 3, Mana = 105, HealsMin = 162, HealsMax = 186, Level = 20 }, 
    
        [HEALBOT_CHAIN_HEAL] = {
            CastTime = 2.5, Cast = 2.5, Mana = 260, HealsMin = 320, HealsMax = 368, Level = 40 },

        [HEALBOT_EARTH_SHIELD] = {
            CastTime = 0, Cast = 0, Duration = 600, Mana = 0, HealsMin = 0, HealsMax = 0, HealsExt =  0, Level = 50 },

        [HEALBOT_WATER_SHIELD] = {
            CastTime = 0, Cast = 0, Duration = 600, Mana = 0, HealsMin = 0, HealsMax = 0, HealsExt =  0, Level = 20 },

        [HEALBOT_RIPTIDE] = {
            CastTime = 0, Cast = 0, Duration = 15, Mana = 250, HealsMin = 639, HealsMax = 691, HealsExt =  500, Level = 60 },
			
        [HEALBOT_HEALING_RAIN] = {
            CastTime = 0, Cast = 0, Duration = 15, Mana = 250, HealsMin = 639, HealsMax = 691, HealsExt =  500, Level = 60 },
            
        [HEALBOT_HEALING_SURGE] = {
           CastTime = 1.5, Cast = 1.5, Mana = 105, HealsMin = 162, HealsMax = 186, Level = 20 }, 
            
--    };
--  end

--  if strsub(class,1,4)==HealBot_Class_En[HEALBOT_HUNTER] then
--  Hunter
--    HealBot_Spells = {
        [HEALBOT_MENDPET] = {
            CastTime = 0, Cast = 0, Duration = 15, Mana =  40, HealsMin = 0, HealsMax = 0, HealsExt =   125, Level =  12, Buff = HEALBOT_MENDPET }, 
    };
--  end

end


function HealBot_Init_SmartCast()
    if strsub(HealBot_PlayerClassEN,1,4)=="PRIE" then
        SmartCast_Res=HEALBOT_RESURRECTION;
    elseif strsub(HealBot_PlayerClassEN,1,4)=="DRUI" then
        SmartCast_Res=HEALBOT_REVIVE;
    elseif strsub(HealBot_PlayerClassEN,1,4)=="PALA" then
        SmartCast_Res=HEALBOT_REDEMPTION;
    elseif strsub(HealBot_PlayerClassEN,1,4)=="SHAM" then
        SmartCast_Res=HEALBOT_ANCESTRALSPIRIT;
    end
end
