local addonName, core = ...;
core.libs = {};

local function tablelength(T)
	local count = 0
	for _ in pairs(T) do count = count + 1 end
	return count
end
core.libs.tablelength = tablelength;

local function ifDoneTask(task)
	if (task ~= nil) then
		if (task["hf"] ~= nil) then
			if (task["hf"][4] ~= true) then
				return false
			end
		elseif (task["quest"] ~= nil) then
			if (task["quest"].completed ~= true) then
				return false
			end
		end
		return true;
	end
	return false;
end
core.libs.ifDoneTask = ifDoneTask;

local function ifDoneStep(step)
	if (step ~= nil and step.task ~= nil) then
		for i = 1, tablelength(step.task) do
			if (step.task[i]["hf"] ~= nil) then
				if (step.task[i]["hf"][4] ~= true) then
					return false
				end
			elseif (step.task[i]["quest"] ~= nil) then
				if (step.task[i]["quest"].completed ~= true) then
					return false
				end
			elseif (step.task[i]["need"] ~= nil) then
				if (step.task[i]["need"].done ~= true) then
					return false
				end
			elseif (step.task[i]["custom"] ~= nil) then
				if (step.task[i]["custom"].done ~= true) then
					return false
				end
			end
		end
		return true;
	end
	return false;
end
core.libs.ifDoneStep = ifDoneStep;

local function ifDoneAllPrevStep(challengePage, stepID)
	if (challengePage ~= nil and challengePage.step ~= nil) then
		stepID = stepID - 1;
		while (stepID > 0) do
			if (ifDoneStep(challengePage.step[stepID]) ~= true) then
				return false
			end
			stepID = stepID - 1;
		end
		return true
	end
	return false;
end
core.libs.ifDoneAllPrevStep = ifDoneAllPrevStep;

local function ifDonePage(page)
	if (page ~= nil and page.step ~= nil) then
		for i = 1, tablelength(page.step) do
			if (ifDoneStep(page.step[i]) == false) then
				return false;
			end
		end
		return true;
	end
	return false;
end
core.libs.ifDonePage = ifDonePage;

local function ifDoneAllPrevPage(challenge, pageID)
	if (challenge ~= nil and challenge.page ~= nil) then
		pageID = pageID - 1;
		while (pageID > 0) do
			if (ifDonePage(challenge.page[pageID]) ~= true) then
				return false
			end
			pageID = pageID - 1;
		end
		return true
	end
	return false;
end
core.libs.ifDoneAllPrevPage = ifDoneAllPrevPage;

local function getPointsHFStep(step)
	local points = 0;
	if (step ~= nil and step.task ~= nil) then
		for i = 1, tablelength(step.task) do
			if (step.task[i]["type"] == "hf") then
				points = points + step.task[i]["hf"][3];
			end
		end
		return points;
	end
	return "ErrOr";
end
core.libs.getPointsHFStep = getPointsHFStep;

local function getLootTask(task)
	if (task ~= nil) then
		return task["hf"][11];
	end
	return "ErrOr";
end
core.libs.getLootTask = getLootTask;

local function getTotalPointsChallenge(challenge)
	local completed = 1;
	local pointsHF = 0;
	for i = 1, tablelength(challenge.tab) do
		if (challenge.tab[i].name == core.L["Alliance"] or challenge.tab[i].name == core.L["Horde"]) then
			local factionPlayer = UnitFactionGroup("player")
			if (factionPlayer == challenge.tab[i].faction) then
				for i2 = 1, tablelength(challenge.tab[i].page) do
					for i3 = 1, tablelength(challenge.tab[i].page[i2].step) do
						pointsHF = pointsHF + getPointsHFStep(challenge.tab[i].page[i2].step[i3]);
						if (ifDoneStep(challenge.tab[i].page[i2].step[i3]) == false) then
							completed = 0;
						end
					end
				end
			end
		else
			for i2 = 1, tablelength(challenge.tab[i].page) do
				for i3 = 1, tablelength(challenge.tab[i].page[i2].step) do
					pointsHF = pointsHF + getPointsHFStep(challenge.tab[i].page[i2].step[i3]);
					if (ifDoneStep(challenge.tab[i].page[i2].step[i3]) == false) then
						completed = 0;
					end
				end
			end
		end
	end
	if (completed == 1) then
		return "|cff00ff00"..pointsHF..core.L["HFPoints"].."|r\n";
	else
		return pointsHF..core.L["HFPoints"].."\n";
	end
end

local function getTotalLootsChallenge(challenge)
	local pointsHFstr = getTotalPointsChallenge(challenge);
	local lootHF = "";
	for i = 1, tablelength(challenge.tab) do
		if (challenge.tab[i].name == core.L["Alliance"] or challenge.tab[i].name == core.L["Horde"]) then
			local factionPlayer = UnitFactionGroup("player")
			if (factionPlayer == challenge.tab[i].faction) then
				for i2 = 1, tablelength(challenge.tab[i].page) do
					for i3 = 1, tablelength(challenge.tab[i].page[i2].step) do
						for i4 = 1, tablelength(challenge.tab[i].page[i2].step[i3].task) do
							if (challenge.tab[i].page[i2].step[i3].task[i4]["type"] == "hf") then
								if (ifDoneTask(challenge.tab[i].page[i2].step[i3].task[i4]) == true) then
									lootHF = lootHF.."|cff00ff00"..getLootTask(challenge.tab[i].page[i2].step[i3].task[i4]).."|r";
								else
									lootHF = lootHF..getLootTask(challenge.tab[i].page[i2].step[i3].task[i4]);
								end
								if (getLootTask(challenge.tab[i].page[i2].step[i3].task[i4]) ~= "") then
									lootHF = lootHF.."\n";
								end
							end
						end
					end
				end
			end
		else
			for i2 = 1, tablelength(challenge.tab[i].page) do
				for i3 = 1, tablelength(challenge.tab[i].page[i2].step) do
					for i4 = 1, tablelength(challenge.tab[i].page[i2].step[i3].task) do
						if (challenge.tab[i].page[i2].step[i3].task[i4]["type"] == "hf") then
							if (ifDoneTask(challenge.tab[i].page[i2].step[i3].task[i4]) == true) then
								lootHF = lootHF.."|cff00ff00"..getLootTask(challenge.tab[i].page[i2].step[i3].task[i4]).."|r";
							else
								lootHF = lootHF..getLootTask(challenge.tab[i].page[i2].step[i3].task[i4]);
							end
							if (getLootTask(challenge.tab[i].page[i2].step[i3].task[i4]) ~= "") then
								lootHF = lootHF.."\n";
							end
						end
					end
				end
			end
		end
	end
	return pointsHFstr..lootHF;
end
core.libs.getTotalLootsChallenge = getTotalLootsChallenge;

local function ifMAExalted(factionId)
	local friendshipInfo = {GetFriendshipReputation(factionId)};
	if (friendshipInfo[2] == 42000) then
		return true;
	end
	return false;
end
core.libs.ifMAExalted = ifMAExalted;

local function changeReputToTextMA(factionId)
	local friendshipInfo = {GetFriendshipReputation(factionId)};
	return friendshipInfo[5];
end
core.libs.changeReputToTextMA = changeReputToTextMA;

local function changeReputToText(reputation)
	local stateFaction = "Err0r";
	local valueMax = 0;
	local valueAct = 0;
	local factionInfo = {GetFactionInfoByID(reputation)}
	if (factionInfo[3] == 1) then
		stateFaction = getglobal("FACTION_STANDING_LABEL1");
		valueAct = factionInfo[6] + 42000
		valueMax = "36000";
	elseif (factionInfo[3] == 2) then
		stateFaction = getglobal("FACTION_STANDING_LABEL2");
		valueAct = factionInfo[6] + 6000
		valueMax = "3000";
	elseif (factionInfo[3] == 3) then
		stateFaction = getglobal("FACTION_STANDING_LABEL3");
		valueAct = factionInfo[6] + 3000
		valueMax = "3000";
	elseif (factionInfo[3] == 4) then
		stateFaction = getglobal("FACTION_STANDING_LABEL4");
		valueAct = factionInfo[6]
		valueMax = "3000";
	elseif (factionInfo[3] == 5) then
		stateFaction = getglobal("FACTION_STANDING_LABEL5");
		valueAct = factionInfo[6] - 3000
		valueMax = "6000";
	elseif (factionInfo[3] == 6) then
		stateFaction = getglobal("FACTION_STANDING_LABEL6");
		valueAct = factionInfo[6] - 9000
		valueMax = "12000";
	elseif (factionInfo[3] == 7) then
		stateFaction = getglobal("FACTION_STANDING_LABEL7");
		valueAct = factionInfo[6] - 21000
		valueMax = "21000";
	elseif (factionInfo[3] == 8) then
		stateFaction = getglobal("FACTION_STANDING_LABEL8");
		valueMax = "21000";
		valueAct = "21000";
	end
	return(stateFaction.." "..valueAct.."/"..valueMax)
end
core.libs.changeReputToText = changeReputToText;

local function progressPourcent(challenge)
	local task = 0;
	local taskMax = 0;
	for i = 1, tablelength(challenge.tab) do
		if (challenge.tab[i].name == core.L["Alliance"] or challenge.tab[i].name == core.L["Horde"]) then
			local factionPlayer = UnitFactionGroup("player")
			if (factionPlayer == challenge.tab[i].faction) then
				for i2 = 1, tablelength(challenge.tab[i].page) do
					for i3 = 1, tablelength(challenge.tab[i].page[i2].step) do
						for i4 = 1, tablelength(challenge.tab[i].page[i2].step[i3].task) do
							if (ifDoneTask(challenge.tab[i].page[i2].step[i3].task[i4]) == true) then
								task = task + 1;
							end
							taskMax = taskMax + 1;
						end
					end
				end
			end
		else
			for i2 = 1, tablelength(challenge.tab[i].page) do
				for i3 = 1, tablelength(challenge.tab[i].page[i2].step) do
					for i4 = 1, tablelength(challenge.tab[i].page[i2].step[i3].task) do
						if (ifDoneTask(challenge.tab[i].page[i2].step[i3].task[i4]) == true) then
							task = task + 1;
						end
						taskMax = taskMax + 1;
					end
				end
			end
		end
	end
	if (taskMax == 0) then
		return (100);
	end
	return ((task/taskMax)*100);
end
core.libs.progressPourcent = progressPourcent;


-- DB toutes les quêtes et HF de l'addon pour refresh -- 1 = onglet HF ; 2 = onglet Mounts ; 3 = onglet Pets ; 4 = onglet Transmog
-- variable structure: {quest/hf id, onglets}
local WC_AllQuests = { {29145, 1}, {29195, 1}, {29196, 1}, {29197, 1}, {29198, 1}, {29199, 1},
						 {29200, 1}, {29201, 1}, {29181, 1}, {29272, 1}, {29283, 1}, {29214, 1},
						 {29245, 1}, {29249, 1}, {29254, 1}, {29279, 1}, {29203, 1}, {29298, 1},
						 {29302, 1}, {29303, 1}, {29310, 1}, {29311, 1}, {29281, 1}, {13667, 1},
						 {13718, 1}, {13717, 1}, {13714, 1}, {13716, 1}, {13715, 1}, {13668, 1},
						 {13697, 1}, {13721, 1}, {13720, 1}, {13722, 1}, {13719, 1}, {30252, 1},
						 {30535, 1}, {30254, 1}, {30255, 1}, {30256, 1}, {30257, 1}, {31945, 1},
						 {30260, 1}, {30516, 1}, {30523, 1}, {30524, 1}, {30529, 1}, {32682, 1},
						 {31338, 1}, {31339, 1}, {31340, 1}, {31341, 1}, {31534, 1}, {31537, 1},
						 {31538, 1}, {30517, 1}, {30518, 1}, {31947, 1}, {30521, 1}, {31320, 1},
						 {31321, 1}, {31322, 1}, {31323, 1}, {31529, 1}, {31530, 1}, {31531, 1},
						 {31532, 1}, {31521, 1}, {31311, 1}, {31470, 1}, {31472, 1}, {31475, 1},
						 {31478, 1}, {31479, 1}, {9739, 1}, {9743, 1}, {9919, 1}, {9806, 1},
						 {9726, 1}, {29691, 1}, {9729, 1}, {28100, 1}, {27989, 1}, {27994, 1},
						 {27995, 1}, {28338, 1}, {28366, 1}, {28362, 1}, {28364, 1}, {28521, 1},
						 {28524, 1}, {28460, 1}, {28464, 1}, {28467, 1}, {28530, 1}, {28469, 1},
						 {28470, 1}, {28472, 1}, {9835, 1}, {10115, 1}, {10116, 1}, {9792, 1},
						 {9888, 1}
						};
local WC_AllHF = { {5869, 1}, {5868, 1}, {5862, 1}, {5864, 1}, {5865, 1}, {5861, 1},
					 {5867, 1}, {5872, 1}, {5870, 1}, {5866, 1}, {5879, 1}, {5871, 1},
					 {5874, 1}, {5873, 1}, {5859, 1}, {2756, 1}, {2758, 1}, {2782, 1},
					 {2836, 1}, {2770, 1}, {2817, 1}, {2772, 1}, {2781, 1}, {2764, 1},
					 {2777, 1}, {2760, 1}, {2780, 1}, {2763, 1}, {2778, 1}, {2761, 1},
					 {2779, 1}, {2762, 1}, {2788, 1}, {2771, 1}, {2816, 1}, {2783, 1},
					 {2765, 1}, {2787, 1}, {2769, 1}, {2786, 1}, {2768, 1}, {2785, 1},
					 {2767, 1}, {2784, 1}, {2766, 1}, {2144, 1}, {911, 1}, {914, 1},
					 {912, 1}, {915, 1}, {1396, 1}, {6006, 1}, {910, 1}, {626, 1},
					 {1552, 1}, {1281, 1}, {937, 1}, {913, 1}, {2676, 1}, {2418, 1},
					 {2436, 1}, {2576, 1}, {2416, 1}, {2419, 1}, {2421, 1}, {2422, 1},
					 {2798, 1}, {1022, 1}, {1023, 1}, {1024, 1}, {6008, 1}, {6011, 1},
					 {1034, 1}, {1028, 1}, {1029, 1}, {1030, 1}, {6007, 1}, {6013, 1},
					 {1035, 1}, {1145, 1}, {263, 1}, {271, 1}, {272, 1}, {1038, 1},
					 {966, 1}, {963, 1}, {969, 1}, {972, 1}, {255, 1}, {981, 1},
					 {1261, 1}, {283, 1}, {288, 1}, {289, 1}, {1040, 1}, {291, 1},
					 {292, 1}, {1656, 1}, {260, 1}, {1279, 1}, {1695, 1}, {1697, 1},
					 {1702, 1}, {1703, 1}, {1188, 1}, {1291, 1}, {1696, 1}, {1699, 1},
					 {1701, 1}, {1704, 1}, {1693, 1}, {1791, 1}, {1789, 1}, {1786, 1},
					 {1788, 1}, {1792, 1}, {1790, 1}, {1793, 1}, {1185, 1}, {295, 1},
					 {1936, 1}, {2796, 1}, {303, 1}, {1683, 1}, {273, 1}, {1255, 1},
					 {277, 1}, {1687, 1}, {1688, 1}, {1690, 1}, {252, 1}, {1282, 1},
					 {279, 1}, {1686, 1}, {1689, 1}, {1691, 1}, {2497, 1}, {2420, 1},
					 {1026, 1}, {1025, 1}, {1027, 1}, {6009, 1}, {6012, 1}, {1036, 1},
					 {1032, 1}, {1031, 1}, {1033, 1}, {6010, 1}, {6014, 1}, {1037, 1},
					 {1039, 1}, {965, 1}, {967, 1}, {968, 1}, {1041, 1}, {1280, 1},
					 {1698, 1}, {1791, 1}, {259, 1}, {1685, 1}, {12951, 1}, {12891, 1},
					 {13467, 1}, {13163, 1}, {13504, 1}, {12954, 1}, {12510, 1}, {12515, 1},
					 {13076, 1}, {12081, 1}, {12066, 1}, {12243, 1}, {12414, 1}, {12076, 1},
					 {12242, 1}, {12291, 1}, {13557, 1}, {13553, 1}, {14013, 1}, {14014, 1},
					 {12957, 1}, {12509, 1}, {12518, 1}, {13077, 1}, {12292, 1}, {10059, 1},
					 {12245, 1}, {12415, 1}, {10778, 1}, {11340, 1}, {12244, 1}, {12413, 1},
					 {12950, 1}, {12479, 1}, {13466, 1}, {13161, 1}, {13503, 1}, {12949, 1},
					 {12478, 1}, {13206, 1}, {14002, 1}, {8935, 1}, {9833, 1}, {10348, 1},
					 {9564, 1}, {10350, 1}, {10018, 1}, {11188, 1}, {11189, 1}, {11157, 1},
					 {10672, 1}, {10994, 1}, {11190, 1}, {11543, 1}, {11545, 1}, {11446, 1},
					 {12988, 1}, {12593, 1}, {13144, 1}, {12947, 1}, {12989, 1}, {13712, 1},
					 {13776, 1}, {13250, 1}, {9923, 1}, {9562, 1}, {10349, 1}, {7292, 1},
					 {7293, 1}, {7294, 1}, {7295, 1}, {6544, 1}, {6552, 1}, {7306, 1},
					 {7325, 1}, {7300, 1}, {7301, 1}, {7302, 1}, {7303, 1}, {7304, 1},
					 {7305, 1}, {942, 1}, {900, 1}, {944, 1}, {899, 1}, {943, 1},
					 {901, 1}
	};

local function refreshUIAuto(item_id, item_type, nbTab)
	if (item_type == 2) then
		for i = 1, #WC_AllQuests do
			if (WC_AllQuests[i][1] == item_id) then
				if (WC_AllQuests[i][2] == nbTab) then
					return true
				else
					return false;
				end
			end
		end
		return false;
	elseif (item_type == 1) then
		for i = 1, #WC_AllHF do
			if (WC_AllHF[i][1] == item_id) then
				if (WC_AllHF[i][2] == nbTab) then
					return true
				else
					return false;
				end
			end
		end
		return false;
	end
	return false;
end
core.libs.refreshUIAuto = refreshUIAuto;