local _G = _G
local IRF3 = _G[...]
IRF3:RegisterEvent("VARIABLES_LOADED")

function IRF3:VARIABLES_LOADED()
	self:UnregisterEvent("VARIABLES_LOADED")
	self.VARIABLES_LOADED = nil
	-- 와우 기본 공격대 프레임 숨기기
	CompactRaidFrameManager_SetSetting("Locked", "1")
	CompactRaidFrameManager_SetSetting("IsShown", "0")
end

local btn, newPoint
local function preClick(self)
	newPoint = self:GetID() == 0
end
for i = 0, 6 do
	btn = CreateFrame("Button", "InvenRaidFrames3WorldMarker"..i, nil, "SecureActionButtonTemplate")
	btn:SetID(i)
	btn:SetScript("PreClick", preClick)
	btn:SetAttribute("*type*", "macro")
	btn:SetAttribute("*macrotext*", "/click CompactRaidFrameManagerDisplayFrameLeaderOptionsRaidWorldMarkerButton"..(i == 0 and "" or ("\n/click DropDownList1Button"..i)))
end
_G["BINDING_NAME_CLICK InvenRaidFrames3WorldMarker0:LeftButton"] = "위치 표시기 메뉴 열기"
_G["BINDING_NAME_CLICK InvenRaidFrames3WorldMarker1:LeftButton"] = "파란색 위치 표시기"
_G["BINDING_NAME_CLICK InvenRaidFrames3WorldMarker2:LeftButton"] = "초록색 위치 표시기"
_G["BINDING_NAME_CLICK InvenRaidFrames3WorldMarker3:LeftButton"] = "보라색 위치 표시기"
_G["BINDING_NAME_CLICK InvenRaidFrames3WorldMarker4:LeftButton"] = "빨간색 위치 표시기"
_G["BINDING_NAME_CLICK InvenRaidFrames3WorldMarker5:LeftButton"] = "흰색 위치 표시기"
_G["BINDING_NAME_CLICK InvenRaidFrames3WorldMarker6:LeftButton"] = "위치 표시기 모두 삭제"
LibStub("LibEnhanceDDMenu-1.0"):RegisterNewPoint(CompactRaidFrameManagerDisplayFrameLeaderOptionsRaidWorldMarkerButtonDropDown, function() return newPoint end, function() newPoint = nil end, "cursor")
CompactRaidFrameManagerDisplayFrameLeaderOptionsRaidWorldMarkerButton:SetScript("PostClick", function(self)
	if not IRF3:IsLeader() then
		CloseDropDownMenus(1)
	end
end)

CompactRaidFrameManager:SetFrameStrata("MEDIUM")
CompactRaidFrameManager:SetFrameLevel(10)
CompactRaidFrameManager:SetScale(0.95)
CompactRaidFrameManagerDisplayFrameFilterRoleTank.selectedHighlight:SetTexture("")
CompactRaidFrameManagerDisplayFrameFilterRoleTank:EnableMouse(nil)
CompactRaidFrameManagerDisplayFrameFilterRoleHealer.selectedHighlight:SetTexture("")
CompactRaidFrameManagerDisplayFrameFilterRoleHealer:EnableMouse(nil)
CompactRaidFrameManagerDisplayFrameFilterRoleDamager.selectedHighlight:SetTexture("")
CompactRaidFrameManagerDisplayFrameFilterRoleDamager:EnableMouse(nil)
CompactRaidFrameManagerDisplayFrameOptionsButton:SetScript("OnClick", function()
	IRF3:OnClick("LeftButton")
end)
local function updateOptionsFrame(preview)
	if IRF3.optionFrame:IsVisible() and LibStub("LibBlueOption-1.0", true) then
		LibStub("LibBlueOption-1.0"):Refresh()
	end
end

local manager = CreateFrame("Frame", nil, CompactRaidFrameManagerDisplayFrame)
IRF3.manager = manager
manager.locked = CreateFrame("Button", IRF3:GetName().."ManagerLockedButton", CompactRaidFrameManagerDisplayFrame, "UIMenuButtonStretchTemplate")
manager.locked:SetAllPoints(CompactRaidFrameManagerDisplayFrameLockedModeToggle)
manager.locked.Update = function(self)
	self:SetText(IRF3.db.lock and "이동하기" or "고정하기")
end
manager.locked:SetScript("OnClick", function(self)
	IRF3.db.lock = not IRF3.db.lock
	self:Update()
	updateOptionsFrame()
end)
CompactRaidFrameManagerDisplayFrameLockedModeToggle:Hide()
CompactRaidFrameManagerDisplayFrameLockedModeToggle:SetAlpha(0)
CompactRaidFrameManagerDisplayFrameLockedModeToggle:EnableMouse(nil)
manager.hidden = CreateFrame("Button", IRF3:GetName().."ManagerHiddenButton", CompactRaidFrameManagerDisplayFrame, "UIMenuButtonStretchTemplate")
manager.hidden:SetAllPoints(CompactRaidFrameManagerDisplayFrameHiddenModeToggle)
manager.hidden.Update = function(self)
	if InCombatLockdown() then
		self:Disable()
	else
		self:Enable()
	end
	self:SetText(IRF3.db.run and "숨기기" or "표시하기")
end
manager.hidden:SetScript("OnClick", function(self)
	IRF3.db.run = not IRF3.db.run
	IRF3:SetAttribute("run", IRF3.db.run)
	InvenRaidFrames3Overlord:GetScript("PostClick")(InvenRaidFrames3Overlord)
end)
CompactRaidFrameManagerDisplayFrameHiddenModeToggle:Hide()
CompactRaidFrameManagerDisplayFrameHiddenModeToggle:SetAlpha(0)
CompactRaidFrameManagerDisplayFrameHiddenModeToggle:EnableMouse(nil)

function manager:Update()
	self.locked:Update()
	self.hidden:Update()
	self:UpdateGroup()
end

manager.group = {}

local function groupOnClick(self)
	IRF3.db.groupshown[self:GetID()] = not IRF3.db.groupshown[self:GetID()]
	if IRF3.db.groupshown[self:GetID()] then
		self.selectedHighlight:Show()
	else
		self.selectedHighlight:Hide()
	end
	IRF3:UpdateGroupFilter()
end

local function groupSetPos(self)
	self:SetUserPlaced(nil)
	self:ClearAllPoints()
	self:SetUserPlaced(nil)
	self:SetPoint("CENTER", _G["CompactRaidFrameManagerDisplayFrameFilterGroup"..IRF3.db.grouporder[self:GetID()]], 0, 0)
	self:SetUserPlaced(nil)
end

local function groupOnDragStart(self)
	manager.group.drag = self
	self:SetFrameLevel(CompactRaidFrameManagerDisplayFrame:GetFrameLevel() + 2)
	self:SetUserPlaced(nil)
	self:StartMoving()
end

local function groupOnHide(self)
	if manager.group.drag == self then
		manager.group.drag = nil
		self:StopMovingOrSizing()
		groupSetPos(self)
		self:SetFrameLevel(CompactRaidFrameManagerDisplayFrame:GetFrameLevel() + 1)
		return true
	end
	return nil
end

local function groupOnDragStop(self)
	if groupOnHide(self) then
		self:GetScript("OnMouseUp")(self)
		for i = 1, 8 do
			if self:GetID() ~= i and manager.group[i]:IsMouseOver() then
				IRF3.db.grouporder[self:GetID()], IRF3.db.grouporder[i] = IRF3.db.grouporder[i], IRF3.db.grouporder[self:GetID()]
				groupSetPos(self)
				groupSetPos(manager.group[i])
				IRF3:UpdateGroupFilter()
				break
			end
		end
	end
end

for i = 1, 8 do
	_G["CompactRaidFrameManagerDisplayFrameFilterGroup"..i]:Hide()
	manager.group[i] = CreateFrame("Button", IRF3:GetName().."ManagerGroup"..i, CompactRaidFrameManagerDisplayFrame, "CRFManagerFilterGroupButtonTemplate")
	manager.group[i]:SetFrameLevel(CompactRaidFrameManagerDisplayFrame:GetFrameLevel() + 1)
	manager.group[i]:Hide()
	manager.group[i]:SetText(i)
	manager.group[i]:SetID(i)
	manager.group[i]:SetMovable(true)
	manager.group[i]:RegisterForDrag("LeftButton")
	manager.group[i]:SetScript("OnClick", groupOnClick)
	manager.group[i]:SetScript("OnDragStart", groupOnDragStart)
	manager.group[i]:SetScript("OnDragStop", groupOnDragStop)
	manager.group[i]:SetScript("OnHide", groupOnHide)
	manager.group[i]:SetClampedToScreen(true)
end

local function classOnClick(self)
	IRF3.db.classshown[self.class] = not IRF3.db.classshown[self.class]
	if IRF3.db.classshown[self.class] then
		self.selectedHighlight:Show()
	else
		self.selectedHighlight:Hide()
	end
	IRF3:UpdateGroupFilter()
end

local function lookupTable(tbl, value)
	for i = 1, #tbl do
		if tbl[i] == value then
			return i
		end
	end
	return nil
end

local function classSetPos(self, index)
	index = index or lookupTable(IRF3.db.classorder, self.class)
	self:SetUserPlaced(nil)
	self:ClearAllPoints()
	self:SetUserPlaced(nil)
	self:SetPoint("CENTER", manager.classAnchor[index], 0, 0)
	self:SetUserPlaced(nil)
end

local function classOnDragStart(self)
	manager.group.drag = self
	self:SetFrameLevel(CompactRaidFrameManagerDisplayFrame:GetFrameLevel() + 2)
	self:SetUserPlaced(nil)
	self:StartMoving()
end

local function classOnHide(self)
	if manager.group.drag == self then
		manager.group.drag = nil
		self:StopMovingOrSizing()
		classSetPos(self)
		self:SetFrameLevel(CompactRaidFrameManagerDisplayFrame:GetFrameLevel() + 1)
		return true
	end
	return nil
end

local function classOnDragStop(self)
	if classOnHide(self) then
		self:GetScript("OnMouseUp")(self)
		for i, class in ipairs(IRF3.db.classorder) do
			if self.class ~= class and manager.group[class]:IsMouseOver() then
				local index = lookupTable(IRF3.db.classorder, self.class)
				IRF3.db.classorder[index], IRF3.db.classorder[i] = IRF3.db.classorder[i], IRF3.db.classorder[index]
				classSetPos(self, i)
				classSetPos(manager.group[class], index)
				IRF3:UpdateGroupFilter()
				break
			end
		end
	end
end

manager.classAnchor = {}

for i, class in ipairs(IRF3.classes) do
	manager.group[class] = CreateFrame("Button", IRF3:GetName().."ManagerGroup"..class, CompactRaidFrameManagerDisplayFrame, "CRFManagerFilterGroupButtonTemplate")
	manager.group[class]:SetFrameLevel(CompactRaidFrameManagerDisplayFrame:GetFrameLevel() + 1)
	manager.group[class]:Hide()
	manager.group[class].class = class
	manager.group[class]:SetMovable(true)
	manager.group[class]:RegisterForDrag("LeftButton")
	manager.group[class]:SetSize(33, 24)
	manager.group[class].tex = manager.group[class]:CreateTexture(nil, "OVERLAY", 2)
	manager.group[class].tex:SetPoint("CENTER", 0, 0)
	manager.group[class].tex:SetSize(18, 18)
	manager.group[class].tex:SetTexture("Interface\\Glues\\CharacterCreate\\UI-CharacterCreate-Classes")
	manager.group[class].tex:SetTexCoord(CLASS_ICON_TCOORDS[class][1], CLASS_ICON_TCOORDS[class][2], CLASS_ICON_TCOORDS[class][3], CLASS_ICON_TCOORDS[class][4])
	manager.group[class].tex:SetAlpha(0.75)
	manager.group[class]:SetScript("OnClick", classOnClick)
	manager.group[class]:SetScript("OnDragStart", classOnDragStart)
	manager.group[class]:SetScript("OnDragStop", classOnDragStop)
	manager.group[class]:SetScript("OnHide", classOnHide)
	manager.group[class]:SetClampedToScreen(true)
	manager.classAnchor[i] = manager:CreateTexture()
	manager.classAnchor[i]:SetSize(33, 24)
	manager.classAnchor[i]:SetPoint("TOPLEFT", manager.classAnchor[i - 1], "TOPRIGHT", 2, 0)
end

manager.classAnchor[1]:SetPoint("TOPLEFT", CompactRaidFrameManagerDisplayFrameFilterGroup1, 0, 0)
manager.classAnchor[6]:SetPoint("TOPLEFT", CompactRaidFrameManagerDisplayFrameFilterGroup5, 0, 0)

function manager:UpdateGroup()
	if manager.group.drag then
		manager.group.drag:StopMovingOrSizing()
		manager.group.drag:SetUserPlaced(nil)
		manager.group.drag = nil
	end
	if IRF3.db.groupby == "GROUP" then
		if InCombatLockdown() or UnitAffectingCombat("player") then
			for i = 1, 8 do
				groupSetPos(manager.group[i])
				manager.group[i]:Disable()
				manager.group[i]:SetAlpha(0.5)
				if IRF3.db.groupshown[i] then
					manager.group[i].selectedHighlight:Show()
				else
					manager.group[i].selectedHighlight:Hide()
				end
				manager.group[i]:Show()
			end
		else
			for i = 1, 8 do
				groupSetPos(manager.group[i])
				manager.group[i]:Enable()
				manager.group[i]:SetAlpha(1)
				if IRF3.db.groupshown[i] then
					manager.group[i].selectedHighlight:Show()
				else
					manager.group[i].selectedHighlight:Hide()
				end
				manager.group[i]:Show()
			end
		end
		for _, class in ipairs(IRF3.classes) do
			manager.group[class]:Hide()
		end
	else
		if InCombatLockdown() or UnitAffectingCombat("player") then
			for i, class in ipairs(IRF3.db.classorder) do
				classSetPos(manager.group[class], i)
				manager.group[class]:Disable()
				manager.group[class]:SetAlpha(0.5)
				manager.group[class].tex:SetDesaturated(true)
				if IRF3.db.classshown[class] then
					manager.group[class].selectedHighlight:Show()
				else
					manager.group[class].selectedHighlight:Hide()
				end
				manager.group[class]:Show()
			end
		else
			for i, class in ipairs(IRF3.db.classorder) do
				classSetPos(manager.group[class], i)
				manager.group[class]:Enable()
				manager.group[class]:SetAlpha(1)
				manager.group[class].tex:SetDesaturated(nil)
				manager.group[class].selectedHighlight:SetDesaturated(nil)
				if IRF3.db.classshown[class] then
					manager.group[class].selectedHighlight:Show()
				else
					manager.group[class].selectedHighlight:Hide()
				end
				manager.group[class]:Show()
			end
		end
		for i = 1, 8 do
			groupSetPos(manager.group[i])
			manager.group[i]:Hide()
		end
	end
end

manager:SetScript("OnShow", manager.Update)
manager:SetScript("OnEvent", manager.Update)
manager:RegisterEvent("PLAYER_REGEN_ENABLED")
manager:RegisterEvent("PLAYER_REGEN_DISABLED")

function IRF3:ToggleManager()
	if self.db.useManager then
		CompactRaidFrameManager:EnableMouse(true)
		CompactRaidFrameManagerDisplayFrame:EnableMouse(true)
		CompactRaidFrameManager:EnableDrawLayer("BACKGROUND")
		CompactRaidFrameManager:EnableDrawLayer("BORDER")
		CompactRaidFrameManager:EnableDrawLayer("ARTWORK")
		CompactRaidFrameManager:EnableDrawLayer("OVERLAY")
		CompactRaidFrameManager:EnableDrawLayer("HIGHLIGHT")
		CompactRaidFrameManagerToggleButton:EnableDrawLayer("BACKGROUND")
		CompactRaidFrameManagerToggleButton:EnableDrawLayer("BORDER")
		CompactRaidFrameManagerToggleButton:EnableDrawLayer("ARTWORK")
		CompactRaidFrameManagerToggleButton:EnableDrawLayer("OVERLAY")
		CompactRaidFrameManagerToggleButton:EnableDrawLayer("HIGHLIGHT")
		CompactRaidFrameManagerToggleButton:EnableMouse(true)
	else
		if not CompactRaidFrameManager.collapsed and manager:IsShown() then
			CompactRaidFrameManager_Collapse(CompactRaidFrameManager)
		end
		CompactRaidFrameManager:EnableMouse(nil)
		CompactRaidFrameManagerDisplayFrame:EnableMouse(nil)
		CompactRaidFrameManager:DisableDrawLayer("BACKGROUND")
		CompactRaidFrameManager:DisableDrawLayer("BORDER")
		CompactRaidFrameManager:DisableDrawLayer("ARTWORK")
		CompactRaidFrameManager:DisableDrawLayer("OVERLAY")
		CompactRaidFrameManager:DisableDrawLayer("HIGHLIGHT")
		CompactRaidFrameManagerToggleButton:DisableDrawLayer("BACKGROUND")
		CompactRaidFrameManagerToggleButton:DisableDrawLayer("BORDER")
		CompactRaidFrameManagerToggleButton:DisableDrawLayer("ARTWORK")
		CompactRaidFrameManagerToggleButton:DisableDrawLayer("OVERLAY")
		CompactRaidFrameManagerToggleButton:DisableDrawLayer("HIGHLIGHT")
		CompactRaidFrameManagerToggleButton:EnableMouse(nil)
	end
end