addonName, PakuHud = ...

local frame = nil
local Print = PakuHud.Print
local round = PakuHud.Round
local ToString = PakuHud.ToString

local function CreateLabel(frame, text, fontType)
	local label = frame:CreateFontString(nil, "ARTWORK", fontType)
	label:SetText(ToString(text))
	
	return label
end

local function CreateLargeGameFontLabel(frame, text)
	return CreateLabel(frame, text, "GameFontNormalLarge")
end

local function CreateNormalGameFontLabel(frame, text)
	return CreateLabel(frame, text, "GameFontNormal")
end

local function CreateSmallGameFontLabel(frame, text)
	return CreateLabel(frame, text, "GameFontNormalSmall")
end

local function CreateEditBox(editBoxName, parent, width, height)
	local editBox = CreateFrame("EditBox", editBoxName, parent, "InputBoxTemplate")
	editBox:SetSize(width, height)
	
	return editBox
end

local function CreateCheckbox(checkboxName, parent, text, tooltipText)
	local checkbox = CreateFrame("CheckButton", checkboxName, parent, "InterfaceOptionsCheckButtonTemplate")
	checkbox.text = getglobal(checkboxName .. "Text")
	checkbox.text:SetText(ToString(text))
	checkbox.tooltip = ToString(tooltipText)
	
	return checkbox
end

local function CreateSlider(sliderName, parent, minValue, maxValue, label, tooltipText)
	local slider = CreateFrame("Slider", sliderName, parent, "OptionsSliderTemplate")
	slider:SetSize(200, 16)
	slider:SetOrientation("HORIZONTAL")
	slider:SetMinMaxValues(minValue, maxValue)
	slider:SetValue((maxValue - minValue) / 2)
	slider.tooltipText = tooltipText
	slider.lowText = getglobal(sliderName .. "Low")
	slider.highText = getglobal(sliderName .. "High")
	slider.midText = getglobal(sliderName .. "Text")
	slider.lowText:SetText(ToString(minValue)); --Sets the left-side slider text (default is "Low").
	slider.highText:SetText(ToString(maxValue)); --Sets the right-side slider text (default is "High").
	slider.midText:SetText(ToString(label)); --Sets the "title" text (top-centre of slider).end
	
	return slider
end

local function SaveOptions(self)
--[[
	frameDetails = {
		width = 512,
		height = 512,
		top = 0,
		left = 0,
		showAtMouse = true,
	},
	distanceTolerance = 50
]]--
	PakuHud.db.frameDetails.width = round(self.widthSlider:GetValue())
	PakuHud.db.frameDetails.height = round(self.heightSlider:GetValue())
	PakuHud.db.frameDetails.top = round(self.topSlider:GetValue())
	PakuHud.db.frameDetails.left = round(self.leftSlider:GetValue())
	PakuHud.db.frameDetails.showAtMouse = self.showAtMouseCheckbox:GetChecked()
	
	PakuHud.SetFrameSize()
end

PakuHud.CreateOptionsFrame = function()
	local options = PakuHud.db.frameDetails
	
	frame = CreateFrame("Frame", "PakuHudOptionsFrame", UIParent, InterfaceOptionsFramePanelContainer)
	frame.name = "Pa'Ku HUD"
	frame.okay = SaveOptions
	frame.cancel = function(self) end
	frame.refresh = function(self) end
	
	frame.title = CreateLargeGameFontLabel(frame, "Pa'Ku HUD")
	frame.title:SetPoint("TOPLEFT", 16, -16)
	
	-- Frame Size
	frame.frameSizeTitle = CreateLargeGameFontLabel(frame, "Image Frame Size")
	frame.frameSizeTitle:SetPoint("TOP", frame, "TOP", 0, -32)
	
	frame.widthSlider = CreateSlider("PakuHudOptionsFrameSizeWidth", frame, 1, 1024, "Width", "Set the width of the frame.")
	frame.widthSlider:SetPoint("TOP", frame.frameSizeTitle, "BOTTOM", -150, -30)
	frame.widthSlider:SetValue(options.width)
	
	frame.widthLabel = CreateSmallGameFontLabel(frame, round(frame.widthSlider:GetValue()))
	frame.widthLabel:SetPoint("TOP", frame.widthSlider, "BOTTOM", 0, -7)
	frame.widthSlider:SetScript("OnValueChanged", function(self, value)
		frame.widthLabel:SetText(round(value))
	end)
	
	frame.heightSlider = CreateSlider("PakuHudOptionsFrameSizeHeight", frame, 1, 1024, "Height", "Set the height of the frame.")
	frame.heightSlider:SetPoint("TOP", frame.frameSizeTitle, "BOTTOM", 150, -30)
	frame.heightSlider:SetValue(options.height)

	frame.heightLabel = CreateSmallGameFontLabel(frame, round(frame.heightSlider:GetValue()))
	frame.heightLabel:SetPoint("TOP", frame.heightSlider, "BOTTOM", 0, -7)
	frame.heightSlider:SetScript("OnValueChanged", function(self, value)
		frame.heightLabel:SetText(round(value))
	end)
	
	-- Frame Position
	frame.framePositionTitle = CreateLargeGameFontLabel(frame, "Image Frame Position")
	frame.framePositionTitle:SetPoint("TOP", frame.frameSizeTitle, "BOTTOM", 0, -100)
	
	frame.showAtMouseCheckbox = CreateCheckbox("PakuHudOptionsFrameFollowMouse", frame, "Follow Mouse", "Check this to have the image frame popup at mouse location. Clear to have frame popup in set location.")
	frame.showAtMouseCheckbox:SetPoint("TOP", frame.framePositionTitle, "BOTTOM", -250, -20)
	frame.showAtMouseCheckbox:SetChecked(options.showAtMouse)
	
	local width = round(GetScreenWidth())
	frame.leftSlider = CreateSlider("PakuHudOptionsFramePositionLeft", frame, -width, width, "Left", "Set the left side of the frame.")
	frame.leftSlider:SetPoint("TOP", frame.framePositionTitle, "BOTTOM", -150, -60)
	frame.leftSlider:SetValue(options.left)
	
	frame.leftLabel = CreateSmallGameFontLabel(frame, round(frame.leftSlider:GetValue()))
	frame.leftLabel:SetPoint("TOP", frame.leftSlider, "BOTTOM", 0, -7)
	frame.leftSlider:SetScript("OnValueChanged", function(self, value)
		frame.leftLabel:SetText(round(value))
	end)
	
	local height = round(GetScreenHeight())
	frame.topSlider = CreateSlider("PakuHudOptionsFramePositionTop", frame, -height, height, "Top", "Set the top of the frame.")
	frame.topSlider:SetPoint("TOP", frame.framePositionTitle, "BOTTOM", 150, -60)
	frame.topSlider:SetValue(options.top)

	frame.topLabel = CreateSmallGameFontLabel(frame, round(frame.topSlider:GetValue()))
	frame.topLabel:SetPoint("TOP", frame.topSlider, "BOTTOM", 0, -7)
	frame.topSlider:SetScript("OnValueChanged", function(self, value)
		frame.topLabel:SetText(round(value))
	end)
	
	-- Distance to "Find" known totem
	

	InterfaceOptions_AddCategory(frame)
end
