local IRF2 = InvenRaidFrames2

local _G = _G
local CopyTable = _G.CopyTable
local LBDB = LibStub("LibBlueDB-1.0")
local function getTable() return {} end

local L = IRF2.GetSpellLocale
local BLACK, WHITE, RED, GREEN, YELLOW = { 0, 0, 0 }, { 1, 1, 1 }, { 1, 0, 0 }, { 0, 1, 0 }, { 1, 1, 0 }

local default = {
	_ignoreDebuff = {
		[L( 6788)] = true, [L( 8326)] = true, [L(11196)] = true, [L(15822)] = true, [L(21163)] = true,
		[L(24360)] = true, [L(24755)] = true, [L(25771)] = true, [L(26004)] = true, [L(26013)] = true,
		[L(26680)] = true, [L(28169)] = true, [L(28504)] = true, [L(29232)] = true, [L(30108)] = true,
		[L(30451)] = true, [L(30529)] = true, [L(36893)] = true, [L(36900)] = true, [L(36901)] = true,
		[L(40880)] = true, [L(40882)] = true, [L(40883)] = true, [L(40891)] = true, [L(40896)] = true,
		[L(40897)] = true, [L(41292)] = true, [L(41337)] = true, [L(41350)] = true, [L(41425)] = true,
		[L(42164)] = true, [L(43681)] = true, [L(55711)] = true, [L(57723)] = true, [L(57724)] = true,
		[L(64805)] = true, [L(64808)] = true, [L(64809)] = true, [L(64810)] = true, [L(64811)] = true,
		[L(64812)] = true, [L(64813)] = true, [L(64814)] = true, [L(64815)] = true, [L(64816)] = true,
		[L(66233)] = true, [L(69127)] = true, [L(69438)] = true, [L(70402)] = true, [L(71328)] = true,
		[L(72144)] = true, [L(72145)] = true, [L(72369)] = true,
	},
	_userDebuffs = {
		[L(  605)] = true, [L( 8399)] = true, [L(11433)] = true, [L(12294)] = true, [L(17140)] = true,
		[L(29879)] = true, [L(30115)] = true, [L(30756)] = true, [L(31306)] = true, [L(31344)] = true,
		[L(31347)] = true, [L(31943)] = true, [L(31972)] = true, [L(32779)] = true, [L(37006)] = true,
		[L(37027)] = true, [L(37676)] = true, [L(38049)] = true, [L(38246)] = true, [L(39837)] = true,
		[L(40239)] = true, [L(40251)] = true, [L(40327)] = true, [L(40481)] = true, [L(40508)] = true,
		[L(40585)] = true, [L(40594)] = true, [L(40869)] = true, [L(40932)] = true, [L(41032)] = true,
		[L(41472)] = true, [L(41917)] = true, [L(42005)] = true, [L(42783)] = true, [L(43095)] = true,
		[L(43149)] = true, [L(43657)] = true, [L(44811)] = true, [L(44867)] = true, [L(45141)] = true,
		[L(45150)] = true, [L(45230)] = true, [L(45256)] = true, [L(45324)] = true, [L(45348)] = true,
		[L(45641)] = true, [L(45661)] = true, [L(45737)] = true, [L(45996)] = true, [L(46469)] = true,
		[L(46771)] = true, [L(51121)] = true, [L(55249)] = true, [L(55550)] = true, [L(56112)] = true,
		[L(57975)] = true, [L(58517)] = true, [L(59265)] = true, [L(59847)] = true, [L(61888)] = true,
		[L(61903)] = true, [L(61968)] = true, [L(61969)] = true, [L(62130)] = true, [L(62331)] = true,
		[L(62526)] = true, [L(62532)] = true, [L(62589)] = true, [L(62661)] = true, [L(62717)] = true,
		[L(63018)] = true, [L(63276)] = true, [L(63355)] = true, [L(63498)] = true, [L(63666)] = true,
		[L(63830)] = true, [L(64234)] = true, [L(64292)] = true, [L(64396)] = true, [L(64705)] = true,
		[L(64771)] = true, [L(65121)] = true, [L(65292)] = true, [L(65598)] = true, [L(66013)] = true,
		[L(66869)] = true, [L(67049)] = true, [L(67283)] = true, [L(67298)] = true, [L(67472)] = true,
		[L(67478)] = true, [L(67574)] = true, [L(67618)] = true, [L(68125)] = true, [L(69065)] = true,
		[L(69200)] = true, [L(69240)] = true, [L(69278)] = true, [L(69409)] = true, [L(69483)] = true,
		[L(69674)] = true, [L(70126)] = true, [L(70337)] = true, [L(70447)] = true, [L(70541)] = true,
		[L(70672)] = true, [L(70867)] = true, [L(71204)] = true, [L(71289)] = true, [L(71330)] = true,
		[L(71340)] = true, [L(72004)] = true, [L(72219)] = true, [L(72293)] = true, [L(72385)] = true,
		[L(72408)] = true, [L(72451)] = true, [L(72650)] = true,
	},
	strata = "MEDIUM",
	use = "항상", run = true, shown = "항상",
	anchor = "좌측 상단", limitMember = "40명",
	tooltip = "비전투 중에만", pos = { "CENTER", 0, 0 },
	updateTime = 0.1, lock = false, partyTag = true,
	partyTagColor = { BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, [0] = { 0.8, 0.36, 0.36 } },
	layout = {
		scale = 1, height = 30, width = 80,
		texture = "Smooth", offset = 1,
		healthOrientation = "가로", healthDisplayType = "남은 생명력",
		backdropColor = { 0, 0, 0, 0 }, healthColor = GREEN, offlineColor = { 0.25, 0.25, 0.25 },
		manaBar = 0.2, className = false, 	classHealth = true,
		nameRv = true, nameRvDeath = false, nameColor = WHITE,
		twoLine = false, lostHP = "항상", replaceLostHP = false,
		lostHPred = false, lostHPshort = false,
		outRangeAlpha = 0, outRangeAlphaMana = false, outRangeAlphaName = false,
		showAggro = true, showSurvivalSkill = true, lostHPColor = "ff0000",
	},
	font = {
		file = "기본 글꼴", size = 12, attribute = "", shadow = true,
	},
	heal = {
		use = true, barAlpha = 0.5, pos = "좌측 하단",
		showMyHeal = true, showIcon = true,
		myIcon = true, otherIcon = true,
		myHealColor = YELLOW, otherHealColor = GREEN,
	},
	groupOrder = {
		showParty = false, nl = 3, cl = 6, byClass = false, byName = false, point = "세로", dir = "가로", sortIndex = false,
		class = { "전사", "드루이드", "성기사", "죽음의 기사", "사제", "주술사", "도적", "마법사", "흑마법사", "사냥꾼" },
	},
	border = {
		show = true, backdrop = { 0.29, 0.35, 0.37, 0.52 }, border = { 0.58, 0.58, 0.58, 1 },
	},
	showEnemy = true, enemyColor = { 0.5, 0, 0 },
	debuffColor = {
		changeHealthColor = true, useHighlight = true, showBorder = true,
		Curse = { 0.6, 0, 1 }, Magic = WHITE, Disease = { 0.6, 0.4, 0 }, Poison = { 0, 0.25, 0 }, Normal = { 0.31, 0.64, 0.44 },
	},
	raidIcon = {
		use = true, pos = "좌측 상단", showTarget = false, size = 12, filter = { true, true, true, true, true, true, true, true },
	},
	outline = "디버프",
	outlineLowHealth = { value = 0.75, color = RED },
	outlineRaidIcon = { skip = { false, false, false, false, false, false, false, false }, color = WHITE },
	outlineAggroColor = RED, outlineTarget = WHITE, outlineMouseOver = YELLOW,
	showSurvivalSkill = true,
	debuff = {
		use = true, num = 5, pos = "우측 상단", size = 10, iconType = "아이콘 + 테두리",
	},
	userDebuff = {
		pos = "중앙", size = 18, alpha = 0.75, hideTimer = false,
	},
	buffCheck = {
		use = true, pos = "좌측", size = 12,
	},
	resurrection = {
		use = true, color = { 0, 0.75, 1 },
	},
	castingBar = {
		use = true, color = { 0.2, 0.8, 0.2 }, pos = "하단", height = 2,
	},
	pet = { scale = 1, column = 5, anchor = "좌측 상단", offset = 0, point = "세로", pos = { "CENTER", 0, 0 } },
	frameLevel = {
		castingBar = 5, resCastingBar = 6, buffCheck = 7, raidIcon = 8, debuff = 9, spellTimer = 10, healIcon = 11, name = 12, userDebuff = 13, readyCheck = 14
	},
	highlightFrameAlpha = 0.5,
	classColor = {
		PET = GREEN,
		VEHICLE = { 0, 0.4, 0 },
	},
	powerColor = {},
}
default._userDebuffs["*"] = nil
default._ignoreDebuff["*"] = nil

for class, color in pairs(RAID_CLASS_COLORS) do
	default.classColor[class] = { color.r, color.g, color.b }
end
for powerType, color in pairs(PowerBarColor) do
	if type(powerType) == "string" then
		default.powerColor[powerType] = { color.r, color.g, color.b }
	end
end

function IRF2:InitDB()
	if InvenRaidFrames2DB == nil or (InvenRaidFrames2CharDB and InvenRaidFrames2CharDB.class ~= self.playerClass) then
		InvenRaidFrames2CharDB = nil
	end
	if not InvenRaidFrames2DB or not InvenRaidFrames2DB.profileKeys then
		InvenRaidFrames2DB = { LBDB = true, version = self.numberVersion, profileKeys = {}, profiles = {} }
	end
	-- 기존 구버전 DB 컨버팅
	if not InvenRaidFrames2DB.LBDB then
		for name, db in pairs(InvenRaidFrames2DB.profiles) do
			db._ignoreDebuff = CopyTable(InvenRaidFrames2DB.ignoreDebuff)
			db._userDebuffs = CopyTable(InvenRaidFrames2DB.userDebuffs)
			db.classColor = CopyTable(InvenRaidFrames2DB.classColor)
			db.powerColor = CopyTable(InvenRaidFrames2DB.powerColor)
			if db.font.attribute == "그림자" then
				db.font.attribute, db.font.shadow = "", true
			elseif db.font.attribute == "외곽선" then
				db.font.attribute, db.font.shadow = "OUTLINE", false
			elseif db.font.attribute == "굵은 외곽선" then
				db.font.attribute, db.font.shadow = "THICKOUTLINE", false
			elseif db.font.attribute == "예리하게 + 그림자" then
				db.font.attribute, db.font.shadow = "MONOCHROME", true
			elseif db.font.attribute == "예리하게 + 외곽선" then
				db.font.attribute, db.font.shadow = "OUTLINE,MONOCHROME", false
			elseif db.font.attribute == "예리하게 + 굵은 외곽선" then
				db.font.attribute, db.font.shadow = "THICKOUTLINE,MONOCHROME", false
			else
				db.font.attribute, db.font.shadow = "", true
			end
			LBDB:UnregisterDB(db, default)
		end
		InvenRaidFrames2DB.ignoreDebuff, InvenRaidFrames2DB.userDebuffs = nil
		InvenRaidFrames2DB.classColor, InvenRaidFrames2DB.powerColor = nil
		for name, key in pairs(InvenRaidFrames2DB.profileKeys) do
			if key == "기본값" then
				InvenRaidFrames2DB.profileKeys[name] = nil
			end
		end
		InvenRaidFrames2DB.LBDB = true
	end
	-- 캐릭터 DB 업데이트
	self:InitCharDB()
	-- 최초 프로파일 적용
	InvenRaidFrames2DB.version = self.numberVersion
	self:SetProfile(InvenRaidFrames2DB.profileKeys[self.playerProfile])
	self.baseDB = InvenRaidFrames2DB
	self.charDB = InvenRaidFrames2CharDB
	self.defaultUserDebuffs = default._userDebuffs
	self.defaultIgnoreDebuff = default._ignoreDebuff
end

function IRF2:SetProfile(profile)
	profile = profile or "기본값"
	if type(profile) == "string" and profile ~= self.dbName then
		if self.dbName and InvenRaidFrames2DB.profiles[self.dbName] then
			LBDB:UnregisterDB(InvenRaidFrames2DB.profiles[self.dbName])
		end
		if profile == "기본값" then
			InvenRaidFrames2DB.profileKeys[self.playerProfile] = nil
		else
			InvenRaidFrames2DB.profileKeys[self.playerProfile] = profile
		end
		if not InvenRaidFrames2DB.profiles[profile] then
			InvenRaidFrames2DB.profiles[profile] = {}
		end
		self.db = LBDB:RegisterDB(InvenRaidFrames2DB.profiles[profile], default, getTable)
		self.dbName = profile
	end
end

function IRF2:InitCharDB()
	InvenRaidFrames2CharDB = InvenRaidFrames2CharDB or {
		class = self.playerClass,
		instanceCC = true,
		clickCasting = {}, clickCasting2 = {}, clickCastingMacro = {},
		debuff = {
			Curse = true, Magic = true, Disease = true, Poison = true, Normal = true,
		},
		buffCheck = { showInCombat = true, shownType = "버프가 없을 때 표시", enter = true, useField = true, usePVP = true, useInstance = true },
		usePet = false, aggroSound = "파티/공격대",
	}
	if not InvenRaidFrames2CharDB.dispel then
		InvenRaidFrames2CharDB.dispel = {
			Curse = (self.playerClass == "MAGE" or self.playerClass == "DRUID" or self.playerClass == "SHAMAN") and true or false,
			Magic = (self.playerClass == "PRIEST" or self.playerClass == "PALADIN") and true or false,
			Disease = (self.playerClass == "PRIEST" or self.playerClass == "PALADIN" or self.playerClass == "SHAMAN") and true or false,
			Poison = (self.playerClass == "PALADIN" or self.playerClass == "DRUID" or self.playerClass == "SHAMAN") and true or false,
			Normal = false,
		}
		if self.playerClass == "PRIEST" then
			InvenRaidFrames2CharDB.buffCheck.check = { true, true, false }		-- 인내, 정신, 암보
		elseif self.playerClass == "MAGE" then
			InvenRaidFrames2CharDB.buffCheck.check = { true, false, false }		-- 지능, 마증, 마감
		elseif self.playerClass == "WARLOCK" then
			InvenRaidFrames2CharDB.buffCheck.check = { true, false, false }		-- 영석, 투감, 영숨
			InvenRaidFrames2CharDB.buffCheck.shownType = "버프가 있을 때 표시"
		elseif self.playerClass == "DRUID" then
			InvenRaidFrames2CharDB.buffCheck.check = { true, false }		-- 발바닥, 가시
		elseif self.playerClass == "SHAMAN" then
			InvenRaidFrames2CharDB.buffCheck.check = { false, false }		-- 호흡, 걷기
			InvenRaidFrames2CharDB.buffCheck.shownType = "버프가 있을 때 표시"
		elseif self.playerClass == "PALADIN" then
			InvenRaidFrames2CharDB.buffCheck.check = true				-- 축복
		else
			InvenRaidFrames2CharDB.buffCheck = nil
		end
	end
	if self.GetSelfTimerData and self:GetSelfTimerData() then
		if InvenRaidFrames2CharDB.spellTimer == nil then
			InvenRaidFrames2CharDB.spellTimer = {
				enable = { true, true, true, true },
				pos = { "좌측 하단", "하단", "우측 하단", "우측 상단" },
				showSelf = CopyTable(self:GetSelfTimerData()),
				size = 1,
				showIcon = { true, true, true, true },
			}
		end
	else
		InvenRaidFrames2CharDB.spellTimer = nil
	end
	if InvenRaidFrames2CharDB.sound == nil then
		InvenRaidFrames2CharDB.sound = { aggroGain = "None", aggroLost = "None", debuffGain = "None" }
	end
	if InvenRaidFrames2CharDB.buffCheck and InvenRaidFrames2CharDB.buffCheck.enter == nil then
		InvenRaidFrames2CharDB.buffCheck.enter = true
		InvenRaidFrames2CharDB.buffCheck.useField = true
		InvenRaidFrames2CharDB.buffCheck.usePVP = true
		InvenRaidFrames2CharDB.buffCheck.useInstance = true
	end
	if InvenRaidFrames2CharDB.spellTimer then
		InvenRaidFrames2CharDB.spellTimer.size = InvenRaidFrames2CharDB.spellTimer.size or 1
		InvenRaidFrames2CharDB.spellTimer.showIcon = InvenRaidFrames2CharDB.spellTimer.showIcon or { true, true, true, true }
	end
	InvenRaidFrames2CharDB.aggroSound = InvenRaidFrames2CharDB.aggroSound or "사용 안함"
	if not InvenRaidFrames2CharDB.clickCasting2 then
		InvenRaidFrames2CharDB.clickCasting2 = CopyTable(InvenRaidFrames2CharDB.clickCasting)
	end
	if InvenRaidFrames2CharDB.instanceCC == nil then
		InvenRaidFrames2CharDB.instanceCC = true
		local cc, k = "clickCasting"
		for i = 1, 2 do
			if i > 1 then
				cc = cc..i
			end
			for p, v in pairs(InvenRaidFrames2CharDB[cc]) do
				k = tonumber(p:match("(%d+)"))
				if k == 1 then
					if v == "대상 선택" then
						InvenRaidFrames2CharDB[cc][p] = nil
					end
				elseif k == 2 then
					if v == "메뉴" then
						InvenRaidFrames2CharDB[cc][p] = nil
					end
				elseif v == "없음" then
					InvenRaidFrames2CharDB[cc][p] = nil
				end
			end
		end
	end
	InvenRaidFrames2CharDB.userSpellTimer = InvenRaidFrames2CharDB.userSpellTimer or {
		auraTypes = { "사용 안함", "사용 안함", "사용 안함", "사용 안함", "사용 안함" },
		auraNames = { "", "", "", "", "", "" },
		auraPos = { "좌측 상단", "좌측", "상단", "우측", "우측 상단" },
		icon = { true, true, true, true, true },
		scale = 1.0,
	}
	if #InvenRaidFrames2CharDB.userSpellTimer.auraTypes ~= 5 then
		for i = #InvenRaidFrames2CharDB.userSpellTimer.auraTypes + 1, 5 do
			InvenRaidFrames2CharDB.userSpellTimer.auraTypes[i] = "사용 안함"
			InvenRaidFrames2CharDB.userSpellTimer.auraNames[i] = ""
			InvenRaidFrames2CharDB.userSpellTimer.auraPos[i] = "상단"
			InvenRaidFrames2CharDB.userSpellTimer.icon[i] = true
		end
	end
end