local IRF2 = InvenRaidFrames2
local Option = IRF2.optionFrame
local LBO = LibStub("LibBlueOption-1.0")

local _G = _G
local pairs = _G.pairs
local twipe = _G.table.wipe
local CreateFrame = _G.CreateFrame

function Option:CreateLayerMenu(menu, parent)
	local frameLevelNames = { castingBar = "시전바", resCastingBar = "부활 시전바", buffCheck = "버프 확인 아이콘", raidIcon = "전술 목표 아이콘", debuff = "디버프 아이콘", healIcon = "들어오는 치유 아이콘", spellTimer = "주문 타이머", userSpellTimer = "사용자 주문 타이머", name = "이름", userDebuff = "중요 디버프", readyCheck = "전투 준비 확인 아이콘" }
	local levelOrder, levelIndex = {}
	local numFrame = 0
	for frame, level in pairs(IRF2.db.frameLevel) do
		numFrame = numFrame + 1
	end
	menu.list, menu.listUp, menu.listDown = {}, {}, {}
	local function frameLevelListUpdate()
		twipe(levelOrder)
		for frame, level in pairs(IRF2.db.frameLevel) do
			levelIndex = numFrame - level + 5
			levelOrder[levelIndex] = frame
			menu.list[levelIndex]:SetText(levelIndex..". "..frameLevelNames[frame])
		end
		for i = 1, numFrame do
			menu.listUp[i].value = levelOrder[i]
			menu.listDown[i].value = levelOrder[i]
			menu.listUp[i].value2 = levelOrder[i - 1]
			menu.listDown[i].value2 = levelOrder[i + 1]
		end
	end
	local function frameLevelUpdate()
		for _, object in pairs(IRF2.objects) do
			if object.textFrame then
				object.textFrame:SetFrameLevel(IRF2.db.frameLevel.name)
			end
			if object.buffFrame then
				object.buffFrame:SetFrameLevel(IRF2.db.frameLevel.buffCheck)
			end
			if object.castingBar then
				object.castingBar:SetFrameLevel(IRF2.db.frameLevel.castingBar)
			end
			if object.debuffFrame then
				object.debuffFrame:SetFrameLevel(IRF2.db.frameLevel.debuff)
			end
			if object.heal and object.heal.iconFrame then
				object.heal.iconFrame:SetFrameLevel(IRF2.db.frameLevel.healIcon)
			end
			if object.resCastingBar then
				object.resCastingBar:SetFrameLevel(IRF2.db.frameLevel.resCastingBar)
			end
			if object.spellTimer then
				for i = 1, 4 do
					if object.spellTimer[i] then
						object.spellTimer[i]:SetFrameLevel(IRF2.db.frameLevel.spellTimer)
					end
				end
			end
			if object.userSpellTimer then
				for i = 1, #IRF2CharDB.userSpellTimer.auraTypes do
					if object.userSpellTimer[i] then
						object.userSpellTimer[i]:SetFrameLevel(IRF2.db.frameLevel.spellTimer)
					end
				end
			end
			if object.userDebuff then
				object.userDebuff:SetFrameLevel(IRF2.db.frameLevel.userDebuff)
			end
			if object.readyCheck then
				object.readyCheck:SetFrameLevel(IRF2.db.frameLevel.readyCheck)
			end
			if object.raidIcon then
				object.raidIconFrame:SetFrameLevel(IRF2.db.frameLevel.raidIcon)
			end
		end
	end
	local function up_onenter(self)
		GameTooltip:SetOwner(self, "ANCHOR_LEFT")
		GameTooltip:AddLine("위로")
		GameTooltip:Show()
	end
	local function down_onenter(self)
		GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
		GameTooltip:AddLine("아래로")
		GameTooltip:Show()
	end
	local function up_down_onclick(self)
		self.swap = IRF2.db.frameLevel[self.value]
		IRF2.db.frameLevel[self.value] = IRF2.db.frameLevel[self.value2]
		IRF2.db.frameLevel[self.value2] = self.swap
		frameLevelListUpdate()
		frameLevelUpdate()
		self.swap = nil
	end
	for i = 1, numFrame do
		menu.list[i] = parent:CreateFontString("IRF2OptionFrameLevelOrder"..i, "OVERLAY", "GameFontNormal")
		menu.list[i]:SetJustifyH("LEFT")
		menu.list[i]:SetTextColor(1, 1, 1)
		menu.listUp[i] = CreateFrame("Button", nil, parent)
		menu.listUp[i]:SetWidth(26)
		menu.listUp[i]:SetHeight(26)
		menu.listUp[i]:SetNormalTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollUp-Up")
		menu.listUp[i]:SetPushedTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollUp-Down")
		menu.listUp[i]:SetDisabledTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollUp-Disabled")
		menu.listUp[i]:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Round", "ADD")
		menu.listUp[i]:SetScript("OnClick", up_down_onclick)
		menu.listUp[i]:SetScript("OnEnter", up_onenter)
		menu.listUp[i]:SetScript("OnLeave", GameTooltip_Hide)
		menu.listUp[i]:SetPoint("RIGHT", menu.list[i], "LEFT")
		menu.listDown[i] = CreateFrame("Button", nil, parent)
		menu.listDown[i]:SetWidth(26)
		menu.listDown[i]:SetHeight(26)
		menu.listDown[i]:SetNormalTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Up")
		menu.listDown[i]:SetPushedTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Down")
		menu.listDown[i]:SetDisabledTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Disabled")
		menu.listDown[i]:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Round", "ADD")
		menu.listDown[i]:SetPoint("LEFT", menu.list[i], "RIGHT", 2, 0)
		menu.listDown[i]:SetScript("OnClick", up_down_onclick)
		menu.listDown[i]:SetScript("OnEnter", down_onenter)
		menu.listDown[i]:SetScript("OnLeave", GameTooltip_Hide)
		menu.list[i]:SetPoint("TOPLEFT", menu.list[i - 1], "BOTTOMLEFT", 0, -20)
	end
	menu.list[1]:ClearAllPoints()
	menu.list[1]:SetPoint("TOPLEFT", 32, -16)
	menu.listUp[1]:Disable()
	menu.listDown[numFrame]:Disable()
	menu.reset = LBO:CreateWidget("Button", parent, "기본값 복원", "표시 우선 순위를 기본값으로 되돌립니다.", nil, nil, nil,
		function()
			IRF2.db.frameLevel.castingBar = 5
			IRF2.db.frameLevel.resCastingBar = 6
			IRF2.db.frameLevel.buffCheck = 7
			IRF2.db.frameLevel.raidIcon = 8
			IRF2.db.frameLevel.debuff = 9
			IRF2.db.frameLevel.spellTimer = 10
			IRF2.db.frameLevel.healIcon = 11
			IRF2.db.frameLevel.name = 12
			IRF2.db.frameLevel.userDebuff = 13
			IRF2.db.frameLevel.readyCheck = 14
			frameLevelListUpdate()
			frameLevelUpdate()
		end
	)
	menu.reset:SetWidth(100)
	menu.reset:SetPoint("TOPRIGHT", -5, 5)
	menu.listUp[1]:SetScript("OnShow", frameLevelListUpdate)
end