-- This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
-- To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
-- or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.

local MereHealingFrames, privateVars = ...

MereHealingFrames.Config = MereHealingFrames.Config or {}
MereHealingFrames.Config.UI = MereHealingFrames.Config.UI or {}

MereHealingFrames.Config.UI.Utilities = {}

function MereHealingFrames.Config.UI.Utilities.CreateTextField(parentFrame)
	local backgroundFrame = UI.CreateFrame("Frame", "BackgroundFrame", parentFrame)
	local self = UI.CreateFrame("RiftTextfield", "TextField", backgroundFrame)

	backgroundFrame:SetPoint("TOPLEFT", self, "TOPLEFT", -1, -1)
	backgroundFrame:SetPoint("BOTTOMRIGHT", self, "BOTTOMRIGHT", 1, 1)
	backgroundFrame:SetBackgroundColor(1, 1, 1, 1)

	self:SetBackgroundColor(0,0,0,1)
	self.SetBorderColor = function (self, ... )
		backgroundFrame:SetBackgroundColor(...)
	end

	return self
end

local defaultDpi = 96
function MereHealingFrames.Config.UI.Utilities.CalcPointFromPixels(pixels)
	return pixels * ( 72 / defaultDpi)
end

function MereHealingFrames.Config.UI.Utilities.CalcPointFromPixelsIgnoreHang(pixels)
	return (pixels * 1.3) * ( 72 / defaultDpi)
end

function MereHealingFrames.Config.UI.Utilities.CreateRuler(parentFrame, text)
    local backgroundFrame
    if text then
        backgroundFrame =  UI.CreateFrame("Frame", "RulerFrame", parentFrame)
        backgroundFrame:SetPoint("RIGHT", parentFrame, "RIGHT")
        backgroundFrame:SetPoint("LEFT", parentFrame, "LEFT")
        parentFrame = backgroundFrame
    end

	local ruler = UI.CreateFrame("Frame", "Ruler", parentFrame)

	ruler:SetHeight(2)
	ruler:SetBackgroundColor(0.5,0.5,0.5,1)
	ruler:SetPoint("RIGHT", parentFrame, "RIGHT")
	ruler:SetPoint("LEFT", parentFrame, "LEFT")


    if text then
        local textFrame = UI.CreateFrame("Text", "RulerText", parentFrame)
        textFrame:SetText(text)
        textFrame:SetPoint("CENTER", parentFrame, "CENTER")
        ruler:SetPoint("CENTERY", parentFrame, "CENTERY")

        backgroundFrame:SetHeight(textFrame:GetHeight())

        textFrame:SetBackgroundColor(0,0,0,1)
        textFrame:SetLayer(5)
        backgroundFrame:SetBackgroundColor(0, 0, 0, 0)
        return backgroundFrame
    end

	return ruler
end


MereHealingFrames.Config.UI.Utilities.LabelSlider = {
	LabelWidth = 100
	}

local LabelSlider = MereHealingFrames.Config.UI.Utilities.LabelSlider

function LabelSlider:new(parentFrame, label, minRange, maxRange, labelWidth)
	local this = {}
	this.initialized = false

	setmetatable(this, self)
	self.__index = self

	if labelWidth then
		this.LabelWidth = labelWidth
	end

	this:CreateUI(parentFrame, label, minRange, maxRange)

	return this
end

function LabelSlider:CreateUI(parentFrame, label, minimum, maximum)
	self.frame = UI.CreateFrame("Frame", "Frame"..label, parentFrame)
	self.label = UI.CreateFrame("Text", "Label"..label, self.frame)
	self.slider = UI.CreateFrame("SimpleSlider", "Slider"..label, self.frame)

	self.label:SetText(label)
	self.slider:SetRange(minimum, maximum)

	self.label:SetPoint("LEFT", self.frame, "LEFT")
	self.label:SetWidth(self.LabelWidth)

	self.slider:SetPoint("TOPLEFT", self.frame, "TOPLEFT", self.LabelWidth + 20, 0)
	self.label:SetPoint("CENTERY", self.slider, "CENTERY")

	self.frame:SetHeight(self.slider:GetHeight())
	self.frame:SetWidth(self.slider:GetRight())
end

function LabelSlider:GetContent()
	return self.frame
end


function LabelSlider:SetCurrentValue(value)
	self.slider:SetPosition(value, true)
end

function LabelSlider:GetCurrentValue()
	return self.slider:GetPosition()
end

function LabelSlider:SetCallback(callbackFunction)
	self.slider.Event.SliderChange = function ()
		callbackFunction(self:GetCurrentValue())
		end
end