-- Stormlash Totem Watcher

local tracked_spellid = 120668
local tracked_class = "SHAMAN"
local tracked_event = "SPELL_SUMMON"
local tracked_levelreq = 78
local tracked_cooldown = 300
local tracked_duration = 10

local optionFrame
local displayWidgets = {}

tracked_units = {}
local temporary_units = {}

local visibilityChoice = {
	"Show", -- 1
	"Hide", -- 2
}

local alignText = {
	"LEFT", -- 1
	"RIGHT", -- 2
	"CENTER", -- 3
}

local anchorPoint = {
	"LEFT", -- 1
	"RIGHT", -- 2
	"CENTER", -- 3
	"TOP", -- 4
	"BOTTOM", -- 5
	"BOTTOMLEFT", -- 6
	"BOTTOMRIGHT", -- 7
	"TOPLEFT", -- 8
	"TOPRIGHT", -- 9
}

local anchorPointVertical = {
	"CENTER", -- 1
	"TOP", -- 2
	"BOTTOM", -- 3
}

local anchorPointHorizontal = {
	"CENTER", -- 1
	"LEFT", -- 2
	"RIGHT", -- 3
}

local growFirst = {
	{ first = 1, second = nil, text = "LEFT" }, -- 1
	{ first = 2, second = nil, text = "RIGHT" }, -- 2
	{ first = 4, second = nil, text = "TOP" }, -- 3
	{ first = 5, second = nil, text = "BOTTOM" }, -- 4
}

local growSecond = {
	{ first = nil, second = 1, text = "Top / Left" }, -- 1
	{ first = nil, second = 2, text = "Bottom / Right" }, -- 2
}

local fontList = {
	{ first = "Fonts\\ARIALN.TTF", second = nil, text = "Arial Narrow" }, -- 1
	{ first = "Fonts\\FRIZQT__.TTF", second = nil, text = "Friz Quadrata TT" }, -- 2
	{ first = "Fonts\\skurri.ttf", second = nil, text = "Skurri" }, -- 3
	{ first = "Fonts\\MORPHEUS.ttf", second = nil, text = "Morpheus" }, -- 4
}

local fontStyle = {
	"None", -- 1
    "MONOCHROME", -- 2
    "OUTLINE", -- 3
    "THICKOUTLINE", -- 4
}

sltw_settings = {
	["display_border"] = 2,
	["display_zoom"] = 100, -- percent
	["display_size"] = 60, -- pixels
	["display_icon_zoom"] = 10, -- percent
	["display_spacing"] = 10, -- pixels
	["display_grow_first"] = 1, -- index growFirst
	["display_grow_second"] = 1, -- index growSecond
	["display_wrap"] = 5,
	["display_max"] = 19,

	["name_show"] = 1, -- index visibilityChoice
	["name_text_size"] = 10, -- pixels
	["name_text_font"] = 1, -- index fontList
	["name_text_outline"] = 3, -- index fontStyle
	["name_text_align"] = 1, -- index alingText

	["timer_show"] = 1, -- index visibilityChoice
	["timer_text_size"] = 20, -- pixels
	["timer_text_font"] = 1, -- index fontList
	["timer_text_outline"] = 4, -- index fontStyle
	["timer_text_align"] = 3, -- index alingText
	
	["position_vertical"] = 1, -- index anchorPointVertical
	["position_horizontal"] = 1, -- index anchorPointHorizontal
	["position_delta_x"] = 0,
	["position_delta_y"] = 0,

	["name_anchor_vertical"] = 3, -- index anchorPointVertical
	["name_anchor_delta_x"] = 0,
	["name_anchor_delta_y"] = 0, 

	["timer_anchor_vertical"] = 1, -- index anchorPointVertical
	["timer_anchor_delta_x"] = 0,
	["timer_anchor_delta_y"] = 0, 
}

local function settings( index )
	return sltw_settings[ index ]
end

local function settings_set( index, value )
	sltw_settings[ index ] = value
end

local function checkUnit( unit )
	local loc, eng, id = UnitClass( unit )
	if eng == tracked_class then
		if UnitLevel( unit ) >= tracked_levelreq then
			local name = string.gsub( GetUnitName( unit, true ), " ", "" ) 
			temporary_units[ name ] = 0
		end
	end
end

local function updateUnits()
	wipe( temporary_units )
	checkUnit( "player" )
	local count = GetNumGroupMembers()
	local prefix = "party"
	if IsInRaid() then
		prefix = "raid"
	end
	for i = 1, count do
		checkUnit( prefix .. tostring(i) )
	end
	for k,v in pairs( temporary_units ) do
		if tracked_units[ k ] == nil then
			tracked_units[ k ] = 0
		end
	end
	for k,v in pairs( tracked_units ) do
		if temporary_units[ k ] == nil then
			tracked_units[ k ] = nil
		end
	end
	local i = 1
	for k,v in pairs( tracked_units ) do
		if k == UNKNOWNOBJECT then
			name_update_request = true
		end
		local name = string.gsub( k, "-.*", "" )
		displayWidgets[ i ].name:SetText( name )
		displayWidgets[ i ].frame:Show()
		i = i + 1
	end
	for j = i, settings( "display_max" ) do
		displayWidgets[ j ].name:SetText( "" )
		displayWidgets[ j ].frame:Hide()
	end
end

local function updateDisplay()
	local t = GetTime()
	local i = 1
	for name, last in pairs( tracked_units ) do
		local widgets = displayWidgets[ i ]
		local elapsed = t - last
		local remain = tracked_cooldown - elapsed
		if elapsed > tracked_cooldown then -- available
			widgets.mask:SetTexture( 0, 0, 0, 0.5 )
			widgets.timer:SetText( "" )
		elseif elapsed > tracked_duration then -- on cooldown
			local m = remain / 60
			local s = remain % 60
			widgets.mask:SetTexture( 0, 0, 0, 0.8 ) 
			if remain < 10 then
				widgets.timer:SetTextColor( 1.00, 0.00, 0.00, 1 ) -- red
				widgets.timer:SetText( string.format( "%i", remain ) )
			elseif remain < 30 then
				widgets.timer:SetTextColor( 1.00, 0.62, 0.00, 1 ) -- orange
				widgets.timer:SetText( string.format( "%i", remain ) )
			elseif remain < 60 then
				widgets.timer:SetTextColor( 1.00, 1.00, 0.00, 1 ) -- yellow
				widgets.timer:SetText( string.format( "%i", remain ) )
			elseif remain < 600 then
				widgets.timer:SetTextColor( 1.00, 1.00, 1.00, 1 ) -- white
				widgets.timer:SetText( string.format( "%i:%02i", m, s ) )
			else
				widgets.timer:SetTextColor( 1.00, 1.00, 1.00, 1 ) -- white
				widgets.timer:SetText( string.format( "%im", m ) )
			end
		else -- buff is up
			widgets.timer:SetTextColor( 0.00, 0.75, 0.00, 1 ) -- green
			widgets.mask:SetTexture( 0, 0, 0, 0.0 )
			local buff = tracked_duration - elapsed
			widgets.timer:SetText( string.format( "%i", floor( buff ) ) )
		end
		i = i + 1
	end
end

local function createGui()
	local data = { GetSpellInfo( tracked_spellid ) }
	for i = 1, settings( "display_max" ) do
		local temp_frame = CreateFrame( "Frame", "sltw_frame_" .. tostring(i), UIParent )
		temp_frame:Hide()
		local temp_bkgd = temp_frame:CreateTexture( "sltw_frame_bkgd_" .. tostring(i), "BACKGROUND" )
		local temp_icon = temp_frame:CreateTexture( "sltw_frame_icon_" .. tostring(i), "BORDER" )
		local temp_mask = temp_frame:CreateTexture( "sltw_frame_mask_" .. tostring(i), "ARTWORK" )
		local temp_name = temp_frame:CreateFontString( "sltw_frame_name_" .. tostring(i) )
		local temp_timer = temp_frame:CreateFontString( "sltw_frame_timer_" .. tostring(i) )
		temp_bkgd:SetTexture( 0, 0, 0, 1 )
		temp_icon:SetTexture( data[ 3 ] )
		displayWidgets[ i ] = {
			frame = temp_frame,
			bkgd = temp_bkgd,
			icon = temp_icon,
			mask = temp_mask,
			name = temp_name,
			timer = temp_timer,
		}
	end
	local timer_elapsed = 0
	local name_elapsed = 0
	displayWidgets[ 1 ].frame:SetScript("OnUpdate",
		function( self, elapsed )
			name_elapsed = name_elapsed + elapsed
			if name_update_request and name_elapsed >= 1 then
				name_update_request = false
				name_elapsed = 0
				updateUnits()
			end
			timer_elapsed = timer_elapsed + elapsed
			if timer_elapsed >= 0.1 then
				timer_elapsed = 0
				updateDisplay()
			end
		end
	) 
end

local function getPoint( direction )
	local point = nil
	local relative = nil
	local dx = 0
	local dy = 0
	if direction == 1 then
		point = 2 -- RIGHT
		relative = 1 -- LEFT
		dx = -1
	elseif direction == 2 then -- RIGHT
		point = 1 -- LEFT
		relative = 2 -- RIGHT
		dx = 1
	elseif direction == 4 then -- TOP
		point = 5 -- BOTTOM
		relative = 4 -- TOP
		dy = 1
	elseif direction == 5 then -- BOTTOM
		point = 4 -- TOP
		relative = 5 -- BOTTOM
		dy = -1
	end				
	return point, relative, dx, dy
end

local function positionGui()
	local row = 0
	local col = 0
	for i = 1, settings( "display_max" ) do
		local w = displayWidgets[ i ]
		w.frame:ClearAllPoints()
		w.frame:SetSize( settings( "display_size" ), settings ( "display_size" ) )
		w.frame:SetScale( settings( "display_zoom" ) / 100 )
		if i == 1 then
			w.frame:SetPoint(
				anchorPointVertical[ settings( "position_vertical" ) ],
				UIParent,
				anchorPointVertical[ settings( "position_vertical" ) ],
				settings( "position_delta_x" ),
				settings( "position_delta_y" )
			)
			w.frame:SetPoint(
				anchorPointHorizontal[ settings( "position_horizontal" ) ],
				UIParent,
				anchorPointHorizontal[ settings( "position_horizontal" ) ],
				settings( "position_delta_x" ),
				settings( "position_delta_y" )
			)
		else
			local n = i - 1
			local point = nil
			local relative = nil
			local delta_x = 0
			local delta_y = 0
			local previous = nil
			local gf = growFirst[ settings( "display_grow_first" ) ].first
			local gs = growSecond[ settings( "display_grow_second" ) ].second
			if n % settings( "display_wrap" ) == 0 then
				if gf == 1 or gf == 2 then -- LEFT or RIGHT
					if gs == 1 then
						point, relative, delta_x, delta_y = getPoint( 4 ) -- TOP
					else
						point, relative, delta_x, delta_y = getPoint( 5 ) -- BOTTOM
					end
				else -- TOP or BOTTOM
					if gs == 1 then
						point, relative, delta_x, delta_y = getPoint( 1 ) -- LEFT
					else
						point, relative, delta_x, delta_y = getPoint( 2 ) -- RIGHT
					end
				end
				previous = displayWidgets[ i - settings( "display_wrap" ) ].frame
			else
				previous = displayWidgets[ n ].frame
				point, relative, delta_x, delta_y = getPoint( gf )
			end
			w.frame:SetPoint(
				anchorPoint[ point ],
				previous,
				anchorPoint[ relative ],
				delta_x * settings( "display_spacing" ),
				delta_y * settings( "display_spacing" )
			)
		end
		
		w.bkgd:ClearAllPoints()
		w.bkgd:SetAllPoints( w.frame )
		
		w.icon:ClearAllPoints()
		w.icon:SetPoint( "TOP", w.frame, "TOP", 0, -settings( "display_border" ) )
		w.icon:SetPoint( "LEFT", w.frame, "LEFT", settings( "display_border" ), 0 )
		w.icon:SetPoint( "RIGHT", w.frame, "RIGHT", - settings( "display_border" ), 0 )
		w.icon:SetPoint( "BOTTOM", w.frame, "BOTTOM", 0, settings( "display_border" ) )
		w.icon:SetTexCoord(
			settings( "display_icon_zoom" ) / 100,
			1 - settings( "display_icon_zoom" ) / 100,
			settings( "display_icon_zoom" ) / 100,
			1 - settings( "display_icon_zoom" ) / 100
		)
		
		w.mask:ClearAllPoints()
		w.mask:SetAllPoints( w.icon )
		
		w.timer:ClearAllPoints()
		w.timer:SetWidth( settings( "display_size" ) )
		w.timer:SetFont(
			fontList[ settings( "timer_text_font" ) ].first,
			settings( "timer_text_size" ),
			fontStyle[ settings( "timer_text_outline" ) ]
		)
		w.timer:SetJustifyH( alignText[ settings( "timer_text_align" ) ] )
		w.timer:SetPoint(
			anchorPointVertical[ settings( "timer_anchor_vertical" ) ],
			w.frame,
			anchorPointVertical[ settings( "timer_anchor_vertical" ) ],
			settings( "timer_anchor_delta_x" ),
			settings( "timer_anchor_delta_y" )
		)
		if settings( "timer_show" ) == 1 then
			w.timer:Show()
		else
			w.timer:Hide()
		end

		w.name:ClearAllPoints()
		w.name:SetWidth( settings( "display_size" ) )
		w.name:SetFont(
			fontList[ settings( "name_text_font" ) ].first,
			settings( "name_text_size" ),
			fontStyle[ settings( "name_text_outline" ) ]
		)
		w.name:SetJustifyH( alignText[ settings( "name_text_align" ) ] )
		w.name:SetPoint(
			anchorPointVertical[ settings( "name_anchor_vertical" ) ],
			w.frame,
			anchorPointVertical[ settings( "name_anchor_vertical" ) ],
			settings( "name_anchor_delta_x" ),
			settings( "name_anchor_delta_y" )
		)
		if settings( "name_show" ) == 1 then
			w.name:Show()
		else
			w.name:Hide()
		end
	end
end

local function createSlider( parent, name, valueMin, valueMax, valueStep, description, syncSetting )
	local valueCurrent = settings( syncSetting )
	local frame = CreateFrame('Slider', name, parent, 'OptionsSliderTemplate')
	frame:SetMinMaxValues(valueMin, valueMax)
	frame:SetValue(valueCurrent)
	frame:SetValueStep(valueStep)
	getglobal(frame:GetName() .. 'Low'):SetText( tostring(valueMin) )
	getglobal(frame:GetName() .. 'High'):SetText( tostring(valueMax) )
	getglobal(frame:GetName() .. 'Text'):SetText( description .. " (" .. tostring(valueCurrent) .. ")" )
	frame:Show()
	frame:SetScript("OnValueChanged",
		function( widget, value )
			settings_set( syncSetting, value )
			getglobal(name .. 'Text'):SetText( description .. " (" .. tostring(value) .. ")" )
			positionGui()
		end
	) 
	return frame
end

local function createCombo( parent, name, items, description, syncSetting, itemsValueIsTable, width )
	local frame = CreateFrame("Button", name, parent, "UIDropDownMenuTemplate")
	frame:ClearAllPoints()
	frame:Show()
	frame:SetWidth( width )
	UIDropDownMenu_Initialize( frame, 
		function( widget_combo, level )
			for k,v in pairs( items ) do
				local info = UIDropDownMenu_CreateInfo()
				if itemsValueIsTable then
					info.text = v.text
				else
					info.text = v
				end
				info.value = k
				info.func = function( widget_item, arg1, arg2, checked )
					UIDropDownMenu_SetSelectedID( widget_combo, widget_item:GetID() )
					if not checked then -- means re-selecting already selected
						settings_set( syncSetting, widget_item:GetID() )
						positionGui()
					end
				end
				UIDropDownMenu_AddButton( info, level )
			end
		end
	)
	
	local text = frame:CreateFontString( name .. "_text" )
	text:SetWidth( width )
	text:SetFontObject( "GameFontHighlightSmall" )
	text:SetText( description )
	text:SetJustifyH( "LEFT" )
	text:SetJustifyV( "TOP" )
	text:SetPoint("BOTTOM",frame,"TOP")
	
	UIDropDownMenu_SetWidth( frame, width )
	UIDropDownMenu_SetButtonWidth( frame, width )
	UIDropDownMenu_SetSelectedID( frame, settings( syncSetting ) )
	UIDropDownMenu_JustifyText( frame, "LEFT" )
	return frame
end

local function createOptions()
	local backdrop = { 
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", 
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", 
		tile = false, tileSize = 32, edgeSize = 16, 
		insets = { left = 4, right = 4, top = 4, bottom = 4 }
    }
	local frame = CreateFrame( "Frame", "sltw_optionframe", UIParent )
	optionFrame = frame
	frame:Hide()
    frame:SetBackdrop( backdrop )
    frame:SetBackdropColor( 0, 0, 0, 0.7 )
	frame:SetSize( 660, 580 )
	frame:SetPoint( "CENTER" )
	
	frame:SetMovable(true)
	frame:EnableMouse(true)
	frame:RegisterForDrag("LeftButton")
	frame:SetScript("OnDragStart", frame.StartMoving)
	frame:SetScript("OnDragStop", frame.StopMovingOrSizing)

	local close = CreateFrame( "Button", "sltw_option_close", frame, "UIPanelButtonTemplate" )
	close:Show()
	close:SetSize( 30, 30 )
	close:SetPoint( "TOPRIGHT", frame, "TOPRIGHT", -5, -5 )
	close:SetText("x")
	close:SetScript("OnClick", function() frame:Hide(); name_update_request = true; end)
	
	local title = frame:CreateFontString( "sltw_option_title" )
	title:SetFontObject( "GameFontNormal" )
	title:SetText( "Stormlash Totem Watcher - Settings" )
	title:SetPoint( "TOPLEFT", frame, "TOPLEFT", 5, -5 )
	title:SetPoint( "RIGHT", frame, "RIGHT", -5, 0 )
	title:SetHeight( title:GetStringHeight() )

	local label_icons = frame:CreateFontString( "sltw_option_label_icons" )
	label_icons:SetFontObject( "GameFontNormal" )
	label_icons:SetText( "Icons" )
	label_icons:SetPoint( "TOP", title, "TOP", 0, - 5 - title:GetStringHeight() )
	label_icons:SetPoint( "LEFT", frame, "LEFT", 5, 0 )
	label_icons:SetPoint( "RIGHT", frame, "RIGHT", -5, 0 )
	label_icons:SetJustifyH( "LEFT" )
	label_icons:SetHeight( label_icons:GetStringHeight() )
	
	local display_size =  createSlider( frame, "sltw_option_display_size", 20, 200, 1, "Size", "display_size" )
	display_size:SetWidth( 200 )
	display_size:SetPoint( "TOPLEFT", label_icons, "BOTTOMLEFT", 5, - label_icons:GetStringHeight() )

	local display_spacing =  createSlider( frame, "sltw_option_display_spacing", -10, 50, 1, "Spacing", "display_spacing" )
	display_spacing:SetWidth( 200 )
	display_spacing:SetPoint("LEFT", display_size, "RIGHT", 20, 0)

	local display_wrap =  createSlider( frame, "sltw_option_display_wrap", 1, 20, 1, "Wrap", "display_wrap" )
	display_wrap:SetWidth( 200 )
	display_wrap:SetPoint("LEFT", display_spacing, "RIGHT", 20, 0)

	local display_border =  createSlider( frame, "sltw_option_display_border", 0, 30, 1, "Border", "display_border" )
	display_border:SetWidth( 200 )
	display_border:SetPoint("TOPLEFT", display_size, "BOTTOMLEFT", 0, -30)

	local display_zoom =  createSlider( frame, "sltw_option_display_zoom", 20, 500, 1, "Scale", "display_zoom" )
	display_zoom:SetWidth( 200 )
	display_zoom:SetPoint("LEFT", display_border, "RIGHT", 20, 0)

	local display_icon_zoom =  createSlider( frame, "sltw_option_display_icon_zoom", 0, 49, 1, "Icon zoom", "display_icon_zoom" )
	display_icon_zoom:SetWidth( 200 )
	display_icon_zoom:SetPoint("LEFT", display_zoom, "RIGHT", 20, 0)

	local label_position = frame:CreateFontString( "sltw_option_label_position" )
	label_position:SetFontObject( "GameFontNormal" )
	label_position:SetText( "Position" )
	label_position:SetPoint( "TOP", display_border, "BOTTOM", 0, - 15 )
	label_position:SetPoint( "LEFT", frame, "LEFT", 5, 0 )
	label_position:SetPoint( "RIGHT", frame, "RIGHT", -5, 0 )
	label_position:SetJustifyH( "LEFT" )
	label_position:SetHeight( label_position:GetStringHeight() )
	
	local display_grow_first = createCombo( frame, "sltw_option_display_grow_first", growFirst, "Growing first", "display_grow_first", true, 150 )
	display_grow_first:SetPoint( "TOPLEFT", label_position, "BOTTOMLEFT", 5, - 5 - label_position:GetStringHeight() )
	
	local display_grow_second = createCombo( frame, "sltw_option_display_grow_second", growSecond, "Growing second", "display_grow_second", true, 150 )
	display_grow_second:SetPoint("LEFT", display_grow_first, "RIGHT", 20, 0)
	
	local position_vertical = createCombo( frame, "sltw_option_position_vertical", anchorPointVertical, "Vertical anchor", "position_vertical", false, 150 )
	position_vertical:SetPoint( "TOPLEFT", display_grow_first, "BOTTOMLEFT", 0, -10 )

	local position_horizontal = createCombo( frame, "sltw_option_position_horizontal", anchorPointHorizontal, "Horizontal anchor", "position_horizontal", false, 150 )
	position_horizontal:SetPoint("LEFT", position_vertical, "RIGHT", 20, 0)

	local position_delta_x = createSlider( frame, "sltw_option_position_delta_x", -500, 500, 1, "X offset", "position_delta_x" )
	position_delta_x:SetWidth( 640 )
	position_delta_x:SetPoint( "TOPLEFT", position_vertical, "BOTTOMLEFT", 0, -10 )

	local position_delta_y = createSlider( frame, "sltw_option_position_delta_y", -500, 500, 1, "Y offset", "position_delta_y" )
	position_delta_y:SetWidth( 640 )
	position_delta_y:SetPoint( "TOPLEFT", position_delta_x, "BOTTOMLEFT", 0, -10 )
	
	local label_names = frame:CreateFontString( "sltw_option_label_names" )
	label_names:SetFontObject( "GameFontNormal" )
	label_names:SetText( "Names" )
	label_names:SetPoint( "TOP", position_delta_y, "BOTTOM", 0, - 15 )
	label_names:SetPoint( "LEFT", frame, "LEFT", 5, 0 )
	label_names:SetPoint( "RIGHT", frame, "RIGHT", -5, 0 )
	label_names:SetJustifyH( "LEFT" )
	label_names:SetHeight( label_icons:GetStringHeight() )
	
	local name_text_font = createCombo( frame, "sltw_option_name_text_font", fontList, "Font", "name_text_font", true , 150 )
	name_text_font:SetPoint( "TOPLEFT", label_names, "BOTTOMLEFT", 5, - label_names:GetStringHeight() )

	local name_text_outline = createCombo( frame, "sltw_option_name_text_outline", fontStyle, "Style", "name_text_outline", false, 150 )
	name_text_outline:SetPoint("LEFT", name_text_font, "RIGHT", 20, 0)

	local name_text_align = createCombo( frame, "sltw_option_name_text_align", alignText, "Align", "name_text_align", false, 150 )
	name_text_align:SetPoint("LEFT", name_text_outline, "RIGHT", 20, 0)

	local name_show = createCombo( frame, "sltw_option_name_show", visibilityChoice, "Visibility", "name_show", false, 150 )
	name_show:SetPoint( "TOPLEFT", name_text_font, "BOTTOMLEFT", 0, -10 )
	
	local name_anchor_vertical = createCombo( frame, "sltw_option_name_anchor_vertical", anchorPointVertical, "Vertical anchor", "name_anchor_vertical", false, 150 )
	name_anchor_vertical:SetPoint("LEFT", name_show, "RIGHT", 20, 0)

	local name_text_size = createSlider( frame, "sltw_option_name_text_size", 1, 30, 1, "Font size", "name_text_size" )
	name_text_size:SetWidth( 200 )
	name_text_size:SetPoint( "TOPLEFT", name_show, "BOTTOMLEFT", 0, -10 )

	local name_anchor_delta_x = createSlider( frame, "sltw_option_name_anchor_delta_x", -30, 30, 1, "X offset", "name_anchor_delta_x" )
	name_anchor_delta_x:SetWidth( 200 )
	name_anchor_delta_x:SetPoint( "LEFT", name_text_size, "RIGHT", 20, 0 )

	local name_anchor_delta_y = createSlider( frame, "sltw_option_name_anchor_delta_y", -30, 30, 1, "Y offset", "name_anchor_delta_y" )
	name_anchor_delta_y:SetWidth( 200 )
	name_anchor_delta_y:SetPoint( "LEFT", name_anchor_delta_x, "RIGHT", 20, 0 )
	
	local label_timer = frame:CreateFontString( "sltw_option_label_timer" )
	label_timer:SetFontObject( "GameFontNormal" )
	label_timer:SetText( "Timer" )
	label_timer:SetPoint( "TOP", name_text_size, "BOTTOM", 0, - 15 )
	label_timer:SetPoint( "LEFT", frame, "LEFT", 5, 0 )
	label_timer:SetPoint( "RIGHT", frame, "RIGHT", -5, 0 )
	label_timer:SetJustifyH( "LEFT" )
	label_timer:SetHeight( label_icons:GetStringHeight() )
	
	local timer_text_font = createCombo( frame, "sltw_option_timer_text_font", fontList, "Font", "timer_text_font", true, 150 )
	timer_text_font:SetPoint( "TOPLEFT", label_timer, "BOTTOMLEFT", 5, - label_names:GetStringHeight() )

	local timer_text_outline = createCombo( frame, "sltw_option_timer_text_outline", fontStyle, "Style", "timer_text_outline", false, 150 )
	timer_text_outline:SetPoint("LEFT", timer_text_font, "RIGHT", 20, 0)

	local timer_text_align = createCombo( frame, "sltw_option_timer_text_align", alignText, "Align", "timer_text_align", false, 150 )
	timer_text_align:SetPoint("LEFT", timer_text_outline, "RIGHT", 20, 0)

	local timer_show = createCombo( frame, "sltw_option_timer_show", visibilityChoice, "Visibility", "timer_show", false, 150 )
	timer_show:SetPoint( "TOPLEFT", timer_text_font, "BOTTOMLEFT", 0, -10 )
	
	local timer_anchor_vertical = createCombo( frame, "sltw_option_timer_anchor_vertical", anchorPointVertical, "Vertical anchor", "timer_anchor_vertical", false, 150 )
	timer_anchor_vertical:SetPoint("LEFT", timer_show, "RIGHT", 20, 0)

	local timer_text_size = createSlider( frame, "sltw_option_timer_text_size", 1, 30, 1, "Font size", "timer_text_size" )
	timer_text_size:SetWidth( 200 )
	timer_text_size:SetPoint( "TOPLEFT", timer_show, "BOTTOMLEFT", 0, -10 )

	local timer_anchor_delta_x = createSlider( frame, "sltw_option_timer_anchor_delta_x", -30, 30, 1, "X offset", "timer_anchor_delta_x" )
	timer_anchor_delta_x:SetWidth( 200 )
	timer_anchor_delta_x:SetPoint( "LEFT", timer_text_size, "RIGHT", 20, 0 )

	local timer_anchor_delta_y = createSlider( frame, "sltw_option_timer_anchor_delta_y", -30, 30, 1, "Y offset", "timer_anchor_delta_y" )
	timer_anchor_delta_y:SetWidth( 200 )
	timer_anchor_delta_y:SetPoint( "LEFT", timer_anchor_delta_x, "RIGHT", 20, 0 )
end

local events = {}

function events:ADDON_LOADED( self, addon_name, ... )
	if addon_name == "W_StormlashWatcher" then
		createGui()
		positionGui()
		updateDisplay()
		createOptions()
		SLASH_sltw1 = '/sltw';
		SLASH_sltw2 = '/stormlashtotemwatcher';
		SlashCmdList["sltw"] = function()
			for i = 1, settings( "display_max" ) do
				displayWidgets[ i ].frame:Show()
			end
			optionFrame:Show()
		end;
	end
end

function events:PLAYER_LOGIN( self, ... )
	updateUnits()
end

function events:PLAYER_ENTERING_WORLD( self, ... )
	updateUnits()
end

function events:PLAYER_ALIVE( self, ... )
	updateUnits()
end

function events:GROUP_ROSTER_UPDATE( self, ... )
	updateUnits()
end

function events:COMBAT_LOG_EVENT_UNFILTERED( self, timestamp, event, hideCaster, sourceGUID, sourceName, sourceFlags, sourceFlags2, destGUID, destName, destFlags, destFlags2, arg1, arg2, arg3, ... )
	if event == tracked_event then
		if arg1 == tracked_spellid then
			local t = GetTime()
			tracked_units[ sourceName ] = t
			updateDisplay()
		end
	end
end

local function event_init()
	local event_frame = CreateFrame("Frame")
	event_frame:SetScript( "OnEvent",
		function( self, event, ... )
			events[ event ]( self, event, ... ) -- call one of the functions above
		end
	)
	for k, v in pairs(events) do
		event_frame:RegisterEvent( k ) -- Register all events for which handlers have been defined
	end
end

event_init()
