local _G = _G
local parentMod, addOnOptionFrameName, optionNum = InvenSimulator, "InvenSimulatorOption", 0

if not parentMod then return end

local function cbOnClick(self)
	if self:GetChecked() then
		PlaySound("igMainMenuOptionCheckBoxOn")
		self.set(true)
	else
		PlaySound("igMainMenuOptionCheckBoxOff")
		self.set(false)
	end
end

local function cbOnEnter(self)
	if self.tooltipText then
		GameTooltip:ClearLines()
		GameTooltip:SetOwner(self, "ANCHOR_TOPLEFT")
		GameTooltip:AddLine(self.tooltipText, nil, nil, nil, true)
		GameTooltip:Show()
	end
end

local function cbOnLeave(self)
	GameTooltip:Hide()
end

local function btOnClick(self)
	if type(self.runFunc) == "function" then
		self.runFunc()
	elseif type(self.runFunc) == "string" and type(parentMod[self.runFunc]) == "function" then
		parentMod[self.runFunc](parentMod)
	end
end

local function cbOnEnter(self)
	if self.tooltipText then
		GameTooltip:ClearLines()
		GameTooltip:SetOwner(self, "ANCHOR_TOPLEFT")
		GameTooltip:AddLine(self.tooltipText, nil, nil, nil, true)
		GameTooltip:Show()
	end
end

local function sdOnValueChanged(self, v)
	if InCombatLockdown() then
		v = self.get()
		if self.unit == "%" then
			self.value:SetFormattedText("%d%%", v * 100)
		else
			self.value:SetFormattedText(self.renew..self.unit, v)
		end
		self:SetValue(v)
	else
		if self.unit == "%" then
			self.value:SetFormattedText("%d%%", v * 100)
		else
			self.value:SetFormattedText(self.renew..self.unit, v)
		end
		self.set(tonumber(self.renew:format(v)))
	end
end

local function sdOnShow(self)
	self:SetValue(self.get())
end

local function CreateUICheckButton(parentFrame, text, tooltipText, get, set, ...)
	optionNum = optionNum + 1
	name = addOnOptionFrameName..optionNum
	local frame = CreateFrame("CheckButton", name, parentFrame, "InterfaceOptionsCheckButtonTemplate")
	frame:SetID(optionNum)
	frame:SetPoint(...)
	frame.text = _G[name.."Text"]
	frame.text:SetText(text)
	frame.tooltipText = tooltipText
	frame.get = get
	frame.set = set
	frame:SetChecked(get())
	frame:SetScript("OnClick", cbOnClick)
	frame:SetScript("OnEnter", cbOnEnter)
	frame:SetScript("OnLeave", cbOnLeave)
	frame:SetScript("OnShow", cbOnShow)
	return frame
end

local function CreateUISlider(parentFrame, text, tooltipText, minv, maxv, step, unit, get, set, ...)
	optionNum = optionNum + 1
	name = addOnOptionFrameName..optionNum
	local frame = CreateFrame("Slider", name, parentFrame, "OptionsSliderTemplate")
	frame:SetID(optionNum)
	frame.text = _G[name.."Text"]
	frame.low = _G[name.."Low"]
	frame.high = _G[name.."High"]
	frame:SetPoint(...)
	frame:SetMinMaxValues(minv, maxv)
	frame:SetValueStep(step)
	frame:SetValue(get())
	frame.text:SetText(text)
	frame.low:SetText(minv)
	frame.high:SetText(maxv)
	frame.unit = unit or ""
	frame.step = step
	frame.value = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
	frame.value:SetPoint("TOP", frame, "BOTTOM")
	frame.value:SetText(get()..frame.unit)
	frame.tooltipText = tooltipText
	frame.get = get
	frame.set = set
	if unit == "%" then
		if step < 1 then
			frame.renew = "%.2f"
			frame.low:SetFormattedText("%d", minv * 100)
			frame.high:SetFormattedText("%d", maxv * 100)
			frame.value:SetFormattedText("%d%%", get() * 100)
		else
			frame.renew = "%d"
			frame.unit = "%%"
		end
	elseif floor(step) == step then
		frame.renew = "%d"
	elseif step >= 0.1 then
		frame.renew = "%.1f"
	else
		frame.renew = "%.2f"
	end
	frame:SetScript("OnValueChanged", sdOnValueChanged)
	frame:SetScript("OnShow", sdOnShow)
	return frame
end

function InvenSimulator:CreateOptionFrame()
	local f = CreateFrame("Frame", "InvenSimulatorOptionFrame", InterfaceOptionsFramePanelContainer)
	f.name = "인벤 시뮬레이터"
	f.title = f:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
	f.title:SetPoint("TOPLEFT", f, "TOPLEFT", 16, -16)
	f.title:SetText(f.name)
	f.subText = f:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
	f.subText:SetPoint("TOPLEFT", f.title, "TOPLEFT", 15, -40)
	f.subText:SetHeight(32)
	f.subText:SetJustifyH("LEFT")
	f.subText:SetJustifyV("TOP")
	f.subText:SetNonSpaceWrap(true)
	f.subText:SetPoint("TOPLEFT", f.title, "BOTTOMLEFT", 0, -8)
	f.subText:SetPoint("RIGHT", -32, 0)
	f.subText:SetText("가상의 캐릭터 창을 만들어 아이템 및 마법부여, 보석 등을 미리 세팅해 볼 수 있습니다.")

	f.mapbuttonShow = CreateUICheckButton(f, "미니맵 버튼 보이기", "미니맵 버튼을 보이거나 숨깁니다.",
		function() return not InvenSimulatorDB.mapbuttonHidden end,
		function(v)
			InvenSimulatorDB.mapbuttonHidden = not v
			InvenSimulator:HandleMapButton()
		end,
	"TOPLEFT", f.subText, "BOTTOMLEFT", -2, -8)

	f.mapbuttonLock = CreateUICheckButton(f, "미니맵 버튼 고정", "미니맵 버튼을 잠가 움직이지 못하게 합니다.",
		function() return InvenSimulatorDB.mapbuttonLock end,
		function(v)
			InvenSimulatorDB.mapbuttonLock = v
			InvenSimulator:HandleMapButton()
		end,
	"LEFT", f.mapbuttonShow, "LEFT", 160, 0)

	f.scale = CreateUISlider(f, "창 크기", "UI창의 크기를 조절합니다.", 0.5, 1.5, 0.01, "%",
		function() return InvenSimulatorDB.scale end,
		function(v)
			InvenSimulatorDB.scale = v
			if InvenSimulatorUI then
				InvenSimulatorUI:SetScale(v)
				InvenSimulatorUI.modelFrame:Hide()
				InvenSimulatorUI.modelFrame:SetUnit("player")
				InvenSimulatorUI.modelFrame:RefreshUnit()
				InvenSimulatorUI.modelFrame:Undress()
				InvenSimulatorUI.modelFrame:Show()
			end
		end,
	"TOPLEFT", f.mapbuttonShow, "BOTTOMLEFT", 0, -24)

	InterfaceOptions_AddCategory(f)
end