local _, core = ...;
core.libs = {};
core.nilPetsBg = nil;

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 getInfoByIdItem(idItem, idInfo)
	local tmp = Item:CreateFromItemID(idItem)
	tmp:ContinueOnItemLoad(function()
		tmp = {GetItemInfo(idItem)};
	end)
	return (tmp[idInfo]);
end
core.libs.getInfoByIdItem = getInfoByIdItem;

-------- PETS --------
local function getInfoByIdPet(idPet, idInfo)
	numPets = C_PetJournal.GetNumPets()
	for i = 1, numPets do
		local tmp = {C_PetJournal.GetPetInfoByIndex(i)};
		if (tmp[11] == idPet) then
			if (core.nilPetsBg ~= nil) then
				core.nilPetsBg:Hide();
			end
			return (tmp[idInfo]);
		end
	end
	if (core.nilPetsBg ~= nil) then
		core.nilPetsBg:Show();
	end
	return "Err0r";
end
core.libs.getInfoByIdPet = getInfoByIdPet;

-------- MOUNTS --------
local function getCompletedMount(idMount)
	local tmp = {C_MountJournal.GetMountInfoByID(idMount)};
	return (tmp[11]);
end
core.libs.getCompletedMount = getCompletedMount;

local function printCriteriaHF(hfID)
	local streturn = "";
	for i = 1, GetAchievementNumCriteria(hfID) do
		local getCriteria = {GetAchievementCriteriaInfo(hfID, i)}
		if (getCriteria[3] == true) then
			streturn = streturn.."|cff00ff00"..getCriteria[1]..'|r\n';
		else
			streturn = streturn..getCriteria[1]..'\n';
		end
	end
	return streturn;
end
core.libs.printCriteriaHF = printCriteriaHF;

local function ifDoneNeed(pathTask)
	local pathSaved = {};
	local ret = false;
	for i = 1, #pathTask do
		pathSaved[i] = pathTask:sub(i, i)
	end
	local Saved = CreateFrame("Frame")
	Saved:RegisterEvent("ADDON_LOADED")
	function Saved:OnEvent(event, arg1)
		if event == "ADDON_LOADED" and arg1 == "WoWChallenger" then
			if (DoneMountTask[pathSaved[1]]["page"][pathSaved[2]]["step"][pathSaved[3]]["task"][pathSaved[4]]["done"] == false or DoneMountTask[pathSaved[1]]["page"][pathSaved[2]]["step"][pathSaved[3]]["task"][pathSaved[4]]["blank"] ~= nil) then
				ret = false;
			else
				ret = true;
			end
		end
	end
	Saved:SetScript("OnEvent", Saved.OnEvent);
	return ret;
end
core.libs.ifDoneNeed = ifDoneNeed;

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 heightBgTask(task)
	local height = 0;
	for i = 1, tablelength(task) do
		height = 40*i;
	end
	return height;
end
core.libs.heightBgTask = heightBgTask;

local function centerListObjectX(nb, nbmax, tailleImg)
	local x = 0;
	x =  (nb - 1) * tailleImg - nbmax * tailleImg / 2;
	return x;
end
core.libs.centerListObjectX = centerListObjectX;

local function centerListObjectXByWidth(nb, nbmax, width, tailleImg)
	local x = 0;
	x = (width - tailleImg * nbmax) / (nbmax + 1) * nb + tailleImg * (nb - 1);
	return x;
end
core.libs.centerListObjectXByWidth = centerListObjectXByWidth;

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;