local IRF2 = InvenRaidFrames2
local _G = _G
local mod = _G.mod

local w, h, o, n, t, a_point, a_xoffset, a_yoffset, a_columnAnchorPoint

local function setGroupPosByParty(group, index, realindex)
	w, h, o, n = IRF2.db.layout.width, IRF2.db.layout.height, IRF2.db.layout.offset, IRF2.db.groupOrder.nl
	group:Hide()
	group:ClearAllPoints()
	group:PreCreateChildren(5)
	if IRF2.db.groupOrder.point == "세로" then
		t = IRF2.db.partyTag and 12 or 0
		if IRF2.db.anchor == "좌측 상단" then
			a_point, a_xoffset, a_yoffset = "TOP", o, -o
			if index == 1 then
				group:SetPoint("TOPLEFT", IRF2.anchor, "TOPLEFT")
			elseif IRF2.db.groupOrder.dir == "가로" then
				if n == 1 or mod(index, n) == 1 then
					group:SetPoint("TOPLEFT", group:GetNextGroup(-n), "TOPLEFT", 0, -((h + o) * 5 + t))
				else
					group:SetPoint("TOPLEFT", group:GetNextGroup(-1), "TOPRIGHT", o, 0)
				end
			elseif n == 1 or mod(index, n) == 1 then
				group:SetPoint("TOPLEFT", group:GetNextGroup(-n), "TOPRIGHT", o, 0)
			else
				group:SetPoint("TOPLEFT", group:GetNextGroup(-1), "TOPLEFT", 0, -((h + o) * 5 + t))
			end
		elseif IRF2.db.anchor == "우측 상단" then
			a_point, a_xoffset, a_yoffset = "TOP", o, -o
			if index == 1 then
				group:SetPoint("TOPRIGHT", IRF2.anchor, "TOPRIGHT")
			elseif IRF2.db.groupOrder.dir == "가로" then
				if n == 1 or mod(index, IRF2.db.groupOrder.nl) == 1 then
					group:SetPoint("TOPLEFT", group:GetNextGroup(-n), "TOPLEFT", 0, -((h + o) * 5 + t))
				else
					group:SetPoint("TOPRIGHT", group:GetNextGroup(-1), "TOPLEFT", -o, 0)
				end
			elseif n == 1 or mod(index, IRF2.db.groupOrder.nl) == 1 then
				group:SetPoint("TOPRIGHT", group:GetNextGroup(-n), "TOPLEFT", -o, 0)
			else
				group:SetPoint("TOPLEFT", group:GetNextGroup(-1), "TOPLEFT", 0, -((h + o) * 5 + t))
			end
		elseif IRF2.db.anchor == "좌측 하단" then
			a_point, a_xoffset, a_yoffset = "BOTTOM", o, o
			if index == 1 then
				group:SetPoint("BOTTOMLEFT", IRF2.anchor, "BOTTOMLEFT")
			elseif IRF2.db.groupOrder.dir == "가로" then
				if n == 1 or mod(index, n) == 1 then
					group:SetPoint("BOTTOMLEFT", group:GetNextGroup(-n), "BOTTOMLEFT", 0, (h + o) * 5 + t)
				else
					group:SetPoint("BOTTOMLEFT", group:GetNextGroup(-1), "BOTTOMRIGHT", o, 0)
				end
			elseif n == 1 or mod(index, n) == 1 then
				group:SetPoint("BOTTOMLEFT", group:GetNextGroup(-n), "BOTTOMRIGHT", o, 0)
			else
				group:SetPoint("BOTTOMLEFT", group:GetNextGroup(-1), "BOTTOMLEFT", 0, (h + o) * 5 + t)
			end
		elseif IRF2.db.anchor == "우측 하단" then
			a_point, a_xoffset, a_yoffset = "BOTTOM", o, o
			if index == 1 then
				group:SetPoint("BOTTOMRIGHT", IRF2.anchor, "BOTTOMRIGHT")
			elseif IRF2.db.groupOrder.dir == "가로" then
				if n == 1 or mod(index, IRF2.db.groupOrder.nl) == 1 then
					group:SetPoint("BOTTOMLEFT", group:GetNextGroup(-n), "BOTTOMLEFT", 0, (h + o) * 5 + t)
				else
					group:SetPoint("BOTTOMRIGHT", group:GetNextGroup(-1), "BOTTOMLEFT", -o, 0)
				end
			elseif n == 1 or mod(index, IRF2.db.groupOrder.nl) == 1 then
				group:SetPoint("BOTTOMRIGHT", group:GetNextGroup(-n), "BOTTOMLEFT", -o, 0)
			else
				group:SetPoint("BOTTOMLEFT", group:GetNextGroup(-1), "BOTTOMLEFT", 0, (h + o) * 5 + t)
			end
		end
	else
		t = IRF2.db.partyTag and 12 or 0
		if IRF2.db.anchor == "좌측 상단" then
			a_point, a_xoffset, a_yoffset = "LEFT", o, -o
			if index == 1 then
				group:SetPoint("TOPLEFT", IRF2.anchor, "TOPLEFT")
			elseif IRF2.db.groupOrder.dir == "가로" then
				if n == 1 or mod(index, n) == 1 then
					group:SetPoint("TOPLEFT", group:GetNextGroup(-n), "TOPLEFT", 0, -(h + o))
				else
					group:SetPoint("TOPLEFT", group:GetNextGroup(-1), "TOPLEFT", (w + o) * 5 + t, 0)
				end
			elseif n == 1 or mod(index, n) == 1 then
				group:SetPoint("TOPLEFT", group:GetNextGroup(-n), "TOPLEFT", (w + o) * 5 + t, 0)
			else
				group:SetPoint("TOPLEFT", group:GetNextGroup(-1), "TOPLEFT", 0, -(h + o))
			end
		elseif IRF2.db.anchor == "우측 상단" then
			a_point, a_xoffset, a_yoffset = "RIGHT", -o, -o
			group:SetManyAttributes("point", "RIGHT", "xOffset", -o, "yOffset", -o)
			if index == 1 then
				group:SetPoint("TOPRIGHT", IRF2.anchor, "TOPRIGHT")
			elseif IRF2.db.groupOrder.dir == "가로" then
				if n == 1 or mod(index, n) == 1 then
					group:SetPoint("TOPRIGHT", group:GetNextGroup(-n), "TOPRIGHT", 0, -(h + o))
				else
					group:SetPoint("TOPRIGHT", group:GetNextGroup(-1), "TOPRIGHT", -((w + o) * 5 + t), 0)
				end
			elseif n == 1 or mod(index, n) == 1 then
				group:SetPoint("TOPRIGHT", group:GetNextGroup(-n), "TOPRIGHT", -((w + o) * 5 + t), 0)
			else
				group:SetPoint("TOPRIGHT", group:GetNextGroup(-1), "TOPRIGHT", 0, -(h + o))
			end
		elseif IRF2.db.anchor == "좌측 하단" then
			a_point, a_xoffset, a_yoffset = "LEFT", o, o
			if index == 1 then
				group:SetPoint("BOTTOMLEFT", IRF2.anchor, "BOTTOMLEFT")
			elseif IRF2.db.groupOrder.dir == "가로" then
				if n == 1 or mod(index, n) == 1 then
					group:SetPoint("BOTTOMLEFT", group:GetNextGroup(-n), "BOTTOMLEFT", 0, h + o)
				else
					group:SetPoint("BOTTOMLEFT", group:GetNextGroup(-1), "BOTTOMLEFT", (w + o) * 5 + t, 0)
				end
			elseif n == 1 or mod(index, n) == 1 then
				group:SetPoint("BOTTOMLEFT", group:GetNextGroup(-n), "BOTTOMLEFT", (w + o) * 5 + t, 0)
			else
				group:SetPoint("BOTTOMLEFT", group:GetNextGroup(-1), "BOTTOMLEFT", 0, h + o)
			end
		elseif IRF2.db.anchor == "우측 하단" then
			a_point, a_xoffset, a_yoffset = "RIGHT", -o, o
			if index == 1 then
				group:SetPoint("BOTTOMRIGHT", IRF2.anchor, "BOTTOMRIGHT")
			elseif IRF2.db.groupOrder.dir == "가로" then
				if n == 1 or mod(index, n) == 1 then
					group:SetPoint("BOTTOMRIGHT", group:GetNextGroup(-n), "BOTTOMRIGHT", 0, h + o)
				else
					group:SetPoint("BOTTOMRIGHT", group:GetNextGroup(-1), "BOTTOMRIGHT", -((w + o) * 5 + t), 0)
				end
			elseif n == 1 or mod(index, n) == 1 then
				group:SetPoint("BOTTOMRIGHT", group:GetNextGroup(-n), "BOTTOMRIGHT", -((w + o) * 5 + t), 0)
			else
				group:SetPoint("BOTTOMRIGHT", group:GetNextGroup(-1), "BOTTOMRIGHT", 0, h + o)
			end
		end
	end
	if realindex > 0 then
		group:SetManyAttributes("columnAnchorPoint", false, "maxColumns", false, "unitsPerColumn", false, "point", a_point, "xOffset", a_xoffset, "yOffset", a_yoffset, "groupFilter", ("%d"):format(realindex), "groupingOrder", ("%d"):format(realindex), "groupBy", "GROUP", "sortMethod", IRF2.db.groupOrder.sortIndex and "INDEX" or "NAME")
	else
		group:SetManyAttributes("point", a_point, "xOffset", a_xoffset, "yOffset", a_yoffset)
	end
	if IRF2.db.run then
		group:Show()
	else
		group:Hide()
	end
end

local function setGroupPosByClass(group, index, realindex)
	w, h, o, n, t = IRF2.db.layout.width, IRF2.db.layout.height, IRF2.db.layout.offset, IRF2.db.groupOrder.cl, 0
	group:Hide()
	group:ClearAllPoints()
	if realindex > 1 then
		group:SetManyAttributes("groupFilter", "9", "groupingOrder", "9", "groupBy", "GROUP")
		return
	end
	if IRF2.db.groupOrder.point == "세로" then
		if IRF2.db.anchor == "좌측 상단" then
			a_columnAnchorPoint, a_point, a_xoffset, a_yoffset = "LEFT", "TOP", o, -o
			if index == 1 then
				group:SetPoint("TOPLEFT", IRF2.anchor, "TOPLEFT")
			else
				group:SetPoint("TOPLEFT", group:GetNextGroup(-1), "TOPRIGHT", o, 0)
			end
		elseif IRF2.db.anchor == "우측 상단" then
			a_columnAnchorPoint, a_point, a_xoffset, a_yoffset = "RIGHT", "TOP", o, -o
			if index == 1 then
				group:SetPoint("TOPRIGHT", IRF2.anchor, "TOPRIGHT")
			else
				group:SetPoint("TOPRIGHT", group:GetNextGroup(-1), "TOPLEFT", -o, 0)
			end
		elseif IRF2.db.anchor == "좌측 하단" then
			a_columnAnchorPoint, a_point, a_xoffset, a_yoffset = "LEFT", "BOTTOM", o, o
			if index == 1 then
				group:SetPoint("BOTTOMLEFT", IRF2.anchor, "BOTTOMLEFT")
			else
				group:SetPoint("BOTTOMLEFT", group:GetNextGroup(-1), "BOTTOMRIGHT", o, 0)
			end
		elseif IRF2.db.anchor == "우측 하단" then
			a_columnAnchorPoint, a_point, a_xoffset, a_yoffset = "RIGHT", "BOTTOM", o, o
			if index == 1 then
				group:SetPoint("BOTTOMRIGHT", IRF2.anchor, "BOTTOMRIGHT")
			else
				group:SetPoint("BOTTOMRIGHT", group:GetNextGroup(-1), "BOTTOMLEFT", -o, 0)
			end
		end
	else
		if IRF2.db.anchor == "좌측 상단" then
			a_columnAnchorPoint, a_point, a_xoffset, a_yoffset = "TOP", "LEFT", o, -o
			if index == 1 then
				group:SetPoint("TOPLEFT", IRF2.anchor, "TOPLEFT")
			else
				group:SetPoint("TOPLEFT", group:GetNextGroup(-1), "TOPLEFT", 0, -(h + o))
			end
		elseif IRF2.db.anchor == "우측 상단" then
			a_columnAnchorPoint, a_point, a_xoffset, a_yoffset = "TOP", "RIGHT", -o, -o
			if index == 1 then
				group:SetPoint("TOPRIGHT", IRF2.anchor, "TOPRIGHT")
			else
				group:SetPoint("TOPRIGHT", group:GetNextGroup(-1), "TOPRIGHT", 0, -(h + o))
			end
		elseif IRF2.db.anchor == "좌측 하단" then
			a_columnAnchorPoint, a_point, a_xoffset, a_yoffset = "BOTTOM", "LEFT", o, o
			if index == 1 then
				group:SetPoint("BOTTOMLEFT", IRF2.anchor, "BOTTOMLEFT")
			else
				group:SetPoint("BOTTOMLEFT", group:GetNextGroup(-1), "BOTTOMLEFT", 0, h + o)
			end
		elseif IRF2.db.anchor == "우측 하단" then
			a_columnAnchorPoint, a_point, a_xoffset, a_yoffset = "BOTTOM", "RIGHT", -o, o
			if index == 1 then
				group:SetPoint("BOTTOMRIGHT", IRF2.anchor, "BOTTOMRIGHT")
			else
				group:SetPoint("BOTTOMRIGHT", group:GetNextGroup(-1), "BOTTOMRIGHT", 0, h + o)
			end
		end
	end
	if realindex == 1 then
		group:PreCreateChildren(40)
		if IRF2.db.groupOrder.byClass then
			group:SetManyAttributes("point", a_point, "xOffset", a_xoffset, "yOffset", a_yoffset, "columnAnchorPoint", a_columnAnchorPoint, "maxColumns", 25, "unitsPerColumn", n, "groupFilter", IRF2:GetGroupOrder(), "groupingOrder", IRF2:GetGroupOrder(), "groupBy", "CLASS", "sortMethod", "NAME")
		else
			group:SetManyAttributes("point", a_point, "xOffset", a_xoffset, "yOffset", a_yoffset, "columnAnchorPoint", a_columnAnchorPoint, "maxColumns", 25, "unitsPerColumn", n, "groupFilter", "1,2,3,4,5,6,7,8", "groupingOrder", nil, "groupBy", nil, "sortMethod", "NAME")
		end
	elseif realindex == 0 then
		group:SetManyAttributes("point", a_point, "xOffset", a_xoffset, "yOffset", a_yoffset)
	end
	if IRF2.db.run then
		group:Show()
	else
		group:Hide()
	end
	if not (IRF2.db.groupOrder.byClass or IRF2.db.groupOrder.byName) and IRF2.db.run and IRF2.db.partyTag then
		if IRF2.db.groupOrder.point == "세로" then
			group.partyTag:Show()
			group.partyTag1:Hide()
		else
			group.partyTag:Hide()
			group.partyTag1:Show()
		end
	else
		group.partyTag:Hide()
		group.partyTag1:Hide()
	end
end

local limitMembers = { ["40명"] = 8, ["25명"] = 5, ["20명"] = 4, ["15명"] = 3, ["10명"] = 2, ["5명"] = 1 }

local index, firstGroup, lastGroup

function IRF2:SetGroupPosition()
	index, firstGroup = 0, self:GetPartyHeaderShown() and 0 or 1
	lastGroup = (self.db.groupOrder.byClass or self.db.groupOrder.byName) and 8 or (limitMembers[self.db.limitMember] or 8)
	for i = 0, 8 do
		if i >= firstGroup and i <= lastGroup then
			index = index + 1
			if self.db.groupOrder.byClass or self.db.groupOrder.byName then
				setGroupPosByClass(self.group[i], index, i)
			else
				setGroupPosByParty(self.group[i], index, i)
			end
		else
			self.group[i]:Hide()
		end
	end
	if self.initPEW then
		self:UpdateBorder()
	end
end