Quantcast
local addon, ns = ...
local db, events, F, L = ns:Locals()

MasterLooterFrame_UpdatePlayers = function()
	local function MasterLooterPlayerSort(pInfo1, pInfo2)
		if db.settings.SortByName then
			return pInfo1.name < pInfo2.name
		elseif ( pInfo1.class == pInfo2.class ) then
			return pInfo1.name < pInfo2.name;
		else
			return pInfo1.class < pInfo2.class;
		end
	end

	local buttonsToHide = { };
	local playerInfo = { };
	for i = 1, MAX_RAID_MEMBERS do
		local name, class, className = GetMasterLootCandidate(LootFrame.selectedSlot, i);
		if ( name ) then
			local pInfo = { };
			pInfo["index"] = i;
			pInfo["name"] = name;
			pInfo["class"] = class;
			pInfo["className"] = className;
			tinsert(playerInfo, pInfo);
		end
	end
	table.sort(playerInfo, MasterLooterPlayerSort);

	local numColumns = ceil(#playerInfo / 10);
	numColumns = max(numColumns, 2);
	local numRows = ceil(#playerInfo / numColumns);
	local row = 0;
	local column = 0;
	local shownButtons = { };
	for i = 1, MAX_RAID_MEMBERS do
		if ( playerInfo[i] ) then
			row = row + 1;
			if ( row > numRows ) then
				row = 1;
				column = column + 1;
			end
			local buttonIndex = column * 10 + row;
			local playerFrame = MasterLooterFrame["player"..buttonIndex];
			-- create button if needed
			if ( not playerFrame ) then
				playerFrame = CreateFrame("BUTTON", nil, MasterLooterFrame, "MasterLooterPlayerTemplate");
				MasterLooterFrame["player"..buttonIndex] = playerFrame;
				if ( row == 1 ) then
					playerFrame:SetPoint("LEFT", MasterLooterFrame["player"..(buttonIndex - 10)], "RIGHT", 4, 0);
				else
					playerFrame:SetPoint("TOP", MasterLooterFrame["player"..(buttonIndex - 1)], "BOTTOM", 0, 0);
				end
				if ( mod(row, 2) == 0 ) then
					playerFrame.Bg:SetTexture(0, 0, 0, 0);
				end
			end
			-- set up button
			playerFrame.id = playerInfo[i].index;
			playerFrame.Name:SetText(playerInfo[i].name);
			local color = RAID_CLASS_COLORS[playerInfo[i].className];
			playerFrame.Name:SetTextColor(color.r, color.g, color.b);
			playerFrame:Show();
			if ( buttonsToHide[playerFrame] ) then
				buttonsToHide[playerFrame] = nil;
			end
			shownButtons[playerFrame] = 1;
			if (playerFrame.Name:IsTruncated()) then
				playerFrame.tooltip = playerInfo[i].name;
			else
				playerFrame.tooltip = nil;
			end
		else
			break;
		end
	end
	MasterLooterFrame:SetWidth(numColumns * 102 + 12);
	MasterLooterFrame:SetHeight(numRows * 23 + 63);
	for playerFrame in pairs(buttonsToHide) do
		playerFrame:Hide();
	end
	buttonsToHide = shownButtons;
end

function events:HideConfirmation(self, key)
	key = key or false

	MasterLooterFrame.slot = LootFrame.selectedSlot
	MasterLooterFrame.candidateId = key and MasterLooterFrame[key].id or self.id

	if (LootFrame.selectedQuality >= MASTER_LOOT_THREHOLD) and not db.settings.HideConfirmation then
		StaticPopup_Show("CONFIRM_LOOT_DISTRIBUTION", ITEM_QUALITY_COLORS[LootFrame.selectedQuality].hex .. LootFrame.selectedItemName .. FONT_COLOR_CODE_CLOSE, GetMasterLootCandidate(MasterLooterFrame.slot, MasterLooterFrame.candidateId), "LootWindow")
	else
		MasterLooterFrame_GiveMasterLoot()
	end
end

MasterLooterPlayerFrame_OnClick = function(self)
	events:HideConfirmation(self)
end

MasterLooterFrame:HookScript("OnShow", function(self)
	for k, v in pairs(MasterLooterFrame) do
		if string.find(k, "player") then
			MasterLooterFrame[k]:SetScript("OnClick", function()
				events:HideConfirmation(self, k)
			end)
		end
	end
end)