local IRF2 = InvenRaidFrames2

local _G = _G
local min = _G.math.min
local max = _G.math.max
local abs = _G.math.abs
local ceil = _G.math.ceil
local unpack = _G.unpack

local s, e
local offset = 5
local startPoint = { ["좌측 상단"] = { "TOPLEFT", -offset, offset }, ["우측 상단"] = { "TOPRIGHT", offset, offset }, ["좌측 하단"] = { "BOTTOMLEFT", -offset, -offset }, ["우측 하단"] = { "BOTTOMRIGHT", offset, -offset } }
local widthPoint = { ["좌측 상단"] = "GetLeft", ["우측 상단"] = "GetRight", ["좌측 하단"] = "GetLeft", ["우측 하단"] = "GetRight" }
local heightPoint = { ["좌측 상단"] = "GetTop", ["우측 상단"] = "GetTop", ["좌측 하단"] = "GetBottom", ["우측 하단"] = "GetBottom" }
local rWidthPoint = { ["좌측 상단"] = "GetRight", ["우측 상단"] = "GetLeft", ["좌측 하단"] = "GetRight", ["우측 하단"] = "GetLeft" }
local rHeightPoint = { ["좌측 상단"] = "GetBottom", ["우측 상단"] = "GetBottom", ["좌측 하단"] = "GetTop", ["우측 하단"] = "GetTop" }
local minMaxFunction = { ["좌측 상단"] = { max, min }, ["우측 상단"] = { min, min }, ["좌측 하단"] = { max, max }, ["우측 하단"] = { min, max } }

local function CreateBorder()
	IRF2.borderFrame = CreateFrame("Frame", nil, UIParent)
	IRF2.borderFrame:SetFrameStrata(IRF2.db.strata)
	IRF2.borderFrame:SetFrameLevel(1)
	IRF2.borderFrame:SetBackdrop({
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Addons\\InvenRaidFrames2\\Texture\\Border.tga",
		tile = true, tileSize = 16, edgeSize = 16,
		insets = { left = 2, right = 2, top = 2, bottom = 2 }
	})

	IRF2.borderFrame:SetBackdropColor(unpack(IRF2.db.border.backdrop))
	IRF2.borderFrame:SetBackdropBorderColor(unpack(IRF2.db.border.border))
	IRF2.borderFrame:SetScale(IRF2.db.layout.scale)
	IRF2.borderFrame:EnableMouse(true)
	IRF2.borderFrame:RegisterForDrag("LeftButton")
	IRF2.borderFrame:SetScript("OnDragStart", IRF2.anchor:GetScript("OnDragStart"))
	IRF2.borderFrame:SetScript("OnDragStop", IRF2.anchor:GetScript("OnDragStop"))
end

local function GetFirstGroup()
	return IRF2:GetPartyHeaderShown() and 0 or 1
end

local function GetLastGroup()
	s = GetFirstGroup()
	e = #self.group
	for i = e, s, -1 do
		if IRF2:GetGroupMembers(i) > 0 then
			return i
		end
	end
end

local firstGroup, lastGroup, width, height, frame, wp, hp, tagoffsetx, tagoffsety, tabw, tabh, groupMember, checkShown

function IRF2:UpdateBorder()
	checkShown = nil
	for group, header in pairs(self.group) do
		if header:IsShown() and self:GetGroupMembers(group) > 0 then
			checkShown = true
			break
		end
	end
	self:HandleShownStatus(checkShown)
	if not self.borderFrame then
		CreateBorder()
	end
	self:UpdatePartyTag()
	if self.db.border.show then
		self.borderFrame:SetAlpha(1)
	else
		self.borderFrame:SetAlpha(0)
	end
	if not self:RaidFrameIsShown() or not self.db.run then
		self.borderFrame:Hide()
		return
	end
	if self.db.partyTag and not (self.db.groupOrder.byClass or self.db.groupOrder.byName) then
		if self.db.groupOrder.point == "가로" then
			tagoffsetx, tagoffsety = (startPoint[self.db.anchor][2] / offset) * 12, 0
			tabw, tabh = 12, 0
		else
			tagoffsetx, tagoffsety = 0, (startPoint[self.db.anchor][3] / offset) * 12
			tabw, tabh = 0, 12
		end
	else
		tagoffsetx, tagoffsety, tabw, tabh = 0, 0, 0, 0
	end
	self.borderFrame:ClearAllPoints()
	self.borderFrame:SetPoint(startPoint[self.db.anchor][1], self.anchor, startPoint[self.db.anchor][1], startPoint[self.db.anchor][2] + tagoffsetx, startPoint[self.db.anchor][3] + tagoffsety)
	if (self.db.groupOrder.byClass or self.db.groupOrder.byName) and self.raidMembers > 0 then
		if self.db.groupOrder.point == "가로" then
			wp = min(self.db.groupOrder.cl, self.raidMembers)
			hp = ceil(self.raidMembers / self.db.groupOrder.cl)
			if GetFirstGroup() == 0 then
				wp = max(wp, self.partyMembers + 1)
				hp = hp + 1
			end
		else
			wp = ceil(self.raidMembers / self.db.groupOrder.cl)
			hp = min(self.db.groupOrder.cl, self.raidMembers)
			if GetFirstGroup() == 0 then
				hp = max(hp, self.partyMembers + 1)
				wp = wp + 1
			end
		end
		width = wp * (self.db.layout.width + self.db.layout.offset) + offset * 2
		height = hp * (self.db.layout.height + self.db.layout.offset) + offset * 2
	else
		firstGroup, lastGroup = GetFirstGroup(), #self.group
		wp, hp = self.anchor[widthPoint[self.db.anchor]](self.anchor), self.anchor[heightPoint[self.db.anchor]](self.anchor)
		for i = firstGroup, lastGroup do
			groupMember = self.group[i]:GetNumChild()
			if groupMember > 0 then
				frame = self.group[i].children[groupMember]
				if frame then
					if frame:GetTop() then
						wp = minMaxFunction[self.db.anchor][1](wp, frame[rWidthPoint[self.db.anchor]](frame))
						hp = minMaxFunction[self.db.anchor][2](hp, frame[rHeightPoint[self.db.anchor]](frame))
					else
						self:CallbackTimer("Border", 0.1, self, "UpdateBorder")
						return
					end
				end
			end
		end
		width = ceil(abs(wp - self.anchor[widthPoint[self.db.anchor]](self.anchor)) + offset * 2 + tabw)
		height = ceil(abs(hp - self.anchor[heightPoint[self.db.anchor]](self.anchor)) + offset * 2 + tabh)
	end
	if width > 10 and height > 22 then
		self.borderFrame:SetWidth(width)
		self.borderFrame:SetHeight(height)
		self.borderFrame:Show()
	else
		self.borderFrame:Hide()
		self:CallbackTimer("Border", 0.1, self, "UpdateBorder")
	end
end