local self = InvenRaidFrames
local clickHealFrames = {}
local ignore = {
	["지휘의 외침"] = 1, ["전투의 외침"] = 1, ["치타 무리의 상"] = 1, ["기원의 오라"] = 1, ["집중의 오라"] = 1,
	["화염 저항의 오라"] = 1, ["냉기 저항의 오라"] = 1, ["응보의 오라"] = 1, ["고결의 오라"] = 1, ["암흑 저항의 오라"] = 1,
	["격노의 토템"] = 1, ["천벌의 토템"] = 1, ["평온의 토템"] = 1, ["물의 보호막"] = 1,
}

local freeforall = {
	["나루의 축복"] = 1,
}
local soulStoneRank = { ["1 레벨"] = "최하급 영혼석", ["2 레벨"] = "하급 영혼석", ["3 레벨"] = "영혼석", ["4 레벨"] = "상급 영혼석", ["5 레벨"] = "최상급 영혼석", ["6 레벨"] = "일급 영혼석", ["7 레벨"] = "악마의 영혼석" }

local spell, maxSpell, id, c, name, rank, sshot, soulstone, insert, bt, button, modifier, types, value, addSpell

local _G = _G
local pairs = _G.pairs
local tinsert =_G.tinsert
local strfind = _G.strfind
local strmatch = _G.strmatch
local GetSpellName = _G.GetSpellName
local InCombatLockdown = _G.InCombatLockdown

self.clickhealValidate = { "대상 선택", "메뉴", "없음" }

local initFrames = {
	["PlayerFrame"] = false, ["PetFrame"]  = false, ["TargetFrame"]  = false, ["PartyMemberFrame#"]  = false, ["PartyMemberFrame#PetFrame"]  = false, ["TargetofTargetFrame"]  = false, ["FocusFrame"]  = false, ["TargetofFocusFrame"]  = false, ["RaidPullout#Button#ClearButton"] = false,
	["aUFplayer"]  = false, ["aUFpet"]  = false, ["aUFtarget"]  = false, ["aUFtargettarget"]  = false, ["aUFtargettargettarget"]  = false, ["aUFfocus"]  = false, ["aUFfocustarget"]  = false, ["aUFpartyUnitButton#"]  = false, ["aUFpartypetUnitButton#"] = false,
}

function InvenRaidFrames:ScanSpell()
	spell, maxSpell, id, c = { "대상 선택", "메뉴", "없음" }, {}, 0, self.vars.classDB
	addSpell = {}
	if c then
		c.clickheal = c.clickheal or {}
		while true do
			id = id + 1
			name, rank = GetSpellName(id, BOOKTYPE_SPELL)
			if name then
				if name == "영혼석 창조" then
					soulstone = rank
				elseif name == "신비한 사격" and rank == "6 레벨" then
					sshot = true
				elseif not(ignore[name]) and (c.clickheal[name] or c.spell[name] or c.buff[name] or freeforall[name]) then
					addSpell[name] = rank
				end
			else
				break
			end
		end
		for p in pairs(addSpell) do
			tinsert(spell, p.."()")
		end
		if self.vars.class == "사냥꾼" and sshot then
			tinsert(spell, "신비한 사격()")
		elseif self.vars.class == "흑마법사" then
			tinsert(spell, "마법 삼키기")
			if soulstone then
				tinsert(spell, "영혼석 부활")
				soulstone = soulStoneRank[soulstone]
			end
		end
	end
	for p, v in pairs(self.db.char.clickheal) do
		if strfind(v, "%((.+)%)") then
			self.db.char.clickheal[p] = gsub(v, "%((.+)%)", "()")
		end
	end
	for _, v in pairs(self.db.char.macro) do
		tinsert(spell, "[매크로] "..v)
	end
	self.clickhealValidate = spell
	if IRF_Options then
		IRF_Options:UpdateClickHealValidate()
	end
end

function InvenRaidFrames:SetClickHealValue(key, value)
	self.db.char.clickheal[key] = value
	self:SetAllClickHealFrames()
end

function InvenRaidFrames:InitClickHealFrame(frame)
	if frame and not clickHealFrames[frame] then
		frame:RegisterForClicks("AnyUp")
		clickHealFrames[frame] = true
	end
end

function InvenRaidFrames:SetAllClickHealFrames()
	for frame in pairs(clickHealFrames) do
		self:SetClickHealFrame(frame)
	end
end

local clickTypes = { "target", "menu", "macro", "item", "spell" }

function InvenRaidFrames:SetFrameAttribute(f, modifier, button, ftype, value)
	types = modifier.."%s"..button
	if InCombatLockdown() then
		self:RunLeaveCombat(f:GetName()..types:format("type"), f, "SetAttribute", types:format("type"), ftype)
		if value then
			self:RunLeaveCombat(f:GetName()..types:format(ftype), f, "SetAttribute", types:format(ftype), value)
		end
		for i = 3, #clickTypes do
			if clickTypes[i] ~= ftype then
				self:RunLeaveCombat(f:GetName()..types:format(clickTypes[i]), f, "SetAttribute", types:format(clickTypes[i]), nil)
			end
		end
	else
		f:SetAttribute(types:format("type"), ftype)
		if value then
			f:SetAttribute(types:format(ftype), value)
		end
		for i = 3, #clickTypes do
			if clickTypes[i] ~= ftype then
				f:SetAttribute(types:format(clickTypes[i]), nil)
			end
		end
	end
end

function InvenRaidFrames:SetClickHealFrame(f)
	if f then
		if not clickHealFrames[f] then
			f:RegisterForClicks("AnyUp")
			clickHealFrames[f] = true
		end
		for p, v in pairs(self.db.char.clickheal) do
			button = strmatch(p, "(%d+)")
			modifier = gsub(p, "BUTTON(%d+)", "")
			if v == "대상 선택" then
				self:SetFrameAttribute(f, modifier, button, "target")
			elseif v == "메뉴" then
				self:SetFrameAttribute(f, modifier, button, "menu")
			elseif strfind(v, "%[매크로%] ") then
				self:SetFrameAttribute(f, modifier, button, "macro", gsub(v, "%[매크로%] ", "").."")
			elseif v == "영혼석 부활" and soulstone then
				self:SetFrameAttribute(f, modifier, button, "item", soulstone)
			elseif v == "없음" then
				self:SetFrameAttribute(f, modifier, button)
			else
				self:SetFrameAttribute(f, modifier, button, "spell", v)
			end
		end
	end
end

function InvenRaidFrames:AddMacro(macro)
	if macro ~= "" then
		local add = true
		for i = 1, #(self.db.char.macro) do
			if self.db.char.macro[i] == macro then
				add = false
				break
			end
		end
		if add then
			tinsert(self.db.char.macro, macro)
			self:ScanSpell()
		end
	end
end

function InvenRaidFrames:DelMacro(macro)
	value = "[매크로] "..self.db.char.macro[macro]
	tremove(self.db.char.macro, macro)
	for p, v in pairs(self.db.char.clickheal) do
		if v == value then
			self.db.char.clickheal[p] = "없음"
		end
	end
	self:SetAllClickHealFrames()
	self:ScanSpell()
end