--[==[

An Addon for Resto Druids

potential healer classes that use HoT techniques that could be added in the future ?
-monk
-disc priest



]==]--

DruidHealing = {}
if druidhealing_global_settings == nil then
	druidhealing_global_settings = {}
end
local name, DruidHealing = ...

SLASH_DRUIDHEALING1 = '/druidhealing'
SlashCmdList['DRUIDHEALING'] = function(msg)
	if msg == 'new character' then
		StaticPopup_Show ("Druid_Healing_Initialize")
	end
end

--------------------------------------------------------------------------------------------------------------------------------------------
-- Variables
--------------------------------------------------------------------------------------------------------------------------------------------
--these 2 variables allow healer to be displayed
local myMaxHealth = 0
local myOldHealth = 0

--these 2 tables will act as collections containing the player cooldown bars and the buff bars
my_hot_bars ={}
my_cooldown_bars = {}
--used to determine which target buffs are displayed for
local mouse_target = nil

--character index from druidhealing_global_settings
DruidHealing.char_PK = nil

--local data table object for updating colour
local datatable_object = nil
local colour_box_var = nil

--print text colour
local text_header = '|cffabd473DruidHealing: '

--SPELL DATA TABLES
local cooldowns_data_table = {
	{ class = 'DRUID', name = 'Cenarion Ward', icon = 132137, display = true, RGB = { r = 0.7, g = 1, b = 0.7} },
	{ class = 'DRUID', name = 'Swiftmend', icon = 136085, display = true, RGB = { r = 0, g = 0.44, b = 0.87} }, 
	{ class = 'DRUID', name = 'Nature\'s Cure', icon = 136081, display = true, RGB = { r = 1, g = 1, b = 1} },
	{ class = 'DRUID', name = 'Tranquility', icon = 132137, display = true, RGB = { r = 0.1, g = 0.1, b = 0.6} },
	{ class = 'DRUID', name = 'Wild Growth', icon = 236153, display = true, RGB = { r = 0.2, g = 1.0, b = 0.6 } },
	{ class = 'DRUID', name = 'Mass Entanglement', icon = 236153, display = true, RGB = { r = 0.2, g = 1.0, b = 0.6 } },
	{ class = 'DRUID', name = 'Mighty Bash', icon = 132114, display = true, RGB = { r = 0.2, g = 1.0, b = 0.6 } },
	{ class = 'DRUID', name = 'Typhoon', icon = 236170, display = true, RGB = { r = 0.2, g = 1.0, b = 0.6 } },
	{ class = 'DRUID', name = 'Incarnation: Tree of Life', icon = 236157, display = true, RGB = { r = 0.2, g = 1.0, b = 0.6 } },
	{ class = 'DRUID', name = 'Flourish', icon = 538743, display = true, RGB = { r = 0.2, g = 1.0, b = 0.6 } },
	{ class = 'DRUID', name = 'Rebirth', icon = 136080, display = true, RGB = { r = 0.2, g = 1.0, b = 0.6 } },
}

--duration is no longer used, left in table for now
local hots_data_table = {
	{ class = 'DRUID', name = 'Regrowth', icon = 136085, display = true, duration = 12, RGB = { r = 0.0, g = 0.8, b = 0.0 } }, 
	{ class = 'DRUID', name = 'Rejuvenation', icon = 136081, display = true, duration = 12, RGB = { r = 1.0, g = 0.0, b = 0.49 } },
	{ class = 'DRUID', name = 'Lifebloom', icon = 134206, display = true, duration = 15, RGB = { r = 0.2, g = 0.8, b = 0.6 } },
	{ class = 'DRUID', name = 'Cenarion Ward', icon = 132137, display = true, duration = 30, RGB ={ r = 0.74, g = 0.86, b = 0.74 } },
	{ class = 'DRUID', name = 'Wild Growth', icon = 236153, display = true, duration = 7, RGB = { r = 0.2, g = 1.0, b = 0.6 } },
}


--------------------------------------------------------------------------------------------------------------------------------------------
-- Functions
--------------------------------------------------------------------------------------------------------------------------------------------

--this function is called when the players combat log is changed
function DruidHealing.print_health()
	local myHealth = UnitHealth("player")
	if myHealth < myOldHealth then
		print("Healer Health at "..tonumber(string.format("%.0f", (myHealth/myMaxHealth)*100)))
		myOldHealth = myHealth
	end
end

function DruidHealing.make_frame_movable(frame)
	frame:SetMovable(true)
	frame:EnableMouse(true)
	frame:RegisterForDrag("LeftButton")
	frame:SetScript("OnDragStart", frame.StartMoving)
	frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
end

function DruidHealing.show_color_picker(r, g, b, a, callback)
	ColorPickerFrame.hasOpacity, ColorPickerFrame.opacity = (a ~= nil), a
	ColorPickerFrame.previousValues = {r, g, b, a}
	ColorPickerFrame.func = callback
	ColorPickerFrame.opacityFunc = callback
	ColorPickerFrame.cancelFunc = callback
	ColorPickerFrame:SetColorRGB(r, g, b)
	ColorPickerFrame:Hide()
	ColorPickerFrame:Show()
end

function DruidHealing.color_picker_callback(old_rgb)
	local new_R, new_G, new_B, new_A
	if old_rgb then
		new_R, new_G, new_B, new_A = unpack(old_rgb)
	else
		new_A = OpacitySliderFrame:GetValue()
		new_R, new_G, new_B = ColorPickerFrame:GetColorRGB()
	--keep simple for now for ease of reading but could skip the extra variables in future
		datatable_object.RGB.r, datatable_object.RGB.g, datatable_object.RGB.b, datatable_object.RGB.a = new_R, new_G, new_B, new_A
		colour_box_var:SetBackdropColor(new_R, new_G, new_B, new_A)
		DruidHealing.update_bar_colour(datatable_object.name, datatable_object.RGB.r, datatable_object.RGB.g, datatable_object.RGB.b, datatable_object.RGB.a)
	
	end
end

function DruidHealing.update_bar_colour(name, r, g, b, a)
	for k, bar in ipairs(my_cooldown_bars) do
		if bar.id == name then
			bar.bar:SetStatusBarColor(r, g, b, a)
		end
	end
	for k, bar in ipairs(my_hot_bars) do
		if bar.id == name then
			bar.bar:SetStatusBarColor(r, g, b, a)
		end
	end
end

function DruidHealing.create_colour_box(frame, r, g, b, width, height)
	frame:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background", 
												edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
												tile = true, tileSize = 8, edgeSize = 8, 
												insets = { left = 2, right = 2, top = 2, bottom = 2 }});
	frame:SetBackdropColor(r, g, b, 1)
	frame:SetSize(width, height)
end

--------------------------------------------------------------------------------------------------------------------------------------------
-- First Load
--------------------------------------------------------------------------------------------------------------------------------------------
-- function loadCharSettings()
	-- print('func called')
	-- local character_settings = { GUID = UnitGUID('player'), name = select(6, GetPlayerInfoByGUID(UnitGUID('player'))), class = select(2, GetPlayerInfoByGUID(UnitGUID('player'))), cooldown_table = cooldowns_data_table, hots_table = hots_data_table }
	-- table.insert(druidhealing_global_settings, character_settings)
	-- for k, character in ipairs(druidhealing_global_settings) do
		-- if character.GUID == UnitGUID('player') then
			-- DruidHealing.char_PK = k
			-- load_cooldown_bars()
			-- load_cooldown_options()
			-- load_target_buff_bars()
			-- load_hot_options()
		-- end
	-- end
-- end

StaticPopupDialogs["Druid_Healing_Initialize"] = {
	text = "Welcome to Druid Healing, click 'Ok' to load/save character settings.",
	button1 = "Ok",
	OnAccept = function()
		--check if character exists
		for k, character in ipairs(druidhealing_global_settings) do
			if character.GUID == UnitGUID('player') then
				DruidHealing.char_PK = k
				load_cooldown_bars()
				load_cooldown_options()
				load_target_buff_bars()
				load_hot_options()
				print(text_header..'Character exists, loaded ['..character.name..']')
			end
		end
		--if no character exists then add new 
		if DruidHealing.char_PK == nil then
			local character_settings = { GUID = UnitGUID('player'), name = select(6, GetPlayerInfoByGUID(UnitGUID('player'))), class = select(2, GetPlayerInfoByGUID(UnitGUID('player'))), cooldown_table = cooldowns_data_table, hots_table = hots_data_table }
			table.insert(druidhealing_global_settings, character_settings)
			for k, character in ipairs(druidhealing_global_settings) do
				if character.GUID == UnitGUID('player') then
					DruidHealing.char_PK = k
					load_cooldown_bars()
					load_cooldown_options()
					load_target_buff_bars()
					load_hot_options()
					print(text_header..'Added new character ['..character.name..']')
				end
			end
		end
	end,
	timeout = 0,
	whileDead = true,
	hideOnEscape = true,
	preferredIndex = 3,
}
	
--------------------------------------------------------------------------------------------------------------------------------------------
-- Basic frame set-up
--------------------------------------------------------------------------------------------------------------------------------------------
DruidHealing.mainFrame = CreateFrame("FRAME", "Healer_Assist_UI_Frame", UIParent)

--register events
DruidHealing.mainFrame:RegisterEvent("ADDON_LOADED"); -- inform user addon has been loaded
DruidHealing.mainFrame:RegisterEvent("PLAYER_REGEN_DISABLED"); --used to find if healer has aggro
DruidHealing.mainFrame:RegisterEvent("PLAYER_REGEN_ENABLED"); --used to find if healer has lost aggro
DruidHealing.mainFrame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED"); --used
DruidHealing.mainFrame:RegisterEvent("PLAYER_CONTROL_LOST") --used when the healer is silenced etc
DruidHealing.mainFrame:RegisterEvent("UPDATE_MOUSEOVER_UNIT") --used to update buff frame data

--------------------------------------------------------------------------------------------------------------------------------------------
-- Interface Panel
--------------------------------------------------------------------------------------------------------------------------------------------
--create the frame for the addon interface
DruidHealing.optionsPanel = CreateFrame("Frame", "Healer_AssistOptionsPanel", UIParent);
DruidHealing.optionsPanel.name = "Druid Healing";
InterfaceOptions_AddCategory(DruidHealing.optionsPanel);

--add a title
DruidHealing.optionsPanel.title = DruidHealing.optionsPanel:CreateFontString("DruidHealing_OptionsPanelTitle", "OVERLAY", "GameFontNormal")
DruidHealing.optionsPanel.title:SetPoint("TOP", 0, -28)
DruidHealing.optionsPanel.title:SetFont("Fonts\\FRIZQT__.TTF", 16)
DruidHealing.optionsPanel.title:SetText("Druid Healing Spells & Cooldowns")

--about
DruidHealing.optionsPanel.about = DruidHealing.optionsPanel:CreateFontString('DruidHealing_OptionsPanelAbout', 'OVERLAY', 'GameFontNormal')
DruidHealing.optionsPanel.about:SetPoint('TOPLEFT', 24, -60)
DruidHealing.optionsPanel.about:SetSize(580, 50)
DruidHealing.optionsPanel.about:SetJustifyH('LEFT')
DruidHealing.optionsPanel.about:SetText('Select which Cooldowns and Hot\'s to display using the check boxes. The colour can be set by clicking on the colour bar next to the spell.')
DruidHealing.optionsPanel.about:SetTextColor(1,1,1,1)

--icon
DruidHealing.optionsPanel.icon = CreateFrame("Frame", "Icon", DruidHealing.optionsPanel)
DruidHealing.optionsPanel.icon:SetPoint("TOPRIGHT", -10, -10)
DruidHealing.optionsPanel.icon:SetSize(50,50)
DruidHealing.optionsPanel.iconTexture = DruidHealing.optionsPanel.icon:CreateTexture("iconTexture", "BACKGROUND")
DruidHealing.optionsPanel.iconTexture:SetAllPoints(DruidHealing.optionsPanel.icon)
DruidHealing.optionsPanel.iconTexture:SetTexture(625999)

--reload button
--description
DruidHealing.optionsPanel.reload_ui_button_fs = DruidHealing.optionsPanel:CreateFontString(nil, 'OVERLAY', 'GameFontNormal')
DruidHealing.optionsPanel.reload_ui_button_fs:SetPoint('BOTTOMLEFT', 30, 75)
DruidHealing.optionsPanel.reload_ui_button_fs:SetSize(425, 40)
DruidHealing.optionsPanel.reload_ui_button_fs:SetJustifyH('LEFT')
DruidHealing.optionsPanel.reload_ui_button_fs:SetText('After adding/removing a HoT or Cooldown you must reload the UI to apply changes.')
DruidHealing.optionsPanel.reload_ui_button_fs:SetTextColor(1,1,1,1)
--button object
DruidHealing.optionsPanel.reload_ui_button = CreateFrame("BUTTON", nil, DruidHealing.optionsPanel, "UIPanelButtonTemplate")
DruidHealing.optionsPanel.reload_ui_button:SetPoint('BOTTOMLEFT', 465, 90)
DruidHealing.optionsPanel.reload_ui_button:SetText('Reload UI')
DruidHealing.optionsPanel.reload_ui_button:SetSize(80, 22)
DruidHealing.optionsPanel.reload_ui_button:SetScript('OnClick', function() ReloadUI() end)

--new character button
DruidHealing.optionsPanel.new_character_button = CreateFrame('BUTTON', nil, DruidHealing.optionsPanel, 'UIPanelButtonTemplate')
DruidHealing.optionsPanel.new_character_button:SetPoint('BOTTOMLEFT', 440, 50)
DruidHealing.optionsPanel.new_character_button:SetText('Add Character')
DruidHealing.optionsPanel.new_character_button:SetSize(120, 22)
DruidHealing.optionsPanel.new_character_button:SetScript('OnClick', function() 
	StaticPopup_Show ("Druid_Healing_Initialize")
	DruidHealing.optionsPanel.new_character_button:Hide()
	DruidHealing.optionsPanel.new_character_button_fs:Hide()
end)

--info about button
DruidHealing.optionsPanel.new_character_button_fs = DruidHealing.optionsPanel:CreateFontString(nil, 'OVERLAY', 'GameFontNormal')
DruidHealing.optionsPanel.new_character_button_fs:SetPoint('BOTTOMLEFT', 30, 40)
DruidHealing.optionsPanel.new_character_button_fs:SetSize(350, 40)
DruidHealing.optionsPanel.new_character_button_fs:SetJustifyH('LEFT')
DruidHealing.optionsPanel.new_character_button_fs:SetText('If the addon failed to load, sometimes due to other addons, click \'Add Character\' to refresh the settings and launch the UI.')

--cooldown options
DruidHealing.cooldown_header = DruidHealing.optionsPanel:CreateFontString('cooldown_header', 'OVERLAY', 'GameFontNormal')
DruidHealing.cooldown_header:SetPoint('TOPLEFT', 125, -130)
--DruidHealing.cooldown_header:SetSize(600, 30)
--DruidHealing.cooldown_header:SetJustifyH("LEFT")
DruidHealing.cooldown_header:SetText('Cooldowns')
DruidHealing.cooldown_header:SetFont("Fonts\\FRIZQT__.TTF", 14)
--loop to make check boxes
function load_cooldown_options()
	for k, cd in ipairs(druidhealing_global_settings[DruidHealing.char_PK].cooldown_table) do
		--check box
		local cb = CreateFrame("CheckButton", cd.name..'_display_CB', DruidHealing.optionsPanel, "ChatConfigCheckButtonTemplate")
		cb:SetScript('OnClick', function() cd.display = cb:GetChecked() end)
		cb:SetChecked(cd.display)
		cb:SetPoint('TOPLEFT', 30, ((k * -20) -138))
		--font string
		local fs = DruidHealing.optionsPanel:CreateFontString(cd.name..'_cb_fs', 'OVERLAY', 'GameFontNormal')
		fs:SetPoint('TOPLEFT', 60, ((k * -20) -143))
		fs:SetText(cd.name)
		fs:SetTextColor(1,1,1,1)
		--colour
		local colour_box = CreateFrame('BUTTON', cd.name..'_colour_box', DruidHealing.optionsPanel)
		colour_box:SetPoint('TOPLEFT', 230, ((k * -20) -141))
		colour_box:SetScript('OnClick', function() datatable_object = cd colour_box_var = colour_box DruidHealing.show_color_picker(cd.RGB.r, cd.RGB.g, cd.RGB.b, 0.9, DruidHealing.color_picker_callback) end)
		DruidHealing.create_colour_box(colour_box, cd.RGB.r, cd.RGB.g, cd.RGB.b, 50, 16)
		
	end
end

--HoT options
DruidHealing.hot_header = DruidHealing.optionsPanel:CreateFontString('hot_header', 'OVERLAY', 'GameFontNormal')
DruidHealing.hot_header:SetPoint('TOPLEFT', 420, -130)
--DruidHealing.hot_header:SetSize(600, 30)
--DruidHealing.hot_header:SetJustifyH("LEFT")
DruidHealing.hot_header:SetText('HoT\'s')
DruidHealing.hot_header:SetFont("Fonts\\FRIZQT__.TTF", 14)
--loop to make check boxes
function load_hot_options()
	for k, hot in ipairs(druidhealing_global_settings[DruidHealing.char_PK].hots_table) do
		--check box
		local cb = CreateFrame("CheckButton", hot.name..'_display_CB', DruidHealing.optionsPanel, "ChatConfigCheckButtonTemplate")
		cb:SetPoint('TOPLEFT', 330, ((k * -20) -138))
		cb:SetScript('OnClick', function() hot.display = cb:GetChecked() end)
		cb:SetChecked(hot.display)
		--font string
		local fs = DruidHealing.optionsPanel:CreateFontString(hot.name..'_cb_fs', 'OVERLAY', 'GameFontNormal')
		fs:SetPoint('TOPLEFT', 360, ((k * -20) -143))
		fs:SetText(hot.name)
		fs:SetTextColor(1,1,1,1)
		--colour box
		local colour_box = CreateFrame('BUTTON', hot.name..'_colour_box', DruidHealing.optionsPanel)
		colour_box:SetPoint('TOPLEFT', 520, ((k * -20) -141))
		colour_box:SetScript('OnClick', function() datatable_object = hot colour_box_var = colour_box DruidHealing.show_color_picker(hot.RGB.r, hot.RGB.g, hot.RGB.b, 0.9, DruidHealing.color_picker_callback) end)
		DruidHealing.create_colour_box(colour_box, hot.RGB.r, hot.RGB.g, hot.RGB.b, 50, 16)
	end
end

--------------------------------------------------------------------------------------------------------------------------------------------
-- MAIN FRAME - USED TO SHOW COOLDOWN STATUS BARS
--------------------------------------------------------------------------------------------------------------------------------------------
DruidHealing.mainFrame.name = "DruidHealing";
DruidHealing.mainFrame:SetPoint("TOP", 0, -10)
DruidHealing.mainFrame:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background", 
                                            edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
                                            tile = true, tileSize = 16, edgeSize = 16, 
                                            insets = { left = 4, right = 4, top = 4, bottom = 4 }});
DruidHealing.mainFrame:SetBackdropColor(0,0,0,0.8)
DruidHealing.mainFrame:SetSize(20,20)
DruidHealing.make_frame_movable(DruidHealing.mainFrame)

local fs_width = 0
local cd_i = 0
function load_cooldown_bars()
	--loop the cooldown data table and create cooldown bars
	for k, cooldown in ipairs(druidhealing_global_settings[DruidHealing.char_PK].cooldown_table) do
		if cooldown.display == true then
			cd_i = cd_i + 1
			--create a fontstring to act as a title
			local fs = DruidHealing.mainFrame:CreateFontString(cooldown.name..'_fs', 'OVERLAY', 'GameFontNormal')
			fs:SetText(cooldown.name)
			fs:SetPoint("TOPLEFT", 8, (-22*(cd_i-1))-6)
			fs:SetSize(155, 20)
			fs:SetJustifyH('Right')
			--create the status bar
			local sb = CreateFrame("StatusBar", cooldown.name..'sb', DruidHealing.mainFrame)
			sb:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
			sb:GetStatusBarTexture():SetHorizTile(false)
			sb:SetMinMaxValues(0, 100)
			sb:SetValue(0)
			sb:SetSize(180,20)
			sb:SetPoint("TOPLEFT", 170, (-22*(cd_i-1))-6)
			sb:SetStatusBarColor(cooldown.RGB.r,cooldown.RGB.g,cooldown.RGB.b, 0.9)
			--create cooldown object and add to collection
			local sb_item = { id = cooldown.name, title = fs, bar = sb }
			table.insert(my_cooldown_bars, sb_item)
			--resize the frame for new cooldown count
			DruidHealing.mainFrame:SetSize(380, (cd_i * 22) + 10)
		end
	end
end

--------------------------------------------------------------------------------------------------------------------------------------------
-- TARGET BUFFS FRAME - DISPLAYS THER BUFFS ON THE MOUSEOVER TARGET
--------------------------------------------------------------------------------------------------------------------------------------------
DruidHealing.buffsFrame = CreateFrame("FRAME", "Healer_Assist_BuffsFrame_Frame", UIParent)
DruidHealing.buffsFrame:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background", 
                                            edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
                                            tile = true, tileSize = 16, edgeSize = 16, 
                                            insets = { left = 4, right = 4, top = 4, bottom = 4 }});
DruidHealing.buffsFrame:SetBackdropColor(0,0,0,1)

local buff_i = 0
function load_target_buff_bars()
	DruidHealing.buffsFrame:ClearAllPoints()
	DruidHealing.buffsFrame:SetPoint("TOPLEFT", 860, -4)	
	--loop over the HoT data table to create buff bar objects
	for k, HoT in ipairs(druidhealing_global_settings[DruidHealing.char_PK].hots_table) do
		--check for display bool
		if HoT.display == true then
			buff_i = buff_i + 1
			--icon frame
			local f = CreateFrame("Frame", HoT.name..'_icon', DruidHealing.buffsFrame)
			f:SetSize(26, 26)
			f:SetPoint("TOPLEFT", 6,((buff_i-1)*-28)-6)
			--icon texture
			local t = f:CreateTexture("$parent_texture", "BACKGROUND")
			t:SetTexture(HoT.icon)
			t:SetAllPoints(f)
			--buff status bar
			local sb = CreateFrame("StatusBar", HoT.name..'_bar', DruidHealing.buffsFrame)
			sb:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
			sb:GetStatusBarTexture():SetHorizTile(false)
			sb:SetMinMaxValues(0, 100)
			sb:SetValue(0)
			sb:SetSize(200,24)
			sb:SetPoint("TOPLEFT", 34, (-28*(buff_i-1))-7)
			sb:SetStatusBarColor(HoT.RGB.r, HoT.RGB.g, HoT.RGB.b, 0.9)
			--create the object and add to collection
			local hot_bar = { id = HoT.name, icon = t, bar = sb }
			table.insert(my_hot_bars, hot_bar)
			--resize the frame for new buff bar count
			DruidHealing.buffsFrame:SetSize(250, (buff_i*-28)-10)
		end
	end
end

--------------------------------------------------------------------------------------------------------------------------------------------
-- ON UPDATE SCRIPT
--------------------------------------------------------------------------------------------------------------------------------------------
function onUpdate()

	--skip all checks if addon UI disabled -> hard coded as true for now -> need to add setting in options panel
	if 1 == 1 then

		--update the buff frame location to cursor position
		if 1 == 1 then
			local x, y = GetCursorPosition()
			local s = UIParent:GetEffectiveScale()
			DruidHealing.buffsFrame:ClearAllPoints()
			DruidHealing.buffsFrame:SetPoint("BOTTOMLEFT", (x/s)+48, (y/s)-70)
		end
		
		--update the current HoT buffs active on target
		if mouse_target ~= nil then
			for i=1,40 do
				local name, icon, a, spell_type, duration, etime, d = UnitBuff(mouse_target,i) 
				if name then
					for k, HoT in ipairs(my_hot_bars) do
						if name == HoT.id then
							HoT.bar:SetValue((100/duration)*(etime-GetTime()))
						end
						if etime-GetTime() == 0 then
							HoT.bar:SetValue(0)
						end
					end
				end
			end
		end
			
		--update the player cooldown bars
		for k, cd_bar in ipairs(my_cooldown_bars) do	
			local start, duration, enabled, modRate = GetSpellCooldown(cd_bar.id) --this is the check for a spell cooldown, uses bar object id which is also spell name
			if duration ~= nil then
				local remain = ((start + duration) - GetTime())
				if duration > 1.5 then
					cd_bar.bar:SetValue((remain / duration) * 100)
				end
			end
		end
		
	end
	
end

--------------------------------------------------------------------------------------------------------------------------------------------
-- ON EVENT SCRIPT
--------------------------------------------------------------------------------------------------------------------------------------------
function onEvent(self, event, ...)

	if event == "ADDON_LOADED" and select(1, ...) == "DruidHealing" then
		C_Timer.After(2, function()
			for k, character in ipairs(druidhealing_global_settings) do
				if character.GUID == UnitGUID('player') then
					DruidHealing.char_PK = k
					load_cooldown_bars()
					load_cooldown_options()
					load_target_buff_bars()
					load_hot_options()
					print(text_header..'Loaded character ['..character.name..']')
					DruidHealing.optionsPanel.new_character_button:Hide()
					DruidHealing.optionsPanel.new_character_button_fs:Hide()
				end
			end
			if DruidHealing.char_PK == nil then
				StaticPopup_Show ("Druid_Healing_Initialize")
			end
		end)
	end

	if event == "PLAYER_REGEN_DISABLED" then
		print('Healer has Aggro')
		if myOldHealth == 0 then 
			myOldHealth = UnitHealth("player")
		end
		myMaxHealth = UnitHealth("player")
	end
	
	if event == "PLAYER_REGEN_ENABLED" then
		print('Healer lost Aggro')
		myOldHealth = 0
	end
	
	if event == "COMBAT_LOG_EVENT_UNFILTERED" then
		DruidHealing.print_health()
	end
	
	if event == "PLAYER_CONTROL_LOST" then
		print('Healer Has Lost Control')
	end
	
	if event == "UPDATE_MOUSEOVER_UNIT" then
		if UnitIsPlayer('mouseover') then
			mouse_target, realm = UnitName('mouseover')
		end
		--quick hack to remove previous mouseTarget buffs
		for k, HoT in ipairs(my_hot_bars) do
			HoT.bar:SetValue(0)
		end
	end
	
end


--------------------------------------------------------------------------------------------------------------------------------------------
-- Set Scripts
--------------------------------------------------------------------------------------------------------------------------------------------
DruidHealing.mainFrame:SetScript("OnEvent", onEvent);
DruidHealing.mainFrame:SetScript("OnUpdate", onUpdate);