Quantcast
local L = DressToKillLocals

local frame = CreateFrame("Frame", "DressToKillOptionsFrame", UIParent)
frame.name = L["Dress to Kill"]
frame:Hide()
frame:SetScript("OnShow", function(frame)
	-- Initialize the system
	DressToKill:Initialize()
	local title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
	title:SetPoint("TOPLEFT", frame, "TOPLEFT", 15, -15)
	title:SetPoint("BOTTOMRIGHT", frame, "TOPRIGHT", 10, -45)
	title:SetJustifyH("LEFT")
	title:SetJustifyV("TOP")
	title:SetText(L["Dress to Kill"])

	--[[
	local check = CreateFrame("CheckButton", "DressToKillEnableButton", frame, "UICheckButtonTemplate")
	check:SetPoint("TOPLEFT", frame, "TOPLEFT", 15, -50)
	check:SetWidth(24)
	check:SetHeight(24)
	check:SetChecked(AddonLoaderSV.silent)
	AddonLoaderCheckButtonText:SetText("Hide Loading Messages")
	AddonLoaderCheckButtonText:SetPoint("LEFT", check, "RIGHT")
	check:SetScript("OnClick", function()
		AddonLoaderSV.silent = not AddonLoaderSV.silent
		check:SetChecked(AddonLoaderSV.silent)
	end)
	--]]

	local explain = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
	explain:SetTextColor(1,1,1)
	explain:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, 0)
	explain:SetPoint("RIGHT", frame, "RIGHT", -15, 0)
	explain:SetJustifyH("LEFT")
	explain:SetJustifyV("TOP")
	explain:SetHeight(100)
	explain:SetText(L["Dress to Kill is a simple addon which takes the guesswork out of building equipment sets for your character.  Rather than use a complicated library to scan your items for potential bonuses it sacrifices speed for optimal results.  It will scan through your inventory trying items on until it find the most effective mix.  You can customize the way equipment is evaluated through a custom evaluation function or you can simply use one of the built in functions."])

	local function dropdown_onclick()
		local selected = this.value

		if selected == "__NEW__" then
			-- Make a new entry
			StaticPopup_Show("DRESSTOKILL_NEW_FUNCTION")
		else
			DressToKill.profile.selected = selected
			UIDropDownMenu_SetSelectedValue(dropdown, selected)
			editbox:SetText(DressToKill.profile.weightFuncs[selected].handler)
			DressToKillDeleteButton:Enable()
		end
	end

	local function initdropdown()
		local sort = {}
		for k,v in pairs(DressToKill.profile.weightFuncs) do
			table.insert(sort, k)
		end
		table.sort(sort)

		local info = UIDropDownMenu_CreateInfo()

		for idx,name in ipairs(sort) do
			info.text = name
			info.value = name
			info.func = dropdown_onclick
			info.checked = false
			UIDropDownMenu_AddButton(info)
		end

		info.text = L["New weight function"]
		info.value = "__NEW__"
		info.func = dropdown_onclick
		info.checked = false
		UIDropDownMenu_AddButton(info)
	end

	local dropdownlabel = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
	dropdownlabel:SetPoint("TOPLEFT", explain, "BOTTOMLEFT", 0, -10)
	dropdownlabel:SetText("Weight Function:")
	dropdownlabel:SetHeight(15)
	dropdownlabel:SetWidth(100)

	dropdown = CreateFrame("Frame", "DressToKillDropDown", frame, "UIDropDownMenuTemplate")
	dropdown:EnableMouse(true)
	dropdown:SetPoint("LEFT", dropdownlabel, "RIGHT")
	UIDropDownMenu_Initialize(dropdown, initdropdown)
	UIDropDownMenu_SetSelectedValue(dropdown, DressToKill.profile.selected)
	UIDropDownMenu_SetWidth(160, dropdown)
	UIDropDownMenu_JustifyText("LEFT", dropdown)

	editbox = CreateFrame("EditBox", "DressToKillEditBox", frame)
	editbox:SetPoint("TOPLEFT", dropdownlabel, "BOTTOMLEFT", 0, -15)
	editbox:SetPoint("RIGHT", -15, 0)
	editbox:SetPoint("BOTTOM", 0, 45)
	editbox:SetFont("Interface\\AddOns\\DressToKill\\fonts\\VeraMono.ttf", 12)
	editbox:SetTextInsets(8,8,8,8)
	editbox:SetBackdrop({
		bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		edgeSize = 16,
		insets = {left = 4, right = 4, top = 4, bottom = 4}
	})
	editbox:SetBackdropColor(.1,.1,.1,.3)
	editbox:SetBackdropBorderColor(.5,.5,.5)
	editbox:SetMultiLine(true)
	editbox:SetAutoFocus(false)
	editbox:ClearFocus()
	editbox:SetScript("OnEscapePressed", function(self)
		self:ClearFocus()
	end)

	local selected = DressToKill.profile.selected
	if selected then
		editbox:SetText(DressToKill.profile.weightFuncs[selected].handler or "")
	else
		editbox:SetText("-- Select an existing weight function from the above dropdown or create a new function.")
	end

	IndentationLib.enable(editbox)

	reset = CreateFrame("Button", "DressToKillResetButton", frame, "UIPanelButtonTemplate2")
	reset:SetText(L["Reset"])
	reset:SetWidth(80)
	reset:SetPoint("TOPRIGHT", editbox, "BOTTOMRIGHT", 0, -5)
	reset:SetScript("OnClick", function(self)
		local selected = DressToKill.profile.selected
		local handler = DressToKill.profile.weightFuncs[selected].handler
		editbox:SetText(handler)
		self:Disable()
	end)
	reset.counter = 0
	reset:SetScript("OnUpdate", function(self, elapsed)
		self.counter = self.counter + elapsed
		if self.counter > 0.2 then
			self.counter = 0
			-- Do check
			local selected = DressToKill.profile.selected
			if not selected then
				self:Disable()
				return
			end
			local source = DressToKill.profile.weightFuncs[selected].handler
			if editbox:GetText() ~= source then
				self:Enable()
			else
				self:Disable()
			end
		end
	end)

	save = CreateFrame("Button", "DressToKillSaveButton", frame, "UIPanelButtonTemplate2")
	save:SetText(L["Save"])
	save:SetWidth(80)
	save:SetPoint("RIGHT", reset, "LEFT", -3, 0)
	save:SetScript("OnClick", function(self)
		local selected = DressToKill.profile.selected
		local entry = DressToKill.profile.weightFuncs[selected]
		entry.handler = editbox:GetText()
		self:Disable()
	end)
	save.counter = 0
	save:SetScript("OnUpdate", function(self, elapsed)
		self.counter = self.counter + elapsed
		if self.counter > 0.2 then
			self.counter = 0
			-- Do check
			local selected = DressToKill.profile.selected
			if not selected then
				self:Disable()
				return
			end
			local source = DressToKill.profile.weightFuncs[selected].handler
			if editbox:GetText() ~= source then
				self:Enable()
			else
				self:Disable()
			end
		end
	end)

	delete = CreateFrame("Button", "DressToKillDeleteButton", frame, "UIPanelButtonTemplate2")
	delete:SetText(L["Delete"])
	delete:SetWidth(80)
	delete:SetPoint("RIGHT", save, "LEFT", -3, 0)
	delete:SetScript("OnClick", function(self)
		local selected = DressToKill.profile.selected
		DressToKill.profile.selected = nil
		DressToKill.profile.weightFuncs[selected] = nil
		UIDropDownMenu_Initialize(dropdown, initdropdown)
		UIDropDownMenu_SetSelectedValue(dropdown, nil)
		editbox:SetText("")
		self:Disable()
	end)

	local function OnAccept()
		local name = this:GetParent():GetName().."EditBox"
		local button = getglobal(name)
		local text = button:GetText()
		DressToKill.profile.selected = text
		DressToKill.profile.weightFuncs[text] = {
			handler = ""
		}
		UIDropDownMenu_Initialize(dropdown, initdropdown)
		UIDropDownMenu_SetSelectedValue(dropdown, text)
		editbox:SetText([[
--This function receives a single parameter
--'link', which is the link of the item
--being tested.  Example usage to max
--the player's health:
--
-- return UnitHealthMax("player")]])
	end

	-- Create a static popup
	StaticPopupDialogs["DRESSTOKILL_NEW_FUNCTION"] = {
		text = TEXT("Enter the name of the function you'd like to create"),
		button1 = TEXT(OKAY),
		button2 = TEXT(CANCEL),
		OnAccept = OnAccept,
		timeout = 0,
		whileDead = 1,
		exclusive = 1,
		showAlert = 1,
		hideOnEscape = 1,
		hasEditBox = 1,
		maxLetters = 32,
		OnShow = function()
			getglobal(this:GetName().."Button1"):Disable();
			getglobal(this:GetName().."EditBox"):SetFocus();
		end,
		OnHide = function()
			if ( ChatFrameEditBox:IsVisible() ) then
				ChatFrameEditBox:SetFocus();
			end
			getglobal(this:GetName().."EditBox"):SetText("");
		end,
		EditBoxOnEnterPressed = function()
			if ( getglobal(this:GetParent():GetName().."Button1"):IsEnabled() == 1 ) then
				OnAccept()
				this:GetParent():Hide()
			end
		end,
		EditBoxOnTextChanged = function ()
			local editBox = getglobal(this:GetParent():GetName().."EditBox");
			local txt = editBox:GetText()
			if #txt > 0 and not DressToKill.profile.weightFuncs[txt] then
				getglobal(this:GetParent():GetName().."Button1"):Enable();
			else
				getglobal(this:GetParent():GetName().."Button1"):Disable();
			end
		end,
		EditBoxOnEscapePressed = function()
			this:GetParent():Hide();
			ClearCursor();
		end
	}

	frame:SetScript("OnShow", nil)
end )

InterfaceOptions_AddCategory(frame)