local _G = _G
local type = _G.type
local pairs = _G.pairs
local tinsert = _G.table.insert
local GetSpellInfo = _G.GetSpellInfo

local LDM = LibStub("LibDropMenu-2.0")
local iceCrownBuff, iceCrownBuffIcon
local iceCrownBuffTooltip = "생명력 %d%%만큼 증가\n공격력 %d%%만큼 증가"
local iceCrownBuffDefaultTooltip = "얼음왕관 성채 강화 버프"

local function iceCrownBuffFunc(index)
	LDM:CloseMenu(1)
	if index then
		InvenSimulatorUI:UpdateClassBuffText(iceCrownBuff, iceCrownBuffTooltip:format(index * 5, index * 5))
		InvenSimulatorUI:SetEnableBuff(iceCrownBuff, index)
	else
		InvenSimulatorUI:UpdateClassBuffText(iceCrownBuff, iceCrownBuffDefaultTooltip)
		InvenSimulatorUI:SetEnableBuff(iceCrownBuff, nil)
	end
	InvenSimulatorUI:Calculate()
end

local iceCrownBuffMenu = { type = "group", args = {
	none = { name = "효과 제거", desc = " 효과를 제거합니다.", type = "execute", order = 99, func = iceCrownBuffFunc },
}}

function InvenSimulatorUI:InitExtraBuff(buffList, buffTooltipText, buffIcon, isClassBuff, buffStatusFunc)
	if UnitFactionGroup("player"):sub(1,1) == "H" then
		iceCrownBuff, _, iceCrownBuffIcon = GetSpellInfo(73816)
	else
		iceCrownBuff, _, iceCrownBuffIcon = GetSpellInfo(73762)
	end
	tinsert(buffList, iceCrownBuff)
	buffIcon[iceCrownBuff] = iceCrownBuffIcon
	isClassBuff[iceCrownBuff] = true
	buffStatusFunc[#buffList] = self.CheckIceCrownBuff
	buffTooltipText[iceCrownBuff] = iceCrownBuffDefaultTooltip

	for i = 1, 6 do
		iceCrownBuffMenu.args[i] = {
			name = (i * 5).."%",
			desc = (iceCrownBuff.."\n|cffffffff"..iceCrownBuffTooltip.."|r"):format(i * 5, i * 5),
			type = "execute",
			order = 7 - i,
			arg1 = i,
			func = iceCrownBuffFunc,
		}
	end
	iceCrownBuffMenu.args.none.desc = iceCrownBuff..iceCrownBuffMenu.args.none.desc
	LDM:RegisterMenu("InvenSimulatorIceCrownBuffMenu", iceCrownBuffMenu)
end

function InvenSimulatorUI:CheckIceCrownBuff()
	if type(InvenSimulatorCharDB.buffs[self.name]) == "number" and iceCrownBuffMenu.args[InvenSimulatorCharDB.buffs[self.name]] then
		InvenSimulatorUI:UpdateClassBuffText(self.name, iceCrownBuffTooltip:format(InvenSimulatorCharDB.buffs[self.name] * 5, InvenSimulatorCharDB.buffs[self.name] * 5))
	else
		InvenSimulatorCharDB.buffs[self.name] = nil
		InvenSimulatorUI:UpdateClassBuffText(self.name, iceCrownBuffDefaultTooltip)
	end
	return true
end

function InvenSimulatorUI:OpenIceCrownBuffMenu(button)
	if LDM:IsOpen("InvenSimulatorIceCrownBuffMenu") then
		LDM:CloseMenu(1)
	else
		for p, v in pairs(iceCrownBuffMenu.args) do
			v.icon = InvenSimulatorCharDB.buffs[iceCrownBuff] == p and "Interface\\Buttons\\UI-CheckBox-Check" or nil
		end
		LDM:OpenMenu("InvenSimulatorIceCrownBuffMenu", "TOPLEFT", button, "TOPRIGHT", nil, nil, nil, true)
	end
end