local addonName, core = ...;
local campaignDB;

core.locationPlayer = 0; -- 1 Character Page, 2 Fight Scene
core.onTabCampaign = false;

local currFight = {0, 0, 0, 0, 0};
local PdvCurrEnnemy = {"nil", "nil", "nil", "nil", "nil"};
local DamageCurrEnnemy = {"nil", "nil", "nil", "nil", "nil"};
local CdSpellCurrEnnemy = { {}, {}, {}, {}, {} };
local EffectCurrEnnemy = { {}, {}, {}, {}, {} };
local EffectCurrPlayer = {};
local CdSpellPlayer = {};
local AnimEnCours = {false, false, false, false, false};
local canPlay = 0;
local nbPlay = 1;
local selectedSpell = 0;
local selectedSpellAnytime = 0;
local zoneIdFight = 0;
local fightId = 0;
local checkVictoryException = {};
local dontCheckVictory = false;
local SpellEnCours = false;
local RenverseThisLap = false;
local currentEnnemyTooltip = 0;

local LoadCampaignDB = CreateFrame("Frame");
LoadCampaignDB:RegisterEvent("ADDON_LOADED");
LoadCampaignDB:SetScript("OnEvent", function()
	core.Loadcampaign();
	campaignDB = core.DBcampaign;
	core.Loadmobs();
	DBProfile.pdvMax = 50 + core.DBUpgrade[4].list[1].bonus * core.DBUpgrade[4].list[1].level;
	DBProfile.damageMax = 5 + core.DBUpgrade[4].list[2].bonus * core.DBUpgrade[4].list[2].level;
end)

local GainSkillEvent = CreateFrame("FRAME");
GainSkillEvent:RegisterEvent("QUEST_TURNED_IN");
GainSkillEvent:SetScript("OnEvent", function(self, event, ...)
	DBProfile.skillPoints = DBProfile.skillPoints + 1;
	core.cpgn.CheckLimitSkillPoints();
	if (_G["ProfileContentCurrencyText"] ~= nil) then
		_G["ProfileContentCurrencyText"]:SetText(DBProfile.skillPoints);
	end
end)

--- GAMETOOLTIP ---
local OnLeaveProfile = function(self, motion)
	ListCampaign.bgProfile:SetBackdrop({bgFile = "Interface/TALENTFRAME/spec-blue-bg",
										edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
										tile = false, edgeSize = 16, 
												insets = { left = 0, right = 0, top = 0, bottom = -15 }
												});
end

local OnEnterProfile = function(self, motion)
	_G["BtnProfileFrame"]:SetBackdrop({bgFile = "Interface/TALENTFRAME/spec-blue-bg",
										edgeFile = "Interface/AddOns/WoWChallenger/images/campaign/UI-Tooltip-Border",
										tile = false, edgeSize = 16, 
												insets = { left = 0, right = 0, top = 0, bottom = -15 }
												});
end

local OnLeave = function(self, motion)
	currentEnnemyTooltip = 0;
	SetCursor(nil);
	GameTooltip:Hide()
end

local OnEnterCurrency = function(self, motion)
	GameTooltip:SetOwner(self, self.anchor, 8, -13);
	GameTooltip:AddLine(core.L["skillpointsMAJ"], 1, 1, 1);
	GameTooltip:AddLine(core.L["DescPointsCompet"], 1, 1, 1, true);
	GameTooltip:AddLine(" ");
	GameTooltip:AddLine(core.L["Total"]..core.L["2points"].."|cffffffff"..DBProfile.skillPoints.."/50|r");
	GameTooltip:Show();
end

local OnEnterPointsLife = function(self, motion)
	GameTooltip:SetOwner(self, self.anchor);
	GameTooltip:AddLine(core.L["Points de vie"], 1, 1, 1);
	GameTooltip:Show();
end

local OnEnterPointsAttack = function(self, motion)
	GameTooltip:SetOwner(self, self.anchor);
	GameTooltip:AddLine(core.L["Points d'attaque"], 1, 1, 1);
	GameTooltip:Show();
end


local OnEnterEffectPlayer = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_TOP");
	currentEnnemyTooltip = 6;
	GameTooltip:AddLine(core.L["Effects"]);
	if (core.cpgn.NbEffectsPlayer() == 0) then
		GameTooltip:AddLine(core.L["Vous n'avez aucun effet pour l'instant."], 0.6, 0.6, 0.6);
	else
		for i = 1, #EffectCurrPlayer do
			local firstEffect = true;
			if (EffectCurrPlayer[i] ~= nil and EffectCurrPlayer[i] ~= "nil" and EffectCurrPlayer[i].durationEffect ~= -1) then
				if (firstEffect == false) then
					GameTooltip:AddLine(" ");
				end
				if (core.DBEffects[EffectCurrPlayer[i].idEffect].positive == true) then
					GameTooltip:AddLine(core.DBEffects[EffectCurrPlayer[i].idEffect].name, 0, 1, 0);
				else
					GameTooltip:AddLine(core.DBEffects[EffectCurrPlayer[i].idEffect].name, 1, 0, 0);
				end
				local displayPhrase = core.DBEffects[EffectCurrPlayer[i].idEffect].desc;
				if (EffectCurrPlayer[i].chargeEffect ~= nil) then
					displayPhrase = displayPhrase.."\n|cffffffff"..EffectCurrPlayer[i].chargeEffect.." "..core.L["charge(s)"];
				end
				if (EffectCurrPlayer[i].durationEffect ~= 99) then
					displayPhrase = displayPhrase.."\n|cffffffff"..EffectCurrPlayer[i].durationEffect.." "..core.L["tour(s) restant(s)"].."|r";
				end
				GameTooltip:AddLine(displayPhrase, 1, 0.82, 0, true);
				GameTooltip:AddTexture(core.DBEffects[EffectCurrPlayer[i].idEffect].iconId, {width = 32, height = 32});
				firstEffect = false;
			end
		end
	end
	GameTooltip:Show();
end

local OnEnterPlayerSpell = function(self, motion)
	GameTooltip:SetOwner(self, self.anchor);
	GameTooltip:AddLine(self.spell.name, 1, 1, 1);
	GameTooltip:AddTexture(self.spell.iconId, {width = 16,	height = 16});
	GameTooltip:AddLine(self.spell.desc, 1, 0.82, 0, true);
	GameTooltip:AddLine(" ");
	if (self.spell.target == true) then
		GameTooltip:AddLine(core.L["ClickThenChoose"], 0.6, 0.6, 0.6);
	elseif (self.spell.target == false) then
		GameTooltip:AddLine(core.L["Instant"], 0.6, 0.6, 0.6);
	end
	GameTooltip:Show();
end

local OnEnterPlayerSpell_Selected = function(self, motion)
	GameTooltip:SetOwner(self, self.anchor);
	GameTooltip:AddLine(self.spell.name, 1, 1, 1);
	GameTooltip:AddTexture(self.spell.iconId, {width = 16,	height = 16});
	GameTooltip:AddLine(self.spell.desc, 1, 0.82, 0, true);
	GameTooltip:AddLine(" ");
	GameTooltip:AddLine(core.L["ClickToCancel"], 0.8, 0, 0, 0.5);
	GameTooltip:Show();
end

local OnEnterTooltipEnnemy = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_BOTTOM", -25, 0);
	currentEnnemyTooltip = self.id;
	GameTooltip:AddLine(currFight[self.id].name, 1, 1, 1);
	local pdvColor = "";
	if (PdvCurrEnnemy[self.id] < currFight[self.id].pdv) then
		pdvColor = "|cffff0000"..PdvCurrEnnemy[self.id].."|r"
	elseif (PdvCurrEnnemy[self.id] == currFight[self.id].pdv) then
		pdvColor = "|cffffffff"..PdvCurrEnnemy[self.id].."|r"
	elseif (PdvCurrEnnemy[self.id] > currFight[self.id].pdv) then
		pdvColor = "|cff00ff00"..PdvCurrEnnemy[self.id].."|r"
	end
	local damageColor = "";
	if (DamageCurrEnnemy[self.id] < currFight[self.id].damage) then
		damageColor = "|cffff0000"..DamageCurrEnnemy[self.id].."|r"
	elseif (DamageCurrEnnemy[self.id] == currFight[self.id].damage) then
		damageColor = "|cffffffff"..DamageCurrEnnemy[self.id].."|r"
	elseif (DamageCurrEnnemy[self.id] > currFight[self.id].damage) then
		damageColor = "|cff00ff00"..DamageCurrEnnemy[self.id].."|r"
	end
	GameTooltip:AddDoubleLine(core.L["Points de vie"]..core.L["2points"]..pdvColor, core.L["Points d'attaque"]..core.L["2points"]..damageColor);
	GameTooltip:AddLine(" ");
	GameTooltip:AddLine(core.L["Spells"]);
	for i = 1, core.libs.tablelength(currFight[self.id].spells) do
		if (core.DBSpells[currFight[self.id].spells[i]].hide == nil) then
			if (i ~= 1) then
				GameTooltip:AddLine(" ");
			end
			GameTooltip:AddLine(core.DBSpells[currFight[self.id].spells[i]].name, 1, 1, 1);
			if (core.DBSpells[currFight[self.id].spells[i]].cooldown == 0) then
				GameTooltip:AddLine(core.DBSpells[currFight[self.id].spells[i]].desc.."\n"..core.L["Sort de base"], 1, 0.82, 0, true);
			elseif (core.DBSpells[currFight[self.id].spells[i]].cooldown == -1) then
				GameTooltip:AddLine(core.DBSpells[currFight[self.id].spells[i]].desc.."\n"..core.L["Passif"], 1, 0.82, 0, true);
			else
				if (CdSpellCurrEnnemy[self.id][i].cdSpell == 0) then
					GameTooltip:AddLine(core.DBSpells[currFight[self.id].spells[i]].desc.."\n|cff00ff00"..CdSpellCurrEnnemy[self.id][i].cdSpell.." "..core.L["tour(s) restant(s)"].."|r", 1, 0.82, 0, true);
				else
					GameTooltip:AddLine(core.DBSpells[currFight[self.id].spells[i]].desc.."\n|cffffffff"..CdSpellCurrEnnemy[self.id][i].cdSpell.." "..core.L["tour(s) restant(s)"].."|r", 1, 0.82, 0, true);
				end
			end
			GameTooltip:AddTexture(core.DBSpells[currFight[self.id].spells[i]].iconId, {width = 32, height = 32});
		end
	end
	GameTooltip:AddLine(" ");
	GameTooltip:AddLine(core.L["Effects"]);
	if (EffectCurrEnnemy[self.id] ~= nil) then
		for i = 1, #EffectCurrEnnemy[self.id] do
			local firstEffect = true;
			if (EffectCurrEnnemy[self.id][i] ~= nil and EffectCurrEnnemy[self.id][i] ~= "nil" and EffectCurrEnnemy[self.id][i].durationEffect ~= -1) then
				if (firstEffect == false) then
					GameTooltip:AddLine(" ");
				end
				if (core.DBEffects[EffectCurrEnnemy[self.id][i].idEffect].positive == true) then
					GameTooltip:AddLine(core.DBEffects[EffectCurrEnnemy[self.id][i].idEffect].name, 0, 1, 0);
				else
					GameTooltip:AddLine(core.DBEffects[EffectCurrEnnemy[self.id][i].idEffect].name, 1, 0, 0);
				end
				
				local displayPhrase = core.DBEffects[EffectCurrEnnemy[self.id][i].idEffect].desc;
				if (EffectCurrEnnemy[self.id][i].chargeEffect ~= nil) then
					displayPhrase = displayPhrase.."\n|cffffffff"..EffectCurrEnnemy[self.id][i].chargeEffect.." "..core.L["charge(s)"];
				end
				if (EffectCurrEnnemy[self.id][i].durationEffect ~= 99) then
					displayPhrase = displayPhrase.."\n|cffffffff"..EffectCurrEnnemy[self.id][i].durationEffect.." "..core.L["tour(s) restant(s)"].."|r";
				end
				GameTooltip:AddLine(displayPhrase, 1, 0.82, 0, true);
				GameTooltip:AddTexture(core.DBEffects[EffectCurrEnnemy[self.id][i].idEffect].iconId, {width = 32, height = 32});
				firstEffect = false;
			end
		end
	end
	GameTooltip:Show();
end

local OnEnterPointFights = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_TOP", 0, -20);
	if (self.boss == true) then
		GameTooltip:AddLine(core.L["Fight"].." "..self.id.." (Boss)", 1, 1, 1);
	else
		GameTooltip:AddLine(core.L["Fight"].." "..self.id, 1, 1, 1);
	end
	if (self.done == nil) then
		GameTooltip:AddLine(core.L["Lock"], 0.6, 0.6, 0.6);
		GameTooltip:AddLine(core.L["Terminez le combat précédent pour débloquer celui-ci."], 1, 0, 0, true);
	else
		if (self.done == true) then
			GameTooltip:AddLine(core.L["Vaincu"], 0, 1, 0);
		else
			GameTooltip:AddLine(core.L["Invaincu"], 1, 0, 0);
		end
		for i = 1, core.libs.tablelength(self.mobs) do
			if (self.mobs[i] ~= 0) then
				if (self.done == true) then
					GameTooltip:AddLine("- "..self.mobs[i].name, 0.5, 0.5, 0.5);
				else
					GameTooltip:AddLine("- "..self.mobs[i].name, 1, 1, 1);
				end
			end
		end
	end
	GameTooltip:AddLine(" ");
	if (self.done == true) then
		GameTooltip:AddLine(core.L["Rewards"].." |cff1eff00("..core.L["MountCollected"]..")|r");
	else
		GameTooltip:AddLine(core.L["Rewards"]);
	end
	for i = 1, #core.DBUpgrade do
		for i2 = 1, #core.DBUpgrade[i].list do
			if (core.DBUpgrade[i].list[i2].typeItem == "bonusLife" or core.DBUpgrade[i].list[i2].typeItem == "bonusAttack" or core.DBUpgrade[i].list[i2].typeItem == "bonusHeal") then
				for ibonus = 1, #core.DBUpgrade[i].list[i2].levelUnlock do
					if (core.DBUpgrade[i].list[i2].levelUnlock[ibonus][1] == self.zoneId and core.DBUpgrade[i].list[i2].levelUnlock[ibonus][2] == self.id) then
						GameTooltip:AddLine(core.DBUpgrade[i].list[i2].title, 1, 1, 1);
					end
				end
			else
				if (core.DBUpgrade[i].list[i2].levelUnlock[1] == self.zoneId and core.DBUpgrade[i].list[i2].levelUnlock[2] == self.id) then
					if (core.DBUpgrade[i].list[i2].typeItem == "spell") then
						GameTooltip:AddLine(core.DBUpgrade[i].list[i2].title.." ("..core.L["Spell"]..")", 1, 1, 1);
					elseif (core.DBUpgrade[i].list[i2].typeItem == 1 or core.DBUpgrade[i].list[i2].typeItem == 2) then
						GameTooltip:AddLine(core.DBUpgrade[i].list[i2].title.." ("..core.L["Skin"]..")", 1, 1, 1);
					end
				end
			end
		end
	end
	GameTooltip:Show();
end

local OnEnterProfileUpgrade = function(self, motion)
	GameTooltip:SetOwner(self, self.anchor);
	GameTooltip:AddLine(self.info.title, 1, 1, 1);
	GameTooltip:AddTexture(self.info.iconId, {width = 16,	height = 16});
	GameTooltip:AddLine(self.info.desc, 1, 0.82, 0, true);
	if (self.typeList == "skin") then
		if (self.info.unlock == 0) then
			if (DBProfile.Equip[self.info.typeItem] == self.info.displayId) then
				GameTooltip:AddLine(core.L["Actuellement équipé"], 0.6, 0.6, 0.6);
			else
				GameTooltip:AddLine(core.L["Cliquez pour équiper"], 0.6, 0.6, 0.6);
			end
		elseif (self.info.unlock == 2) then
			GameTooltip:AddLine(core.L["Cliquez pour visualiser"], 0.6, 0.6, 0.6);
		end
	end
	local nameMap = "";
	if (self.info.levelUnlock[1] ~= 0) then
		nameMap = core.DBcampaign[self.info.levelUnlock[1]].title;
	end
	if (self.info.unlock == 2) then
		GameTooltip:AddLine(core.L["FinishTheFight"].." "..self.info.levelUnlock[2].." "..core.L["sur"].." "..nameMap.." "..core.L["pour acheter ceci"], 1, 0, 0);
	elseif (self.info.unlock == 1) then
		SetCursor("Interface/CURSOR/Buy");
		GameTooltip:AddLine(core.L["Acheter pour"]..core.L["2points"].."|cffffffff"..self.info.price.." "..core.L["skillpoints"].."|r");
	end
	GameTooltip:Show();
end

local OnEnterProfileBonus = function(self, motion)
	GameTooltip:SetOwner(self, self.anchor);
	GameTooltip:AddDoubleLine("|cffffffff"..self.info.title.."|r", "|cff929292"..core.L["Rang"].." "..self.info.level.."/"..self.info.levelMax.."|r");
	GameTooltip:AddTexture(self.info.iconId, {width = 16,	height = 16});
	GameTooltip:AddLine(self.info.desc, 1, 0.82, 0, true);
	local nameMap = "";
	if (self.info.levelUnlock[DBProfile.bonusLvL[self.info.id]  + 1] ~= nil and self.info.levelUnlock[DBProfile.bonusLvL[self.info.id]  + 1][1] ~= 0) then
		nameMap = core.DBcampaign[self.info.levelUnlock[DBProfile.bonusLvL[self.info.id]  + 1][1]].title;
	end
	if (self.info.unlock == 2) then
		GameTooltip:AddLine(core.L["FinishTheFight"].." "..self.info.levelUnlock[DBProfile.bonusLvL[self.info.id] + 1][2].." "..core.L["sur"].." "..nameMap.." "..core.L["pour acheter ceci"], 1, 0, 0);
	elseif (self.info.unlock == 1) then
		SetCursor("Interface/CURSOR/Buy");
		GameTooltip:AddLine(core.L["Acheter pour"]..core.L["2points"].."|cffffffff"..self.info.price.." "..core.L["skillpoints"].."|r");
	end
	GameTooltip:Show();
end


local OnEnterBtnRez = function(self, motion)
	GameTooltip:SetOwner(self, self.anchor);
	GameTooltip:AddLine(core.L["BtnRez"], 1, 1, 1);
	GameTooltip:AddLine(core.L["DescBtnRez"]);
	GameTooltip:AddLine(core.L["PriceBtnRez"]);
	GameTooltip:Show();
end


local OnEnterEnnemyScene = function(self, motion)
	if (selectedSpell ~= 0) then
		_G["FSFrameTooltipEnnemy"..self.position]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/bg_tooltipEnnemy",
							 edgeFile = "", 
							 tile = false, edgeSize = 16,
							 insets = { left = -15, right = -15, top = 0, bottom = -6 }});
	end
end
local OnLeaveEnnemyScene = function(self, motion)
		_G["FSFrameTooltipEnnemy"..self.position]:SetBackdrop({bgFile = "",
							 edgeFile = "", 
							 tile = false, edgeSize = 16,
							 insets = { left = -15, right = -15, top = 0, bottom = -6 }});
end
--- END GAMETOOLTIP ---
local function nothing()
	return;
end

--- PLAYER PROFILE FUNCTION ---
local function notEnoughCreditsAnim(param)
	if (param == 1) then
		_G["ProfileContentCurrencyText"]:SetFontObject("GameFontRedLarge");
	else
		_G["ProfileContentCurrencyText"]:SetFontObject("GameFontHighlightLarge");
	end
end

local function checkIfEnoughCredits(price)
	if (DBProfile.skillPoints - price >= 0) then
		return true;
	else
		local animTime = 0;
		local diffAnim = 1;
		for i = 1, 8 do
			core.cpgn.WC_wait(animTime, notEnoughCreditsAnim, diffAnim);
			animTime = animTime + 0.1;
			if (diffAnim == 1) then
				diffAnim = 0;
			else
				diffAnim = 1;
			end
		end
		return false;
	end
end 

local function checkNbItems(i)
	return #core.DBUpgrade[i].list
end

local function checkNbItemUnlock(j)
	local nb = 0;
	for i = 1, #DBProfile.itemUnlock[j] do
		if (DBProfile.itemUnlock[j][i] == 0) then
			nb = nb + 1;
		end
	end
	return nb;
end

local function HealPlayerWhenWin()
	_G["ProfileBtnRez"]:Disable();
	DBProfile.pdv = DBProfile.pdvMax;
	if (DBProfile.pdv >= 100 and DBProfile.pdvMax >= 100) then
		_G["BtnProfilePlayerLifeIcon"]:SetPoint("TOPRIGHT", _G["BtnProfileFrame"], "TOPRIGHT", -10, -7);
		_G["BtnProfilePlayerDamageIcon"]:SetPoint("TOPRIGHT", _G["BtnProfileFrame"], "TOPRIGHT", -10, -27);
		_G["BtnProfilePlayerDamageText"]:SetPoint("TOPRIGHT", _G["BtnProfileFrame"], "TOPRIGHT",-33, -27);
	end
	core.cpgn.refreshPlayerLifeFrame(_G["ProfilePlayerLifeText"], true);
	core.cpgn.refreshPlayerLifeFrame(_G["BtnProfilePlayerLifeText"], false);
end

local function ProfileHealPlayer(self)
	if (checkIfEnoughCredits(1) == true) then
		_G["ProfileBtnRez"]:Disable();
		DBProfile.pdv = DBProfile.pdvMax;
		if (DBProfile.pdv >= 100 and DBProfile.pdvMax >= 100) then
			_G["BtnProfilePlayerLifeIcon"]:SetPoint("TOPRIGHT", _G["BtnProfileFrame"], "TOPRIGHT", -10, -7);
			_G["BtnProfilePlayerDamageIcon"]:SetPoint("TOPRIGHT", _G["BtnProfileFrame"], "TOPRIGHT", -10, -27);
			_G["BtnProfilePlayerDamageText"]:SetPoint("TOPRIGHT", _G["BtnProfileFrame"], "TOPRIGHT",-33, -27);
		end
		DBProfile.skillPoints = DBProfile.skillPoints - 1;
		_G["ProfileContentCurrencyText"]:SetText(DBProfile.skillPoints);
		core.cpgn.refreshPlayerLifeFrame(_G["ProfilePlayerLifeText"], true);
		core.cpgn.refreshPlayerLifeFrame(_G["BtnProfilePlayerLifeText"], false);
		PlaySound("1432", "sfx", false);
		_G["ProfileScenePlayerModel"]:ApplySpellVisualKit(3210, true);
	end
end

local function BuyItem_OnClick(self)
	if (checkIfEnoughCredits(self.info.price) == true) then
		if (self.typeList == "bonus") then 
			if (core.DBUpgrade[self.positionItem].list[self.info.id].level <= core.DBUpgrade[self.positionItem].list[self.info.id].levelMax) then
				core.DBUpgrade[self.positionItem].list[self.info.id].level = core.DBUpgrade[self.positionItem].list[self.info.id].level + 1;
				DBProfile.bonusLvL[self.info.id] = DBProfile.bonusLvL[self.info.id] + 1;
				if (core.DBUpgrade[self.positionItem].list[self.info.id].level == core.DBUpgrade[self.positionItem].list[self.info.id].levelMax) then
					core.DBUpgrade[self.positionItem].list[self.info.id].unlock = 0;
					DBProfile.itemUnlock[self.positionItem][self.info.id] = 0;
					self.unlock = 0;
				end
				if (self.info.typeItem == "bonusLife") then
					DBProfile.pdvMax = DBProfile.pdvMax + core.DBUpgrade[4].list[self.info.id].bonus;
					DBProfile.pdv = DBProfile.pdv + core.DBUpgrade[4].list[self.info.id].bonus;
				elseif (self.info.typeItem == "bonusAttack") then
					DBProfile.damageMax = DBProfile.damageMax + core.DBUpgrade[4].list[self.info.id].bonus;
				end
				core.Loadmobs();
				core.cpgn.refreshPlayerLifeFrame(_G["ProfilePlayerLifeText"], true);
				core.cpgn.refreshPlayerLifeFrame(_G["BtnProfilePlayerLifeText"], false);
				_G["ProfilePlayerDamageText"]:SetText(DBProfile.damageMax);
				_G["BtnProfilePlayerDamageText"]:SetText(DBProfile.damageMax);
				if (DBProfile.pdv >= 100 and DBProfile.pdvMax >= 100) then
					_G["BtnProfilePlayerLifeIcon"]:SetPoint("TOPRIGHT", _G["BtnProfileFrame"], "TOPRIGHT", -10, -7);
					_G["BtnProfilePlayerDamageIcon"]:SetPoint("TOPRIGHT", _G["BtnProfileFrame"], "TOPRIGHT", -10, -27);
					_G["BtnProfilePlayerDamageText"]:SetPoint("TOPRIGHT", _G["BtnProfileFrame"], "TOPRIGHT",-33, -27);
				end
			end
			OnEnterProfileBonus(self);
		else
			core.DBUpgrade[self.positionItem].list[self.info.id].unlock = 0;
			DBProfile.itemUnlock[self.positionItem][self.info.id] = 0;
			self.unlock = 0;
			SetCursor(nil);
			OnEnterProfileUpgrade(self);
		end
		DBProfile.skillPoints = DBProfile.skillPoints - self.info.price;
		_G["ProfileContentCurrencyText"]:SetText(DBProfile.skillPoints);
		PlaySound("120", "sfx", false);
		core.cpgn.checkItemUnlock();
	end
end

local function EquipItem_OnClick(self)
	if (self.info.unlock == 0) then
		DBProfile.Equip[self.info.typeItem] = self.info.displayId;
		core.DBPlayer.weaponId = DBProfile.Equip[1];
		core.DBPlayer.displayId = DBProfile.Equip[2];
		PlaySound("5438",  "sfx", false);
		core.cpgn.checkItemUnlock();
		OnEnterProfileUpgrade(self);
	end
	if (self.info.typeItem == 1) then
		_G["ProfileScenePlayerModel"]:SetDisplayInfo(core.DBPlayer.displayId);
		_G["ProfileScenePlayerModel"]:EquipItem(self.info.displayId);
		_G["ProfileScenePlayerModel"]:EquipItem(self.info.displayId);
	elseif (self.info.typeItem == 2) then
		_G["ProfileScenePlayerModel"]:SetDisplayInfo(self.info.displayId);
		_G["ProfileScenePlayerModel"]:EquipItem(core.DBPlayer.weaponId);
		_G["ProfileScenePlayerModel"]:EquipItem(core.DBPlayer.weaponId);
	end
end

local function checkItemUnlock()
	core.cpgn.refreshPlayerLifeFrame(_G["ProfilePlayerLifeText"], true);
	if (DBProfile.pdvMax == DBProfile.pdv) then
		_G["ProfileBtnRez"]:Disable();
	else
		_G["ProfileBtnRez"]:Enable();
	end
	for i = 1, #core.DBUpgrade do
		for i2 = 1, #core.DBUpgrade[i].list do
			if (core.DBUpgrade[i].typeList == "buy" or core.DBUpgrade[i].typeList == "skin") then
				if (core.DBUpgrade[i].list[i2].levelUnlock[1] == 1) then
					if (Campaign1[core.DBUpgrade[i].list[i2].levelUnlock[2]].done == true and core.DBUpgrade[i].list[i2].unlock == 2) then
						core.DBUpgrade[i].list[i2].unlock = 1;
						DBProfile.itemUnlock[i][i2] = 1;
					end
				elseif (core.DBUpgrade[i].list[i2].levelUnlock[1] == 2) then
					if (Campaign2[core.DBUpgrade[i].list[i2].levelUnlock[2]].done == true and core.DBUpgrade[i].list[i2].unlock == 2) then
						core.DBUpgrade[i].list[i2].unlock = 1;
						DBProfile.itemUnlock[i][i2] = 1;
					end
				elseif (core.DBUpgrade[i].list[i2].levelUnlock[1] == 3) then
					if (Campaign3[core.DBUpgrade[i].list[i2].levelUnlock[2]].done == true and core.DBUpgrade[i].list[i2].unlock == 2) then
						core.DBUpgrade[i].list[i2].unlock = 1;
						DBProfile.itemUnlock[i][i2] = 1;
					end
				end
			elseif (core.DBUpgrade[i].typeList == "bonus") then
				if (DBProfile.bonusLvL[i2] < core.DBUpgrade[i].list[i2].levelMax and core.DBUpgrade[i].list[i2].levelUnlock[DBProfile.bonusLvL[i2] + 1][1] == 1) then
					if (Campaign1[core.DBUpgrade[i].list[i2].levelUnlock[DBProfile.bonusLvL[i2] + 1][2]].done == true) then
						core.DBUpgrade[i].list[i2].unlock = 1;
						DBProfile.itemUnlock[i][i2] = 1;
					else
						core.DBUpgrade[i].list[i2].unlock = 2;
						DBProfile.itemUnlock[i][i2] = 2;
					end
				elseif (DBProfile.bonusLvL[i2] < core.DBUpgrade[i].list[i2].levelMax and core.DBUpgrade[i].list[i2].levelUnlock[DBProfile.bonusLvL[i2] + 1][1] == 2) then
					if (Campaign2[core.DBUpgrade[i].list[i2].levelUnlock[DBProfile.bonusLvL[i2] + 1][2]].done == true) then
						core.DBUpgrade[i].list[i2].unlock = 1;
						DBProfile.itemUnlock[i][i2] = 1;
					else
						core.DBUpgrade[i].list[i2].unlock = 2;
						DBProfile.itemUnlock[i][i2] = 2;
					end
				elseif (DBProfile.bonusLvL[i2] < core.DBUpgrade[i].list[i2].levelMax and core.DBUpgrade[i].list[i2].levelUnlock[DBProfile.bonusLvL[i2] + 1][1] == 3) then
					if (Campaign3[core.DBUpgrade[i].list[i2].levelUnlock[DBProfile.bonusLvL[i2] + 1][2]].done == true) then
						core.DBUpgrade[i].list[i2].unlock = 1;
						DBProfile.itemUnlock[i][i2] = 1;
					else
						core.DBUpgrade[i].list[i2].unlock = 2;
						DBProfile.itemUnlock[i][i2] = 2;
					end
				end
			end
			_G["ProfilebarUpgrade"..i.."TextUnlock"]:SetText(checkNbItemUnlock(i).."/"..checkNbItems(i));
			_G["ProfileBgListUpgrade"..i.."Item"..i2].info = core.DBUpgrade[i].list[i2];
			_G["ProfileBgListUpgrade"..i.."Item"..i2].positionItem = i;
			_G["ProfileBgListUpgrade"..i.."Item"..i2].typeList = core.DBUpgrade[i].typeList;
			if (core.DBUpgrade[i].list[i2].unlock == 2) then
				_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetNormalTexture(core.DBUpgrade[i].list[i2].icon.."_lock");
				_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetHighlightTexture("");
				_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetScript("OnClick", nothing);
				if (core.DBUpgrade[i].typeList == "skin") then
					_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetScript("OnClick", EquipItem_OnClick);
				end
			elseif (core.DBUpgrade[i].list[i2].unlock == 1) then
				_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetNormalTexture(core.DBUpgrade[i].list[i2].icon.."_buy");
				_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetHighlightTexture(core.DBUpgrade[i].list[i2].icon.."_buy");
				_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetScript("OnClick", BuyItem_OnClick);
			elseif (core.DBUpgrade[i].list[i2].unlock == 0) then
				if (core.DBUpgrade[i].typeList == "skin") then
					if (DBProfile.Equip[core.DBUpgrade[i].list[i2].typeItem] == core.DBUpgrade[i].list[i2].displayId) then
						_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetNormalTexture(core.DBUpgrade[i].list[i2].icon.."_selected");
						_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetScript("OnClick", nothing);
					else
						_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetNormalTexture(core.DBUpgrade[i].list[i2].icon);
						_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetHighlightTexture(core.DBUpgrade[i].list[i2].icon);
						_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetScript("OnClick", EquipItem_OnClick);
					end
				else
					_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetNormalTexture(core.DBUpgrade[i].list[i2].icon);
					_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetScript("OnClick", nothing);
					_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetHighlightTexture("");
				end
			end
			if (core.DBUpgrade[i].typeList == "buy" or core.DBUpgrade[i].typeList == "skin") then
				_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetScript("OnEnter", OnEnterProfileUpgrade);
			elseif (core.DBUpgrade[i].typeList == "bonus") then
				_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetScript("OnEnter", OnEnterProfileBonus);
			end
			_G["ProfileBgListUpgrade"..i.."Item"..i2]:SetScript("OnLeave", OnLeave);
		end
	end
end
core.cpgn.checkItemUnlock = checkItemUnlock;


local function CreateAllUpgrade()
	for i = 1, #core.DBUpgrade do
		local barUpgrade = CreateFrame("Frame", "ProfilebarUpgrade"..i, _G["ProfileContentUpgrade"], BackdropTemplateMixin and "BackdropTemplate");
		barUpgrade:SetSize(296, 32);
		barUpgrade:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/step_title_bg"});
		barUpgrade.title = barUpgrade:CreateFontString(nil, "OVERLAY");
		barUpgrade.title:SetFontObject("GameFontHighlight");
		barUpgrade.title:SetText(core.DBUpgrade[i].title);
		barUpgrade.title:SetPoint("LEFT", barUpgrade, "LEFT", 10, 0);
		barUpgrade.nbAvailable = barUpgrade:CreateFontString("ProfilebarUpgrade"..i.."TextUnlock", "OVERLAY");
		barUpgrade.nbAvailable:SetFontObject("GameFontHighlight");
		barUpgrade.nbAvailable:SetText(checkNbItemUnlock(i).."/"..checkNbItems(i));
		barUpgrade.nbAvailable:SetPoint("RIGHT", barUpgrade, "RIGHT", -10, 0);
		if (i == 1) then
			barUpgrade:SetPoint("TOP", _G["ProfileContentUpgrade"], "TOP");
		else
			barUpgrade:SetPoint("TOP", _G["ProfileBgListUpgrade"..(i - 1)], "BOTTOM", 0, -5);
		end

		local bgListUpgrade = CreateFrame("Frame", "ProfileBgListUpgrade"..i, _G["ProfileContentUpgrade"], BackdropTemplateMixin and "BackdropTemplate");
		bgListUpgrade:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/step_task_bg"});
		bgListUpgrade:SetPoint("TOP", barUpgrade, "BOTTOM", 0, 5);
		local line = 1;
		for i2 = 1, #core.DBUpgrade[i].list do
			local ProfileItem = CreateFrame("Button", "ProfileBgListUpgrade"..i.."Item"..i2, _G["ProfileContentUpgrade"]);
			ProfileItem:SetSize(40, 40);
			if (i2 == 1) then
				ProfileItem:SetPoint("TOPLEFT", _G["ProfilebarUpgrade"..i], "BOTTOMLEFT", 27, -3);
			elseif (i2%6 == 0) then
				ProfileItem:SetPoint("TOP", _G["ProfileBgListUpgrade"..i.."Item"..(i2 - 5)], "BOTTOM", 0, -5);	
				line = line + 1;
			else
				ProfileItem:SetPoint("RIGHT", _G["ProfileBgListUpgrade"..i.."Item"..(i2 - 1)], "RIGHT", 50, 0);
			end
		end
		bgListUpgrade:SetSize(barUpgrade:GetWidth()-10, 55*line+(-9*(line-1)));
	end
end
-------------------------------
local function SetShowHideToModel(value)
	if (value == "hide") then
		_G["ProfileScenePlayerModel"]:Hide();
		_G["FightSceneEnnemy1"]:Hide();
		_G["FightSceneEnnemy2"]:Hide();
		_G["FightSceneEnnemy3"]:Hide();
		_G["FightSceneEnnemy4"]:Hide();
		_G["FightSceneEnnemy5"]:Hide();
		_G["FightScenePlayer"]:Hide();
	elseif (value == "show") then
		if (core.onTabCampaign == true) then
			if (core.locationPlayer == 1) then
				_G["ProfileScenePlayerModel"]:Show();
				_G["ProfileScenePlayerModel"]:EquipItem(core.DBPlayer.weaponId);
				_G["ProfileScenePlayerModel"]:EquipItem(core.DBPlayer.weaponId);
			elseif (core.locationPlayer == 2) then
				_G["FightScenePlayer"]:Show();
				_G["FightScenePlayer"]:EquipItem(core.DBPlayer.weaponId);
				_G["FightScenePlayer"]:EquipItem(core.DBPlayer.weaponId);
				if (currFight[3] ~= 0) then
					_G["FightSceneEnnemy3"]:Show();
				end
				if (currFight[2] ~= 0) then
					_G["FightSceneEnnemy2"]:Show();
				end
				if (currFight[4] ~= 0) then
					_G["FightSceneEnnemy4"]:Show();
				end
				if (currFight[1] ~= 0) then
					_G["FightSceneEnnemy1"]:Show();
				end
				if (currFight[5] ~= 0) then
					_G["FightSceneEnnemy5"]:Show();
				end
				for i = 1, 5 do
					if (currFight[i] ~= 0) then
						if (currFight[i].weaponId[1] ~= nil) then
							_G["FightSceneEnnemy"..i]:EquipItem(currFight[i].weaponId[1]);
						end
						if (currFight[i].weaponId[2] ~= nil) then
							_G["FightSceneEnnemy"..i]:EquipItem(currFight[i].weaponId[2]);
						end
						if (currFight[i].fxPassif ~= nil) then
							core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..i], currFight[i].fxPassif, false);
						end
					end
				end
			end
		end
	end
end
core.cpgn.SetShowHideToModel = SetShowHideToModel;

local function refreshCurrentEnnemyTooltip()
	if (currentEnnemyTooltip ~= 0) then
		if (currentEnnemyTooltip == 6) then
			OnEnterEffectPlayer(_G["FSPlayerEffectIcon"]);
		else
			OnEnterTooltipEnnemy(_G["FSFrameTooltipEnnemy"..currentEnnemyTooltip]);
		end
	end
end

local function randomBtwnEnnemyAlive()
	local i = 0;
	while (i == 0) do
		local randVar = math.random(1, 5);
		if (currFight[randVar] ~= 0 and PdvCurrEnnemy[randVar] > 0) then
			i = 1;
			return randVar;
		end
	end
	
end

local function refreshEnnemyDamageFrame(enemyPos)
	if (DamageCurrEnnemy[enemyPos] < currFight[enemyPos].damage) then
		_G["FSEnnemyDamageText"..enemyPos]:SetFontObject("GameFontRedLarge");
	elseif (DamageCurrEnnemy[enemyPos] == currFight[enemyPos].damage) then
		_G["FSEnnemyDamageText"..enemyPos]:SetFontObject("GameFontHighlightLarge");
	elseif (DamageCurrEnnemy[enemyPos] > currFight[enemyPos].damage) then
		_G["FSEnnemyDamageText"..enemyPos]:SetFontObject("GameFontGreenLarge");
	end
	_G["FSEnnemyDamageText"..enemyPos]:SetText(DamageCurrEnnemy[enemyPos]);
end

local function refreshEnnemyLifeFrame(enemyPos)
	if (PdvCurrEnnemy[enemyPos] < currFight[enemyPos].pdv) then
		_G["FSEnnemyLifeText"..enemyPos]:SetFontObject("GameFontRedLarge");
	elseif (PdvCurrEnnemy[enemyPos] == currFight[enemyPos].pdv) then
		_G["FSEnnemyLifeText"..enemyPos]:SetFontObject("GameFontHighlightLarge");
	elseif (PdvCurrEnnemy[enemyPos] > currFight[enemyPos].pdv) then
		_G["FSEnnemyLifeText"..enemyPos]:SetFontObject("GameFontGreenLarge");
	end
	_G["FSEnnemyLifeText"..enemyPos]:SetText(PdvCurrEnnemy[enemyPos]);
end

local function refreshPlayerSpellFrameForCD()
	for i = 1, #CdSpellPlayer do
		if (CdSpellPlayer[i] == 0) then
			_G["FSPlayerSpell"..i.."FrameSpellCD"]:Hide();
		else
			_G["FSPlayerSpell"..i.."FrameSpellCD"]:Show();
			_G["FSPlayerSpell"..i.."FrameSpellCDText"]:Show();
			_G["FSPlayerSpell"..i.."FrameSpellCDText"]:SetText(CdSpellPlayer[i]);
		end
	end
end

----- EFFECT FUNCTION -----
local function setSpellEnCours(boolean)
	SpellEnCours = boolean;
end

local function thereIsAnAlly(position, exception)
	for j = 1, 5 do
		if (PdvCurrEnnemy[j] ~= "nil" and PdvCurrEnnemy[j] > 0 and position ~= j and core.cpgn.findInArray(currFight[j].id, exception) == false) then
			return true;
		end
	end
	return false;
end

local function thereIsPlace(position)
	for j = 1, 5 do
		if ((PdvCurrEnnemy[j] == "nil"  or PdvCurrEnnemy[j] <= 0) and position ~= j) then
			return true;
		end
	end
	return false;
end

local function NbEffectsPlayer()
	local nbEffect = 0;
	for i = 1, #EffectCurrPlayer do
		if (EffectCurrPlayer[i] ~= nil and EffectCurrPlayer[i] ~= "nil" and EffectCurrPlayer[i].durationEffect ~= -1) then
			nbEffect = nbEffect + 1;
		end
	end
	return nbEffect;
end
core.cpgn.NbEffectsPlayer = NbEffectsPlayer;

local function CleanBoard(exception)
	for i = 1, 5 do
		if (core.cpgn.findInArray(i, exception) == false) then
			_G["FightSceneEnnemy"..i]:Hide();
			currFight[i] = 0;
			PdvCurrEnnemy[i] = "nil";
			DamageCurrEnnemy[i] = "nil";
			CdSpellCurrEnnemy[i] = "nil";
			_G["FSFrameTooltipEnnemy"..i]:Hide();
		end
	end
end

local function SummonUnit(posAI, idSummonUnit, timeAnim, sounds)
	currFight[posAI] = 0;
	currFight[posAI] = core.DBMobs[idSummonUnit];
	PdvCurrEnnemy[posAI] = core.DBMobs[idSummonUnit].pdv;
	DamageCurrEnnemy[posAI] = core.DBMobs[idSummonUnit].damage;
	CdSpellCurrEnnemy[posAI] = "nil";
	for ispell = 1, #core.DBMobs[idSummonUnit].spells do
		local tmp = {
			idSpell = core.DBSpells[core.DBMobs[idSummonUnit].spells[ispell]].id,
			cdSpell = core.DBSpells[core.DBMobs[idSummonUnit].spells[ispell]].cooldown,
		};
		if (CdSpellCurrEnnemy[posAI] == "nil") then
			CdSpellCurrEnnemy[posAI] = {};
			CdSpellCurrEnnemy[posAI][ispell] = tmp;
		else
			CdSpellCurrEnnemy[posAI][ispell] = tmp;
		end
	end
	EffectCurrEnnemy[posAI] = {};
	----- ADD PASSIF EFFECT WITHOUT SPELL -----
	if (currFight[posAI].effectPassif ~= nil) then
		for i2 = 1, #currFight[posAI].effectPassif do
			core.cpgn.AddEffectOnUnit(posAI, currFight[posAI].effectPassif[i2]);
		end
	end
	----- ADD PASSIF EFFECT -----
	for i = 1, #currFight[posAI].spells do
		if (core.DBSpells[currFight[posAI].spells[i]].cooldown == -1 and core.DBSpells[currFight[posAI].spells[i]].effectId ~= nil) then
			core.cpgn.AddEffectOnUnit(posAI, core.DBSpells[currFight[posAI].spells[i]].effectId);
		end
	end
	 if (UIParent:GetScale() < 0.8 and currFight[posAI].scene[posAI].positionLittle ~= nil) then
		_G["FightSceneEnnemy"..posAI]:SetPosition(currFight[posAI].scene[posAI].positionLittle[1], currFight[posAI].scene[posAI].positionLittle[2], currFight[posAI].scene[posAI].positionLittle[3])
	 else
		_G["FightSceneEnnemy"..posAI]:SetPosition(currFight[posAI].scene[posAI].position[1], currFight[posAI].scene[posAI].position[2], currFight[posAI].scene[posAI].position[3])
	 end
	--_G["FightSceneEnnemy"..posAI]:SetPosition(currFight[posAI].scene[posAI].position[1], currFight[posAI].scene[posAI].position[2], currFight[posAI].scene[posAI].position[3])
	core.cpgn.DisplayMobs(currFight[posAI], posAI)
	-- _G["FightSceneEnnemy"..posAI]:SetDisplayInfo(currFight[posAI].displayId);
	-- _G["FightSceneEnnemy"..posAI]:SetRotation(currFight[posAI].scene[posAI].rotation);
	-- _G["FightSceneEnnemy"..posAI]:SetPosition(currFight[posAI].scene[posAI].position[1], currFight[posAI].scene[posAI].position[2], currFight[posAI].scene[posAI].position[3])
	-- if (core.locationPlayer == 2 and core.onTabCampaign == true and _G["WoWChallengesFrame"]:IsShown()) then
		-- _G["FightSceneEnnemy"..posAI]:Show();
	-- end
	-- if (currFight[posAI].weaponId[1] ~= nil) then
		-- _G["FightSceneEnnemy"..posAI]:EquipItem(currFight[posAI].weaponId[1]);
	-- end
	-- if (currFight[posAI].weaponId[2] ~= nil) then
		-- _G["FightSceneEnnemy"..posAI]:EquipItem(currFight[posAI].weaponId[2]);
	-- end
	-- if (currFight[posAI].fxPassif ~= nil) then
		-- core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..posAI], currFight[posAI].fxPassif, false);
	-- end
	_G["FSEnnemyDamageText"..posAI]:SetText(DamageCurrEnnemy[posAI]);
	refreshEnnemyLifeFrame(posAI);
	refreshEnnemyDamageFrame(posAI);
	_G["FSFrameTooltipEnnemy"..posAI]:Show();
	_G["FightSceneEnnemy"..posAI]:ApplySpellVisualKit(721, true);
	_G["FightSceneEnnemy"..posAI]:SetAnimation(74);
	if (sounds ~= 0) then
		core.cpgn.ManyPlaySound(sounds);
	end
	core.cpgn.WC_wait(timeAnim, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..posAI], currFight[posAI].animBase, posAI);	
	dontCheckVictory = false;
end



local function unitIsStun(posAI)
	if (EffectCurrEnnemy[posAI] ~= nil) then
		for i = 1, #EffectCurrEnnemy[posAI] do
			if (EffectCurrEnnemy[posAI][i] ~= nil and EffectCurrEnnemy[posAI][i] ~= "nil") then
				if (EffectCurrEnnemy[posAI][i].idEffect == 2) then
					return true;
				end
			end
		end
	end
	return false;
end


local function MinusOneEffect()
	for i = 1, #currFight do
		if (currFight[i] ~= 0 and EffectCurrEnnemy[i] ~= nil) then
			for i2 = 1, #EffectCurrEnnemy[i] do
				if (EffectCurrEnnemy[i][i2] ~= nil and EffectCurrEnnemy[i][i2] ~= "nil" and EffectCurrEnnemy[i][i2].durationEffect ~= -1 and EffectCurrEnnemy[i][i2].durationEffect ~= 99) then
					EffectCurrEnnemy[i][i2].durationEffect = EffectCurrEnnemy[i][i2].durationEffect - 1;
					if (EffectCurrEnnemy[i][i2].durationEffect == 0) then
						if (EffectCurrEnnemy[i][i2].idEffect == 2) then
							_G["FightSceneEnnemy"..i]:SetAnimation(currFight[i].animBase);
						end
						EffectCurrEnnemy[i][i2] = "nil";
					end
				end
			end
		end
	end
	for j = 1, #EffectCurrPlayer do
		if (EffectCurrPlayer[j] ~= nil and EffectCurrPlayer[j] ~= "nil" and EffectCurrPlayer[j].durationEffect ~= -1 and EffectCurrPlayer[j].durationEffect ~= 99) then
			EffectCurrPlayer[j].durationEffect = EffectCurrPlayer[j].durationEffect - 1;
			if (EffectCurrPlayer[j].durationEffect == 0) then
				if (EffectCurrPlayer[j].idEffect == 2) then
					_G["FightScenePlayer"]:SetAnimation(core.DBPlayer.animBase);
				end
				EffectCurrPlayer[j] = "nil";
				_G["FSPlayerEffectText"]:SetText(core.cpgn.NbEffectsPlayer());
			end
		end
	end
end

local function MinusOneCharge(j, id)
	if (j == 0) then
		for i = 1, #EffectCurrPlayer do
			if (EffectCurrPlayer[i] ~= nil and EffectCurrPlayer[i] ~= "nil" and EffectCurrPlayer[i].chargeEffect ~= nil) then
				if (EffectCurrPlayer[i].idEffect == id) then
					EffectCurrPlayer[i].chargeEffect = EffectCurrPlayer[i].chargeEffect - 1;
					if (EffectCurrPlayer[i].chargeEffect == 0) then
						EffectCurrPlayer[i] = "nil";
						_G["FSPlayerEffectText"]:SetText(core.cpgn.NbEffectsPlayer());
					end
				end
			end
		end
	else
		for i = 1, #EffectCurrEnnemy[j] do
			if (EffectCurrEnnemy[j][i] ~= nil and EffectCurrEnnemy[j][i] ~= "nil" and EffectCurrEnnemy[j][i].chargeEffect ~= nil) then
				if (EffectCurrEnnemy[j][i].idEffect == id) then
					EffectCurrEnnemy[j][i].chargeEffect = EffectCurrEnnemy[j][i].chargeEffect - 1;
					if (EffectCurrEnnemy[j][i].chargeEffect == 0) then
						EffectCurrEnnemy[j][i] = "nil";
					end
				end
			end
		end
	end
end

local function SameNameEvent(nameEvent, whenActive)
	for i = 1, #whenActive do
		if (nameEvent == whenActive[i]) then
			return true;
		end
	end
	return false;
end

local function EventEffect(nameEvent, ...)
	local timePlay = 0; 
	for j = 1, 5 do
		if (EffectCurrEnnemy[j] ~= nil) then
			for i = 1, #EffectCurrEnnemy[j] do
				if (EffectCurrEnnemy[j][i] ~= nil and EffectCurrEnnemy[j][i] ~= "nil" and SameNameEvent(nameEvent, core.DBEffects[EffectCurrEnnemy[j][i].idEffect].whenActive) == true) then
					if (core.DBEffects[EffectCurrEnnemy[j][i].idEffect].NoTimeplay ~= nil) then
						timePlay = timePlay - 1;
					end
					if (nameEvent == "SufferDamageByDotHimself" or 
						nameEvent == "SufferDamageHimself" or 
						nameEvent == "PASSIFSufferDamageHimself" or 
						nameEvent == "DieHimself" or 
						nameEvent == "UnderAttackHimself" or
						nameEvent == "CustomParadeDuelliste" or
						nameEvent == "HimselfAttacking") then
						local args = {...};
						if (args[1] == j) then
							if (nameEvent == "PASSIFSufferDamageHimself") then
								core.cpgn.WC_wait(0, core.cpgn.ActiveEffectOnEnnemy, core.DBEffects[EffectCurrEnnemy[j][i].idEffect], j);
							else
								core.cpgn.WC_wait(timePlay, core.cpgn.ActiveEffectOnEnnemy, core.DBEffects[EffectCurrEnnemy[j][i].idEffect], j);
							end
							timePlay = timePlay + 1;
						end
					else
						core.cpgn.WC_wait(timePlay, core.cpgn.ActiveEffectOnEnnemy, core.DBEffects[EffectCurrEnnemy[j][i].idEffect], j);
						timePlay = timePlay + 1;
					end
				end
			end
		end
	end
	for p = 1, #EffectCurrPlayer do
		if (EffectCurrPlayer[p] ~= nil and EffectCurrPlayer[p] ~= "nil" and SameNameEvent(nameEvent, core.DBEffects[EffectCurrPlayer[p].idEffect].whenActive) == true) then
			core.cpgn.WC_wait(timePlay, core.cpgn.ActiveEffectOnEnnemy, core.DBEffects[EffectCurrPlayer[p].idEffect], j);
			timePlay = timePlay + 1;
		end
	end
	return timePlay;
end

local function checkSameEffect(unitEffects, id)
	for i = 1, #unitEffects do
		if (unitEffects[i].idEffect == id) then
			return i;
		end
	end
	return 0;
end

local function AddEffectOnUnit(ennemyPos, effectId)
	local tmp = {
		idEffect = effectId,
		durationEffect = core.DBEffects[effectId].duration,
		chargeEffect = nil,
		EventCharge = nil,
	};
	if (core.DBEffects[effectId].charge ~= nil) then
		tmp.chargeEffect = core.DBEffects[effectId].charge;
	end
	if (core.DBEffects[effectId].EventChargeNumber ~= nil) then
		tmp.EventCharge = core.DBEffects[effectId].EventChargeNumber;
	end
	if (ennemyPos == 0) then
		if (#EffectCurrPlayer == 0) then
			EffectCurrPlayer[1] = tmp;
		else
			local res = nil;
			if (core.DBEffects[effectId].Cumulable == nil) then
				res = checkSameEffect(EffectCurrPlayer, tmp.idEffect);
			else
				res = 0;
			end
			if (res ~= 0) then
				EffectCurrPlayer[res].durationEffect = core.DBEffects[EffectCurrPlayer[res].idEffect].duration;
				if (EffectCurrPlayer[res].chargeEffect ~= nil) then
					EffectCurrPlayer[res].chargeEffect = EffectCurrPlayer[res].chargeEffect + 1;
					if (EffectCurrPlayer[res].chargeEffect == EffectCurrPlayer[res].EventCharge) then
						EventEffect("ChargeNumber"..EffectCurrPlayer[res].idEffect);
					end
				end
			else
				EffectCurrPlayer[#EffectCurrPlayer + 1] = tmp;
			end
		end
		_G["FSPlayerEffectText"]:SetText(core.cpgn.NbEffectsPlayer());
	else
		if (#EffectCurrEnnemy[ennemyPos] == 0) then
			EffectCurrEnnemy[ennemyPos][1] = tmp;
		else
			local res = checkSameEffect(EffectCurrEnnemy[ennemyPos], tmp.idEffect);
			if (res ~= 0) then
				EffectCurrEnnemy[ennemyPos][res].durationEffect = core.DBEffects[EffectCurrEnnemy[ennemyPos][res].idEffect].duration;
				if (EffectCurrEnnemy[ennemyPos][res].chargeEffect ~= nil) then
					EffectCurrEnnemy[ennemyPos][res].chargeEffect = EffectCurrEnnemy[ennemyPos][res].chargeEffect + 1;
					if (EffectCurrEnnemy[ennemyPos][res].chargeEffect == EffectCurrEnnemy[ennemyPos][res].EventCharge) then
						EventEffect("ChargeNumber");
					end
				end
			else
				EffectCurrEnnemy[ennemyPos][#EffectCurrEnnemy[ennemyPos] + 1] = tmp;
			end
		end
	end
	
end
core.cpgn.AddEffectOnUnit = AddEffectOnUnit;

local function ActiveEffectOnEnnemy(effect, posAI)
	if (SpellEnCours == false) then
		if (effect.idFctEffect == 0) then
			return;
		elseif (effect.idFctEffect == 1) then
			PdvCurrEnnemy[posAI] = PdvCurrEnnemy[posAI] - effect.damage;
			core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..posAI], effect.fxHimself, true);
			EventEffect("SufferDamageByDotHimself", posAI);
			EventEffect("SufferDamageAlly");
			if (PdvCurrEnnemy[posAI] <= 0) then
				PlaySound(currFight[posAI].sound.death, "sfx", false);
				if (currFight[posAI].typeUnit == "human") then
					_G["FightSceneEnnemy"..posAI]:SetAnimation(1);
				elseif (currFight[posAI].typeUnit == "object") then
					core.cpgn.WC_wait(0.5, core.cpgn.DieObjectUnit, _G["FightSceneEnnemy"..posAI]);
				end
				EventEffect("DieHimself", posAI);
				EventEffect("DieAlly");
				EffectCurrEnnemy[posAI] = {};
			else
				PlaySound(currFight[posAI].sound.hit, "sfx", false);
				_G["FightSceneEnnemy"..posAI]:SetAnimation(9);
				core.cpgn.WC_wait(0.50, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..posAI], currFight[posAI].animBase, posAI);
			end
			refreshEnnemyLifeFrame(posAI);
		elseif (effect.idFctEffect == 2 and unitIsStun(posAI) == false) then
			local randPos = randomBtwnEnnemyAlive();
			_G["FightSceneEnnemy"..posAI]:SetAnimation(effect.animation);
			core.cpgn.ManyPlaySound(effect.soundTarget);
			core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..posAI], effect.fxHimself, true);
			core.cpgn.AddEffectOnUnit(randPos, 4);
			core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..randPos], effect.fxTarget, true);
			core.cpgn.WC_wait(0.75, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..posAI], currFight[posAI].animBase, posAI);
		elseif (effect.idFctEffect == 3) then
			core.cpgn.ManyPlaySound(effect.soundTarget);
			core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], effect.fxTarget, true);
			core.cpgn.DamageToPlayer(effect.damage, false);
			MinusOneCharge(posAI, effect.id);
		elseif (effect.idFctEffect == 4 and unitIsStun(posAI) == false) then
			core.cpgn.ManyPlaySound(effect.soundCharac, "sfx");
			_G["FightSceneEnnemy"..posAI]:SetAnimation(effect.animation, effect.varAnim);
			core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..posAI], effect.fxHimself, true);
			local function afterHit(effect)
				core.cpgn.ManyPlaySound(effect.soundTarget);
				core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], effect.fxTarget, true);
				core.cpgn.DamageToPlayer(DamageCurrEnnemy[posAI], false);
			end
			core.cpgn.WC_wait(effect.timeAnim/2, afterHit, effect);
			core.cpgn.WC_wait(effect.timeAnim+0.2, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..posAI], currFight[posAI].animBase, posAI);
		elseif (effect.idFctEffect == 5) then
			dontCheckVictory = true;
			StaticPopup_Hide("FFfight");
			_G["FightSceneFFbtn"]:Disable();
			core.cpgn.WC_wait(2, SummonUnit, posAI, effect.idSummonUnit, effect.timeAnim, effect.soundTarget);
		elseif (effect.idFctEffect == 6) then
			core.cpgn.ManyPlaySound(effect.soundCharac, "sfx");
			core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..posAI], effect.fxHimself, true);
			DamageCurrEnnemy[posAI] = DamageCurrEnnemy[posAI] + effect.damage;
			refreshEnnemyDamageFrame(posAI);
		elseif (effect.idFctEffect == 7 and PdvCurrEnnemy[posAI] > 0) then
			core.cpgn.ManyPlaySound(effect.soundCharac, "sfx");
			core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..posAI], effect.fxHimself, true);
			DamageCurrEnnemy[posAI] = DamageCurrEnnemy[posAI] + effect.damage;
			PdvCurrEnnemy[posAI] = PdvCurrEnnemy[posAI] + effect.heal;
			refreshEnnemyLifeFrame(posAI);
			refreshEnnemyDamageFrame(posAI);
		elseif (effect.idFctEffect == 8) then
			core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], effect.fxHimself, true);
			core.cpgn.DamageToPlayer(effect.damage, false);
		elseif (effect.idFctEffect == 9) then
			local rand = math.random(1, 2);
			if (rand == 1) then
				core.cpgn.ManyPlaySound(effect.soundTarget);
				core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], effect.fxTarget, true);
				core.cpgn.DamageToPlayer(effect.damage, false);
			end
		elseif (effect.idFctEffect == 12) then
			dontCheckVictory = true;
			StaticPopup_Hide("FFfight");
			_G["FightSceneFFbtn"]:Disable();
			local rand = math.random(1, 5);
			if (rand == 1) then
				core.cpgn.WC_wait(2, SummonUnit, posAI, 5, effect.timeAnim, effect.soundTarget);
			elseif (rand == 2) then
				core.cpgn.WC_wait(2, SummonUnit, posAI, 7, effect.timeAnim, effect.soundTarget);
			elseif (rand == 3) then
				core.cpgn.WC_wait(2, SummonUnit, posAI, 8, effect.timeAnim, effect.soundTarget);
			elseif (rand == 4) then
				core.cpgn.WC_wait(2, SummonUnit, posAI, 9, effect.timeAnim, effect.soundTarget);
			elseif (rand == 5) then
				core.cpgn.WC_wait(2, SummonUnit, posAI, 10, effect.timeAnim, effect.soundTarget);
			end
		elseif (effect.idFctEffect == 13) then
			MinusOneCharge(0, effect.id);
			_G["FightScenePlayer"]:SetDisplayInfo(core.DBPlayer.displayId);
			_G["FightScenePlayer"]:SetAnimation(effect.animation);
			core.cpgn.ManyPlaySound(effect.soundCharac);
			_G["FightScenePlayer"]:EquipItem(core.DBPlayer.weaponId);
			_G["FightScenePlayer"]:EquipItem(core.DBPlayer.weaponId);
			core.cpgn.WC_wait(0.65, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0);
		elseif (effect.idFctEffect == 14 and unitIsStun(posAI) == false) then
			StaticPopup_Hide("FFfight");
			_G["FightSceneFFbtn"]:Disable();
			core.cpgn.ManyPlaySound(effect.soundCharac, "sfx", false);
			_G["FightSceneEnnemy"..posAI]:SetAnimation(effect.animation);
			core.cpgn.WC_wait(0.7, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..posAI], currFight[posAI].animBase, posAI);
			if (thereIsPlace(posAI)) then
				local rand = 0;
				local i = 0;
				while (i == 0) do
					local tmp = math.random(1, 5)
					if (tmp ~= 3 and (currFight[tmp] == 0 or PdvCurrEnnemy[tmp] <= 0)) then
						i = 1;
						rand = tmp;
					end
				end
				core.cpgn.WC_wait(0.5, SummonUnit, rand, effect.idSummonUnit, effect.timeAnim, effect.soundTarget);
			end
		elseif (effect.idFctEffect == 20 and unitIsStun(posAI) == false) then
			StaticPopup_Hide("FFfight");
			_G["FightSceneFFbtn"]:Disable();
			core.cpgn.ManyPlaySound(effect.soundCharac, "sfx", false);
			_G["FightSceneEnnemy"..posAI]:SetAnimation(effect.animation);
			core.cpgn.WC_wait(0.7, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..posAI], currFight[posAI].animBase, posAI);
			if (thereIsPlace(posAI)) then
				local rand = 0;
				local i = 0;
				while (i == 0) do
					local tmp = math.random(1, 5)
					if (tmp ~= 3 and (currFight[tmp] == 0 or PdvCurrEnnemy[tmp] <= 0)) then
						i = 1;
						rand = tmp;
					end
				end
				core.cpgn.WC_wait(0.5, SummonUnit, rand, effect.idSummonUnit, effect.timeAnim, effect.soundTarget);
			end
		elseif (effect.idFctEffect == 15) then
			core.cpgn.ManyPlaySound(effect.soundCharac);
			core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], effect.fxHimself, true);
			core.cpgn.WC_wait(0.5, core.cpgn.DamageToPlayer, effect.damage, false);
			MinusOneCharge(0, effect.id);
			MinusOneCharge(0, effect.id);
			MinusOneCharge(0, effect.id);
		elseif (effect.idFctEffect == 16) then
			if (PdvCurrEnnemy[posAI] <= 25) then
				SpellEnCours = true;
				EffectCurrEnnemy[posAI] = {},
				core.cpgn.ToggleCanPlayPlayerSpell(0);
				core.cpgn.ManyPlaySound(effect.soundCharac, "sfx");
				_G["FightSceneEnnemy"..posAI]:SetAnimation(effect.animation);
				core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..posAI], effect.fxHimself, true);
				local function afterHit(effect)
					local exceptionCleanBoard = {3};
					CleanBoard(exceptionCleanBoard);
					core.cpgn.ManyPlaySound(effect.soundTarget);
					core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], effect.fxTarget, true);
					core.cpgn.WC_wait(0.5, SummonUnit, 3, 15, 0, 0);
					_G["FightScenePlayer"]:SetAnimation(121);
					core.cpgn.WC_wait(1.25, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0);
					core.cpgn.ChangeBgFight("Interface/AddOns/WoWChallenger/images/campaign/bgFight_3_6_2");
				end
				core.cpgn.WC_wait(effect.timeAnim/2, afterHit, effect);
				core.cpgn.WC_wait(effect.timeAnim+2.75, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..posAI], 84, posAI);
				core.cpgn.WC_wait(effect.timeAnim+4.75, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..posAI], currFight[posAI].animBase, posAI);
				core.cpgn.WC_wait(effect.timeAnim+2.75, PlaySound, 45431, "sfx", false);
				core.cpgn.WC_wait(effect.timeAnim+3, SummonUnit, 1, effect.idSummonUnit, 2, 0);
				core.cpgn.WC_wait(effect.timeAnim+3, SummonUnit, 2, effect.idSummonUnit, 2, 0);
				core.cpgn.WC_wait(effect.timeAnim+3, SummonUnit, 4, effect.idSummonUnit, 2, 0);
				core.cpgn.WC_wait(effect.timeAnim+3, SummonUnit, 5, effect.idSummonUnit, 2, 0);
				core.cpgn.WC_wait(effect.timeAnim+3, setSpellEnCours, false);
				core.cpgn.WC_wait(effect.timeAnim+3, core.cpgn.BeginPlayerTurn);
			end
		elseif (effect.idFctEffect == 17) then
			local exception = {14};
			if (thereIsAnAlly(posAI, exception) == false) then
				core.cpgn.ManyPlaySound(effect.soundCharac, "sfx");
				core.cpgn.WC_wait(effect.timeAnim, SummonUnit, 1, 16, 2, 0);
				core.cpgn.WC_wait(effect.timeAnim, SummonUnit, 5, 16, 2, 0);
				core.cpgn.WC_wait(effect.timeAnim, SummonUnit, 3, 17, 2, 0);
				core.cpgn.WC_wait(effect.timeAnim, SummonUnit, 2, 5, 2, 0);
				core.cpgn.WC_wait(effect.timeAnim, SummonUnit, 4, 5, 2, 0);
			end
		elseif (effect.idFctEffect == 18) then
			PdvCurrEnnemy[posAI] = core.DBMobs[currFight[posAI].id].pdv;
			refreshEnnemyLifeFrame(posAI);
		elseif (effect.idFctEffect == 19) then
			local exception = {14};
			if (thereIsAnAlly(posAI, exception) == false) then
				SpellEnCours = true;
				EffectCurrEnnemy[posAI] = {},
				core.cpgn.ToggleCanPlayPlayerSpell(0);
				core.cpgn.ManyPlaySound(effect.soundCharac, "sfx");
				_G["FightSceneEnnemy"..posAI]:SetAnimation(effect.animation);
				core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..posAI], effect.fxHimself, true);
				local function afterHit(effect)
					local exceptionCleanBoard = {3};
					CleanBoard(exceptionCleanBoard);
					core.cpgn.ManyPlaySound(effect.soundTarget);
					core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], effect.fxTarget, true);
					core.cpgn.WC_wait(0.5, SummonUnit, 3, 18, 0, 0);
					_G["FightScenePlayer"]:SetAnimation(121);
					core.cpgn.WC_wait(2, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0, posAI);
					core.cpgn.ChangeBgFight("Interface/AddOns/WoWChallenger/images/campaign/bgFight_3_6_3");
				end
				core.cpgn.WC_wait(effect.timeAnim/2, afterHit, effect);
				core.cpgn.WC_wait(effect.timeAnim+3, setSpellEnCours, false);
				core.cpgn.WC_wait(effect.timeAnim+3, core.cpgn.BeginPlayerTurn);
			end
		elseif (effect.idFctEffect == 21) then
			local rand = math.random(1, 4);
			if (rand == 1) then
				core.cpgn.ManyPlaySound(effect.soundTarget);
				core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], effect.fxTarget, true);
				core.cpgn.DamageToPlayer(effect.damage, false);
			end
		elseif (effect.idFctEffect == 22) then
			_G["FightSceneEnnemy"..posAI]:SetAnimation(effect.animation);
			core.cpgn.ManyPlaySound(effect.soundCharac);
			core.cpgn.WC_wait(effect.timeAnim, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..posAI], currFight[posAI].animBase, posAI);
			MinusOneCharge(posAI, 26);
		elseif (effect.idFctEffect == 23) then
			core.cpgn.ManyPlaySound(effect.soundCharac);
			core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..posAI], effect.fxHimself, true);
			if (CdSpellCurrEnnemy[posAI][3].cdSpell > 0) then
				CdSpellCurrEnnemy[posAI][3].cdSpell = CdSpellCurrEnnemy[posAI][3].cdSpell - 1;
			end
		elseif (effect.idFctEffect == 24) then
			core.cpgn.ManyPlaySound(effect.soundCharac);
			core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..posAI], effect.fxHimself, true);
			if ((PdvCurrEnnemy[posAI] + effect.heal) > currFight[posAI].pdv) then
				PdvCurrEnnemy[posAI] = currFight[posAI].pdv;
			else
				PdvCurrEnnemy[posAI] = PdvCurrEnnemy[posAI] + effect.heal;
			end
			refreshEnnemyLifeFrame(posAI);
		elseif (effect.idFctEffect == 25) then
			core.cpgn.ManyPlaySound(effect.soundCharac);
			core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], effect.fxHimself, true);
			core.cpgn.WC_wait(0.5, core.cpgn.DamageToPlayer, effect.damage, false);
			MinusOneCharge(0, effect.id);
			MinusOneCharge(0, effect.id);
		end
		refreshCurrentEnnemyTooltip();
	end
end
core.cpgn.ActiveEffectOnEnnemy = ActiveEffectOnEnnemy;

local function checkUnitHasSpecificEffect(position, effectId)
	for i = 1, #EffectCurrEnnemy[position] do
		if (EffectCurrEnnemy[position][i].idEffect == effectId) then
			return true;
		end
	end
	return false;
end
core.cpgn.checkUnitHasSpecificEffect = checkUnitHasSpecificEffect;
----- END EFFECT FUNCTION -----

----- ANIMATION FUNCTION -----
local function setWaitAnimation(frameCharacter, animId, position)
	if (position == 0) then
		if (DBProfile.pdv > 0) then
			frameCharacter:SetAnimation(animId);
		end
	else
		if (PdvCurrEnnemy[position] > 0) then
			if (unitIsStun(position) == true) then
				frameCharacter:SetAnimation(14);
			else
				frameCharacter:SetAnimation(animId);
			end
		end
	end
end
core.cpgn.setWaitAnimation = setWaitAnimation;
----- END ANIMATION FUNCTION -----

local function BeginFight(self)
	if (DBProfile.pdv > 0) then
		_G["FightSceneFFbtn"]:Enable();
		zoneIdFight = self.zoneId;
		fightId = self.fightsId
		checkVictoryException = self.exceptionLevel;
		for i2 = 1, 5 do
			if (self.mobs[i2] ~= 0 and self.mobs[i2] ~= nil) then
				currFight[i2] = core.DBMobs[core.DBcampaign[self.zoneId].fights[self.fightsId].mobs[i2]];
				PdvCurrEnnemy[i2] = core.DBMobs[core.DBcampaign[self.zoneId].fights[self.fightsId].mobs[i2]].pdv;
				DamageCurrEnnemy[i2] = core.DBMobs[core.DBcampaign[self.zoneId].fights[self.fightsId].mobs[i2]].damage;
				for ispell = 1, #core.DBMobs[core.DBcampaign[self.zoneId].fights[self.fightsId].mobs[i2]].spells do
					local tmp = {
						idSpell = core.DBSpells[core.DBMobs[core.DBcampaign[self.zoneId].fights[self.fightsId].mobs[i2]].spells[ispell]].id,
						cdSpell = core.DBSpells[core.DBMobs[core.DBcampaign[self.zoneId].fights[self.fightsId].mobs[i2]].spells[ispell]].cooldown,
					};
					CdSpellCurrEnnemy[i2][ispell] = tmp;
				end
			else
				currFight[i2] = 0;
				PdvCurrEnnemy[i2] = "nil";
				DamageCurrEnnemy[i2] = "nil";
				CdSpellCurrEnnemy[i2] = "nil";
				_G["FSFrameTooltipEnnemy"..i2]:Hide();
			end
		end
		_G["FightScene"]:SetBackdrop({bgFile = core.DBcampaign[self.zoneId].fights[fightId].backgroundLevel,
								 edgeFile = "", 
								 tile = false, edgeSize = 16,
								 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
		PlaySound(core.DBcampaign[self.zoneId].fights[fightId].entrySound, "sfx");
		-- DISPLAY PLAYER MODEL
		DBProfile.damage = DBProfile.damageMax;
		_G["FightScenePlayer"]:SetDisplayInfo(core.DBPlayer.displayId);
		_G["FightScenePlayer"]:SetAnimation(core.DBPlayer.animBase);
		_G["FightScenePlayer"]:SetRotation(core.DBPlayer.rotation);
		if (UIParent:GetScale() < 0.8 and core.DBPlayer.positionLittle ~= nil) then
			_G["FightScenePlayer"]:SetPosition(core.DBPlayer.positionLittle[1], core.DBPlayer.positionLittle[2], core.DBPlayer.positionLittle[3])
		else
			_G["FightScenePlayer"]:SetPosition(core.DBPlayer.position[1], core.DBPlayer.position[2], core.DBPlayer.position[3])
		end
		_G["FightScenePlayer"]:Show();
		_G["FightScenePlayer"]:EquipItem(core.DBPlayer.weaponId);
		_G["FightScenePlayer"]:EquipItem(core.DBPlayer.weaponId);
		core.cpgn.refreshPlayerLifeFrame(_G["FSPlayerLifeText"], false);
		_G["FSPlayerLifeText"]:SetText(DBProfile.pdv);
		_G["FSPlayerDamageText"]:SetText(DBProfile.damage);
		core.cpgn.refreshPlayerDamageFrame(_G["FSPlayerDamageText"]);
		EffectCurrPlayer = {};
		_G["FSPlayerEffectText"]:SetText(core.cpgn.NbEffectsPlayer());
		-- DISPLAY ENEMY MODEL IN A SPECIFIC ORDER
		if (currFight[3] ~= 0) then
			core.cpgn.DisplayMobs(currFight[3], 3)
			_G["FSEnnemyDamageText3"]:SetText(DamageCurrEnnemy[3]);
			refreshEnnemyLifeFrame(3);
			refreshEnnemyDamageFrame(3);
		end
		if (currFight[2] ~= 0) then
			core.cpgn.DisplayMobs(currFight[2], 2)
			_G["FSEnnemyDamageText2"]:SetText(DamageCurrEnnemy[2]);
			refreshEnnemyLifeFrame(2);
			refreshEnnemyDamageFrame(2);
		end
		if (currFight[4] ~= 0) then
			core.cpgn.DisplayMobs(currFight[4], 4)
			_G["FSEnnemyDamageText4"]:SetText(DamageCurrEnnemy[4]);
			refreshEnnemyLifeFrame(4);
			refreshEnnemyDamageFrame(4);
		end
		if (currFight[1] ~= 0) then
			core.cpgn.DisplayMobs(currFight[1], 1)
			_G["FSEnnemyDamageText1"]:SetText(DamageCurrEnnemy[1]);
			refreshEnnemyLifeFrame(1);
			refreshEnnemyDamageFrame(1);
		end
		if (currFight[5] ~= 0) then
			core.cpgn.DisplayMobs(currFight[5], 5)
			_G["FSEnnemyDamageText5"]:SetText(DamageCurrEnnemy[5]);
			refreshEnnemyLifeFrame(5);
			refreshEnnemyDamageFrame(5);
		end
		EffectCurrEnnemy[1] = {};
		EffectCurrEnnemy[2] = {};
		EffectCurrEnnemy[3] = {};
		EffectCurrEnnemy[4] = {};
		EffectCurrEnnemy[5] = {};
		----- ADD PASSIF EFFECT WITHOUT SPELL -----
		for i = 1, #currFight do
			if (currFight[i] ~= 0 and currFight[i].effectPassif ~= nil) then
				for i2 = 1, #currFight[i].effectPassif do
					core.cpgn.AddEffectOnUnit(i, currFight[i].effectPassif[i2]);
				end
			end
		end
		----- ADD PASSIF EFFECT -----
		for i = 1, #currFight do
			if (currFight[i] ~= 0) then
				for i2 = 1, #currFight[i].spells do
					if (core.DBSpells[currFight[i].spells[i2]].cooldown == -1 and core.DBSpells[currFight[i].spells[i2]].effectId ~= nil) then
						core.cpgn.AddEffectOnUnit(i, core.DBSpells[currFight[i].spells[i2]].effectId);
					end
				end
			end
		end		
		
		local ipos = 1;
		for i = 1, core.libs.tablelength(core.DBPlayer.spells) do
			if (DBProfile.itemUnlock[3][i] == 0) then
				if (_G["FSPlayerSpell"..ipos] == nil) then
					local FSPlayerSpell = CreateFrame("Button", "FSPlayerSpell"..ipos, FightScene);
					FSPlayerSpell:SetSize(40, 40);
					if (ipos == 1) then
						FSPlayerSpell:SetPoint("BOTTOM", FightScene, "BOTTOM", 150, 50);
					elseif (ipos%6 == 0) then
						FSPlayerSpell:SetPoint("BOTTOM", _G["FSPlayerSpell"..(ipos - 5)], "TOP", 0, 10);	
					else
						FSPlayerSpell:SetPoint("RIGHT", _G["FSPlayerSpell"..(ipos - 1)], "RIGHT", 50, 0);
					end
				end
				_G["FSPlayerSpell"..ipos]:SetNormalTexture(core.DBSpells[core.DBPlayer.spells[i]].iconPath);
				_G["FSPlayerSpell"..ipos]:SetHighlightTexture(core.DBSpells[core.DBPlayer.spells[i]].iconPath);
				_G["FSPlayerSpell"..ipos].spell = core.DBSpells[core.DBPlayer.spells[i]];
				CdSpellPlayer[ipos] = 0;
				core.DBSpells[core.DBPlayer.spells[i]].positionId = ipos;
				
				local FrameSpellCD = CreateFrame("Button", "FSPlayerSpell"..ipos.."FrameSpellCD", FightScene);
				FrameSpellCD:SetSize(40, 40);
				FrameSpellCD:SetPoint("CENTER", _G["FSPlayerSpell"..ipos], "CENTER");
				FrameSpellCD:SetFrameLevel(13);
				
				FrameSpellCD.text = FrameSpellCD:CreateFontString("FSPlayerSpell"..ipos.."FrameSpellCDText", "OVERLAY", FrameSpellCD);
				FrameSpellCD.text:SetFontObject("GameFontHighlightLarge");
				FrameSpellCD.text:SetSize(FrameSpellCD:GetWidth(), 0);
				FrameSpellCD.text:SetText(CdSpellPlayer[ipos]);
				FrameSpellCD.text:SetPoint("CENTER", FrameSpellCD, "CENTER");
				
				FrameSpellCD.bg = FrameSpellCD:CreateTexture(nil, "BACKGROUND");
				FrameSpellCD.bg:SetAllPoints(true);
				FrameSpellCD.bg:SetColorTexture(0, 0, 0, 0.65);
				FrameSpellCD:Hide();
				
				_G["FSPlayerSpell"..ipos]:SetScript("OnEnter", OnEnterPlayerSpell);
				_G["FSPlayerSpell"..ipos]:SetScript("OnLeave", OnLeave);
				_G["FSPlayerSpell"..ipos]:SetScript("OnClick", core.cpgn.PlayerSpell_OnClick);
				ipos = ipos + 1;
			end
		end
		refreshPlayerSpellFrameForCD();
		
		if (zoneIdFight == 1 and fightId == 1 and DBProfile.showTuto == true) then
			core.cpgn.CreateFrameTuto();
		end
			
		_G["FightScene"]:Show();
		core.locationPlayer = 2;
		selectedSpell = 0;
		canPlay = 1;
		nbPlay = 1;
	else
		StaticPopup_Show("CantFight");
	end
end

local function setWaitRotation(frameCharacter, rotation)
	frameCharacter:SetRotation(rotation);
end

local function checkVictory()
	for i = 1, 5 do
		if (PdvCurrEnnemy[i] ~= "nil") then
			if (core.cpgn.findInArray(currFight[i].id, checkVictoryException) == false) then
				if (PdvCurrEnnemy[i] > 0) then
					return false;
				end
			end
		end
	end
	return true;
end

local function MinusOneSpellPlayerCD()
	if (RenverseThisLap == false) then
		for i = 1, #CdSpellPlayer do
			if (CdSpellPlayer[i] ~= 0) then
				CdSpellPlayer[i] = CdSpellPlayer[i] - 1;
			end
		end
	else
		RenverseThisLap = false;
	end
	refreshPlayerSpellFrameForCD();
end

----- AI PART -----
local function RegularAttackAI(positionAI, damage, spellId, animSpell, varAnim, positionSpell, soundAttackCharac, renverse, canBeDodge)
	core.cpgn.ManyPlaySound(soundAttackCharac, "sfx");
	if (varAnim ~= nil and varAnim ~= -1) then
		_G["FightSceneEnnemy"..positionAI]:SetAnimation(animSpell, varAnim);
	else
		_G["FightSceneEnnemy"..positionAI]:SetAnimation(animSpell);
	end
	core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..positionAI], core.DBSpells[spellId].fxHimself, true);
	local function afterHit(spell)
		EventEffect("HimselfAttacking", positionAI);
		EventEffect("AllyAttacking");
		if (core.DBSpells[spell].noFxOnPlayer == nil) then
			core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], core.DBSpells[spell].fxTarget, true);
		end
		core.cpgn.ManyPlaySound(core.DBSpells[spell].soundTarget);
		if (damage > 0) then
			core.cpgn.DamageToPlayer(damage, renverse);
		end
		if (renverse == true) then
			for i = 3, #CdSpellPlayer do
				if (CdSpellPlayer[i] ~= nil) then
					CdSpellPlayer[i] = CdSpellPlayer[i] + 1;
				end
			end
			RenverseThisLap = true;
			refreshPlayerSpellFrameForCD();
		end
	end
	local isEvent = 0;
	if (canBeDodge == true) then
		isEvent = EventEffect("BeforeSufferDamagePlayer");
	end
	if (isEvent == 0) then
		core.cpgn.WC_wait(core.DBSpells[spellId].timeAnim/2, afterHit, spellId);
	end
	CdSpellCurrEnnemy[positionAI][positionSpell].cdSpell = core.DBSpells[spellId].cooldown + 1;
end

----- ACTION MOB1 ----
local function refreshBgFrameTooltipEnnemy(position)
	for i = 1, 5 do
		_G["FSFrameTooltipEnnemy"..i]:SetBackdrop({bgFile = "",
							edgeFile = "", 
							tile = false, edgeSize = 16,
							insets = { left = -15, right = -15, top = 0, bottom = -6 }});
	end
	_G["FSFrameTooltipEnnemy"..position]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/bg_tooltipEnnemy",
							edgeFile = "", 
							tile = false, edgeSize = 16,
							insets = { left = -15, right = -15, top = 0, bottom = -6 }});
end

local function actionMob(position)					
	for i = #CdSpellCurrEnnemy[position], 1, -1 do
		if (CdSpellCurrEnnemy[position][i].cdSpell == 0) then
			for iAction = 1, #core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action do
				local function doAction()
					if (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "RegularAttack") then
						RegularAttackAI(position,
							DamageCurrEnnemy[position],
							CdSpellCurrEnnemy[position][i].idSpell,
							core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].animation,
							core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].varAnim,
							i,
							core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].soundCharac,
							core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].renverse,
							core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].canBeDodge);
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "RegularSpellAttack") then
						RegularAttackAI(position,
							core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].damage,
							CdSpellCurrEnnemy[position][i].idSpell,
							core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].animation,
							core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].varAnim,
							i,
							core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].soundCharac,
							core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].renverse,
							core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].canBeDodge);
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "AddEffect") then
						for j = 1, #core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction] do
							if (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j] == -1) then
								core.cpgn.AddEffectOnUnit(position, core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].effectId);
							elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j] == -2) then
								local rand = 0;
								local irand = 0;
								while (irand == 0) do
									local tmp = math.random(1, 5)
									if (currFight[tmp] ~= 0 and PdvCurrEnnemy[tmp] > 0) then
										irand = 1;
										rand = tmp;
									end
								end
								core.cpgn.AddEffectOnUnit(rand, core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].effectId);
							else
								core.cpgn.AddEffectOnUnit(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j], core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].effectId);
							end
						end
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "SetPdv") then
						for j = 1, #core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction] do
							if (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j] == -1) then
								PdvCurrEnnemy[position] = core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].setValue;
								refreshEnnemyLifeFrame(position);
							elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j] == 0) then
								DBPlayer.pdv = core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].setValue;
								refreshPlayerLifeFrame(_G["FSPlayerLifeText"], false)
							else
								PdvCurrEnnemy[core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j]] = core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].setValue;
								refreshEnnemyLifeFrame(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j]);
							end
						end
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "SetDamage") then
						for j = 1, #core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction] do
							if (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j] == -1) then
								DamageCurrEnnemy[position] = core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].setValue;
								refreshEnnemyDamageFrame(position);
							elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j] == 0) then
								DBProfile.damage = core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].setValue;
								core.cpgn.refreshPlayerDamageFrame(_G["FSPlayerDamageText"]);
							else
								DamageCurrEnnemy[core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j]] = core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].setValue;
								refreshEnnemyDamageFrame(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j]);
							end
						end
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "AddPdv") then
						for j = 1, #core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction] do
							if (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j] == -1) then
								PdvCurrEnnemy[position] = PdvCurrEnnemy[position] + core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].setValue;
								refreshEnnemyLifeFrame(position);
							elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j] == -2) then
									local rand = 0;
									local irand = 0;
									if (thereIsAnAlly(position, {11, 14, 19, 25})) then
										while (irand == 0) do
											local tmp = math.random(1, 5)
											if (currFight[tmp] ~= 0 and PdvCurrEnnemy[tmp] > 0 and tmp ~= position and currFight[tmp].typeUnit ~= "object") then
												irand = 1;
												rand = tmp;
											end
										end
										core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..rand], core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].fxTarget, true);
										PdvCurrEnnemy[rand] = PdvCurrEnnemy[rand] + core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].setValue;
										refreshEnnemyLifeFrame(rand);
									end
							elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j] == 0) then
								DBPlayer.pdv = DBPlayer.pdv + core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].setValue;
								refreshPlayerLifeFrame(_G["FSPlayerLifeText"], false)
							else
								PdvCurrEnnemy[core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j]] = PdvCurrEnnemy[core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j]] + core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].setValue;
								refreshEnnemyLifeFrame(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j]);
							end
						end
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "AddDamage") then
						for j = 1, #core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction] do
							if (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j] == -1) then
								DamageCurrEnnemy[position] = DamageCurrEnnemy[position] + core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].setValue;
								refreshEnnemyDamageFrame(position);
							elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j] == 0) then
								DBProfile.damage = DBProfile.damage + core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].setValue;
								core.cpgn.refreshPlayerDamageFrame(_G["FSPlayerDamageText"]);
							else
								DamageCurrEnnemy[core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j]] = DamageCurrEnnemy[core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j]] + core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].setValue;
								refreshEnnemyDamageFrame(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j]);
							end
						end
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "ResetCD") then
						CdSpellCurrEnnemy[position][i].cdSpell = core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].cooldown + 1;
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "Summon") then
						if (iAction == 1) then
							_G["FightSceneEnnemy"..position]:SetAnimation(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].animation);
							core.cpgn.ManyPlaySound(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].soundCharac, "sfx");
						end
						for j = 1, #core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction] do
							if (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j] == -2) then
									if (thereIsPlace(position)) then
										local rand = 0;
										local irand = 0;
										while (irand == 0) do
											local tmp = math.random(1, 5)
											if (tmp ~= position and (currFight[tmp] == 0 or PdvCurrEnnemy[tmp] <= 0)) then
												irand = 1;
												rand = tmp;
											end
										end
										if (#core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].idSummonUnit > 1) then
											local iRandSummon = math.random(1, #core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].idSummonUnit);
											core.cpgn.WC_wait(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].timeAnim, SummonUnit, rand, core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].idSummonUnit[iRandSummon], 0, 0);									
										else
											core.cpgn.WC_wait(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].timeAnim, SummonUnit, rand, core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].idSummonUnit[1], 0, 0);
										end
									end
							else
								if (#core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].idSummonUnit > 1) then
									local iRandSummon = math.random(1, #core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].idSummonUnit);
									core.cpgn.WC_wait(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].timeAnim, SummonUnit, core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j], core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].idSummonUnit[iRandSummon], 0, 0);
								else
									core.cpgn.WC_wait(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].timeAnim, SummonUnit, core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].targetAction[iAction][j], core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].idSummonUnit[1], 0, 0);
								end
							end
						end
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "DieObject") then
						core.cpgn.WC_wait(1.15, core.cpgn.DieObjectUnit, _G["FightSceneEnnemy"..position]);
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "Custom1") then
						if (DBProfile.pdv < 40 and DBProfile.pdv > 0 and unitIsStun(position) == false) then
							core.cpgn.ManyPlaySound(core.DBSpells[19].soundCharac);
							_G["FightSceneEnnemy"..position]:SetAnimation(110);
							_G["FightScenePlayer"]:ApplySpellVisualKit(40688, true);
							local function afterHit(spell)
								_G["FightSceneEnnemy"..position]:SetAnimation(core.DBSpells[19].animation);
								DBProfile.pdv = DBProfile.pdv - core.DBSpells[19].damage;
								core.cpgn.ManyPlaySound(core.DBSpells[spell].soundTarget);
								core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], core.DBSpells[spell].fxTarget, true);
								PlaySound(core.DBPlayer.sound.death, "sfx", false);
								_G["FightScenePlayer"]:SetAnimation(1);
								core.cpgn.refreshPlayerLifeFrame(_G["FSPlayerLifeText"], false);
							end
							core.cpgn.WC_wait(core.DBSpells[19].timeAnim/2, afterHit, 19);
						else
							CdSpellCurrEnnemy[position][i].cdSpell = core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].cooldown + 1;
							actionMob(position);
						end
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "Custom2") then
						if (position - 1 >= 1 and currFight[position - 1] ~= 0 and currFight[position - 1].id == 16) then
							if (core.cpgn.checkUnitHasSpecificEffect(position - 1, 19)) then
								MinusOneCharge(position - 1, 19);
								PlaySound(13092, "sfx", false);
								_G["FightSceneEnnemy"..position - 1]:ApplySpellVisualKit(40707, true);
							end
						end
						if (position + 1 <= 5 and currFight[position + 1] ~= 0 and currFight[position + 1].id == 16) then
							if (core.cpgn.checkUnitHasSpecificEffect(position + 1, 19)) then
								MinusOneCharge(position + 1, 19);
								PlaySound(13092, "sfx", false);
								_G["FightSceneEnnemy"..position + 1]:ApplySpellVisualKit(40707, true);
							end
						end
						if (currFight[3].id == 18) then
							core.cpgn.WC_wait(1.25, SummonUnit, position, 19, 0, 0);
						end
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "Custom3") then
						for i = 1, 5 do
							if (currFight[i] ~= 0 and currFight[i].id == 19) then
								core.cpgn.ManyPlaySound(core.DBSpells[CdSpellCurrEnnemy[i][1].idSpell].soundCharac);
								core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..i], core.DBSpells[CdSpellCurrEnnemy[i][1].idSpell].fxHimself, true);
								core.cpgn.DamageToPlayer(core.DBSpells[currFight[i].spells[1]].damage, false);
								PdvCurrEnnemy[i] = 0;
								refreshEnnemyLifeFrame(i);
								core.cpgn.WC_wait(0.5, core.cpgn.DieObjectUnit, _G["FightSceneEnnemy"..i]);
							end
						end
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "Custom4") then
						for ithrow = 1, 3 do
							local function throw()
								_G["FightSceneEnnemy"..position]:SetAnimation(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].animation);
								core.cpgn.ManyPlaySound(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].soundCharac, "sfx");
								core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..position], core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].fxHimself, true);
								local function afterHit(spell)
									EventEffect("HimselfAttacking", position);
									EventEffect("AllyAttacking");
									if (core.DBSpells[spell].damage > 0) then
										core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], core.DBSpells[spell].fxTarget, true);
										core.cpgn.ManyPlaySound(core.DBSpells[spell].soundTarget);
										core.cpgn.DamageToPlayer(core.DBSpells[spell].damage, false);
									end
								end
								core.cpgn.WC_wait(0.3, afterHit, CdSpellCurrEnnemy[position][i].idSpell);
							end
							core.cpgn.WC_wait(ithrow/2.5, throw);
						end
						CdSpellCurrEnnemy[position][i].cdSpell = core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].cooldown + 1;
					elseif (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].action[iAction] == "Custom5") then
						local rand = 0;
						local irand = 0;
						if (thereIsAnAlly(position, {11, 14, 19, 25})) then
							while (irand == 0) do
								local tmp = math.random(1, 5)
								if (currFight[tmp] ~= 0 and PdvCurrEnnemy[tmp] > 0 and tmp ~= position and currFight[tmp].typeUnit ~= "object") then
									irand = 1;
									rand = tmp;
								end
							end
							core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..rand], core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].fxTarget, true);
							core.cpgn.AddEffectOnUnit(rand, core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].effectId);
						end
						core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..position], core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].fxHimself, true);
						_G["FightSceneEnnemy"..position]:SetAnimation(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].animation);
						core.cpgn.ManyPlaySound(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].soundTarget, "sfx");
					end
					refreshCurrentEnnemyTooltip();
				end
				if (core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].timeWait ~= nil) then
					core.cpgn.WC_wait(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].timeWait[iAction], doAction);				
				else
					core.cpgn.WC_wait(0, doAction);
				end
			end
			core.cpgn.WC_wait(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].timeAnim+0.2, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..position], currFight[position].animBase, position);
			return;
		end
	end
end

----- ACTION MOBS END -----
local function checkIfUnitCanPlay(position)
	for i = 1, #CdSpellCurrEnnemy[position] do
		if (CdSpellCurrEnnemy[position][i].cdSpell == 0) then
			return true;
		end
	end
	return false;
end

local function PlayerWin()
	canPlay = 0;
	nbPlay = 1;
	PlaySound("118535", "sfx", false);
	if (zoneIdFight == 1) then
		if (Campaign1[fightId].done == false) then
			HealPlayerWhenWin();
		end
	elseif (zoneIdFight == 2) then
		if (Campaign2[fightId].done == false) then
			HealPlayerWhenWin();
		end
	elseif (zoneIdFight == 3) then
		if (Campaign3[fightId].done == false) then
			HealPlayerWhenWin();
		end
	end
	StaticPopup_Show("VictoryPopup");
end

local function BeginPlayerTurn()
	if (SpellEnCours == false) then
		canPlay = 1;
		nbPlay = 1;
		_G["FSTurnText"]:SetText(core.L["YouTurn"]);
		_G["FSTurnText"]:SetFontObject("GameFontNormalLarge");
		if (DBProfile.pdv <= 0) then
			canPlay = 0;
			PlaySound("31750", "sfx", false);
			PlaySound(core.DBcampaign[zoneIdFight].fights[fightId].looseSound, "sfx", false);
			if (DBProfile.skillPoints == 0) then
				StaticPopup_Show("DefeatPopupNoSkill");
			else
				StaticPopup_Show("DefeatPopup");
			end
		elseif (checkVictory() and dontCheckVictory == false) then
			PlayerWin()
		else
			_G["FightSceneFFbtn"]:Enable();
			MinusOneEffect();
			MinusOneSpellPlayerCD();
		end
		refreshCurrentEnnemyTooltip();
	end
end
core.cpgn.BeginPlayerTurn = BeginPlayerTurn;

local function EndAITurn()
	if (_G["FSFrameTooltipEnnemy5"] ~= nil) then
		_G["FSFrameTooltipEnnemy5"]:SetBackdrop({bgFile = "",
							edgeFile = "", 
							tile = false, edgeSize = 16,
							insets = { left = -15, right = -15, top = 0, bottom = -6 }});
	end
	--- '-1' lap for spell ---
	for j = 1, 5 do
		for iLapSpell = 1, #CdSpellCurrEnnemy[j] do
			if (currFight[j] ~= 0) then
				if (PdvCurrEnnemy[j] > 0) then
					if (CdSpellCurrEnnemy[j][iLapSpell].cdSpell > 0) then
						CdSpellCurrEnnemy[j][iLapSpell].cdSpell = CdSpellCurrEnnemy[j][iLapSpell].cdSpell - 1
					end
				end
			end
		end
	end
	local timeEffect = 0
	if (DBProfile.pdv > 0) then
		timeEffect = EventEffect("EndAllEnnemyTurn")
	end
	core.cpgn.WC_wait(timeEffect, core.cpgn.BeginPlayerTurn);
end

local function AITurn()
	StaticPopup_Hide("FFfight");
	_G["FightSceneFFbtn"]:Disable();
	_G["FSTurnText"]:SetText(core.L["EnemyTurn"]);
	_G["FSTurnText"]:SetFontObject("GameFontDisableLarge");
	local timePlay = 1.5; 
	if (currFight[1] ~= 0) then
		if (PdvCurrEnnemy[1] > 0 and unitIsStun(1) == false and checkIfUnitCanPlay(1)) then
			core.cpgn.WC_wait(timePlay, actionMob, 1);
			timePlay = timePlay + 2;
		end
	end
	if (currFight[2] ~= 0) then
		if (PdvCurrEnnemy[2] > 0 and unitIsStun(2) == false and checkIfUnitCanPlay(2)) then
			core.cpgn.WC_wait(timePlay, actionMob, 2);
			timePlay = timePlay + 2;
		end
	end
	if (currFight[3] ~= 0) then
		if (PdvCurrEnnemy[3] > 0 and unitIsStun(3) == false and checkIfUnitCanPlay(3)) then
			core.cpgn.WC_wait(timePlay, actionMob, 3);
			timePlay = timePlay + 2;
		end
	end
	if (currFight[4] ~= 0) then
		if (PdvCurrEnnemy[4] > 0 and unitIsStun(4) == false and checkIfUnitCanPlay(4)) then
			core.cpgn.WC_wait(timePlay, actionMob, 4);
			timePlay = timePlay + 2;
		end
	end
	if (currFight[5] ~= 0) then
		if (PdvCurrEnnemy[5] > 0 and unitIsStun(5) == false and checkIfUnitCanPlay(5)) then
			core.cpgn.WC_wait(timePlay, actionMob, 5);
			timePlay = timePlay + 2;
		end
	end
	core.cpgn.WC_wait(timePlay, EndAITurn);
end	
----- AI PART END -----

local function CancelSpellPlayer(self)
	self:SetNormalTexture(self.spell.iconPath);
	self:SetHighlightTexture(self.spell.iconPath);
	self:SetScript("OnEnter", OnEnterPlayerSpell);
	OnEnterPlayerSpell(self);
	selectedSpell = 0;
end

local function endTurnPlayer(abandon, ...)
	for i = 1, core.libs.tablelength(core.DBPlayer.spells) do
		if (_G["FSPlayerSpell"..i] ~= nil) then
			_G["FSPlayerSpell"..i]:SetNormalTexture(_G["FSPlayerSpell"..i].spell.iconPath);
			_G["FSPlayerSpell"..i]:SetHighlightTexture(_G["FSPlayerSpell"..i].spell.iconPath);
			_G["FSPlayerSpell"..i]:SetScript("OnEnter", OnEnterPlayerSpell);
		end
	end
	selectedSpell = 0;
	if (abandon == false) then
		refreshPlayerSpellFrameForCD();
		if (... == nil) then
			AITurn();
		else
			canPlay = 1;
		end
	end
end

local function checkSamePosSpells(activeSpells, i)
	if (activeSpells == nil) then
		return false;
	end
	for j = 1, #activeSpells do
		if (i == activeSpells[j]) then
			return true;
		end
	end
	return false;
end


local function DisabledSpells(...)
	local activeSpells = {...};
	for i = 1, #core.DBPlayer.spells do
		if (checkSamePosSpells(activeSpells, i)) then
			if (_G["FSPlayerSpell"..i.."FrameSpellCD"] ~= nil) then
				_G["FSPlayerSpell"..i.."FrameSpellCD"]:Hide();
			end
		else
			if (_G["FSPlayerSpell"..i.."FrameSpellCD"] ~= nil) then
				_G["FSPlayerSpell"..i.."FrameSpellCD"]:Show();
				_G["FSPlayerSpell"..i.."FrameSpellCDText"]:Hide();
			end
		end
	end
end

local function ActiveSpells(...)
	local activeSpells = {...};
	for i = 1, #core.DBPlayer.spells do
		if (checkSamePosSpells(activeSpells, i)) then
			_G["FSPlayerSpell"..i.."FrameSpellCD"]:Show();
			_G["FSPlayerSpell"..i.."FrameSpellCDText"]:Hide();
		else
			_G["FSPlayerSpell"..i.."FrameSpellCD"]:Hide();
		end
	end
end

local function changeCanPlayValue(value)
	canPlay = value;
end

local function AddNbPlay()
	nbPlay = nbPlay + 1;
end

local function InteractSpellPlayer(self)
	if (selectedSpell ~= 0 and canPlay == 1) then
		if (core.DBSpells[selectedSpell].target == true) then
			if (currFight[self.position] ~= 0) then
				if (PdvCurrEnnemy[self.position] > 0) then
					canPlay = 0;
					if (self.position == 1) then
						_G["FightScenePlayer"]:SetRotation(3.5);
					elseif (self.position == 2) then
						_G["FightScenePlayer"]:SetRotation(3.25);
					elseif (self.position == 3) then
						_G["FightScenePlayer"]:SetRotation(3);
					elseif (self.position == 4) then
						_G["FightScenePlayer"]:SetRotation(2.5);
					elseif (self.position == 5) then
						_G["FightScenePlayer"]:SetRotation(2.3);
					end
					if (selectedSpell == 4) then
						core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundCharac, "sfx");
						_G["FightScenePlayer"]:SetAnimation(core.DBSpells[selectedSpell].animation);
						local function afterHit()
							local isUnderAttackEffect = 0;
							local isUnderAttackEffect2 = 0;
							local Custom1 = 0;
							if (unitIsStun(self.position) == false) then
								isUnderAttackEffect = EventEffect("UnderAttackHimself", self.position);
								isUnderAttackEffect2 = EventEffect("UnderAttackAlly");
								Custom1 = EventEffect("CustomParadeDuelliste", self.position);		
							end
							local isPassifEffectvar = 0;
							local isDieEffectvar = 0;
							if (isUnderAttackEffect == 0 and isUnderAttackEffect2 == 0 and Custom1 == 0) then
								PdvCurrEnnemy[self.position] = PdvCurrEnnemy[self.position] - DBProfile.damage;
								core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundTarget);
								core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..self.position], core.DBSpells[selectedSpell].fxTarget, true);
								refreshEnnemyLifeFrame(self.position);
								EventEffect("SufferDamageHimself", self.position);
								EventEffect("SufferDamageAlly");
								if (PdvCurrEnnemy[self.position] > 0) then
									isPassifEffectvar = EventEffect("PASSIFSufferDamageHimself", self.position);
								end
								if (PdvCurrEnnemy[self.position] <= 0) then
									PlaySound(currFight[self.position].sound.death, "sfx", false);
									if (currFight[self.position].typeUnit == "human") then
										_G["FightSceneEnnemy"..self.position]:SetAnimation(1);
									elseif (currFight[self.position].typeUnit == "object") then
										core.cpgn.WC_wait(0.5, core.cpgn.DieObjectUnit, _G["FightSceneEnnemy"..self.position]);
									end
									isDieEffectvar = EventEffect("DieHimself", self.position);
									EventEffect("DieAlly");
									EffectCurrEnnemy[self.position] = {};
								else
									PlaySound(currFight[self.position].sound.hit, "sfx", false);
									_G["FightSceneEnnemy"..self.position]:SetAnimation(9);
									if (isPassifEffectvar == 0 or unitIsStun(self.position) == true) then
										core.cpgn.WC_wait(0.50, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..self.position], currFight[self.position].animBase, self.position);
									end
								end
							end	
							
								core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim+0.2, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0);
							if (isDieEffectvar == 0) then
								endTurnPlayer(false);
							else
								core.cpgn.WC_wait(3, endTurnPlayer, false);	
							end
						end
						core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim/2, afterHit);
					elseif (selectedSpell == 9) then
						core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundCharac, "sfx");
						_G["FightScenePlayer"]:SetAnimation(core.DBSpells[selectedSpell].animation);
						local function afterHit()
							local isUnderAttackEffect = 0;
							local isUnderAttackEffect2 = 0;
							local Custom1 = 0;
							if (unitIsStun(self.position) == false) then
								isUnderAttackEffect = EventEffect("UnderAttackHimself", self.position);
								isUnderAttackEffect2 = EventEffect("UnderAttackAlly");
								Custom1 = EventEffect("CustomParadeDuelliste", self.position);
							end
							local isPassifEffectvar = 0;
							local isDieEffectvar = 0;
							if (isUnderAttackEffect == 0 and isUnderAttackEffect2 == 0 and Custom1 == 0) then
								PdvCurrEnnemy[self.position] = PdvCurrEnnemy[self.position] - DBProfile.damage;
								core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundTarget);
								core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..self.position], core.DBSpells[selectedSpell].fxTarget, true);
								refreshEnnemyLifeFrame(self.position);
								EventEffect("SufferDamageHimself", self.position);
								EventEffect("SufferDamageAlly");
								if (PdvCurrEnnemy[self.position] > 0) then
									isPassifEffectvar = EventEffect("PASSIFSufferDamageHimself", self.position);
								end
								if (PdvCurrEnnemy[self.position] <= 0) then
									PlaySound(currFight[self.position].sound.death, "sfx", false);
									if (currFight[self.position].typeUnit == "human") then
										_G["FightSceneEnnemy"..self.position]:SetAnimation(1);
									elseif (currFight[self.position].typeUnit == "object") then
										core.cpgn.WC_wait(0.5, core.cpgn.DieObjectUnit, _G["FightSceneEnnemy"..self.position]);
									end
									isDieEffectvar = EventEffect("DieHimself", self.position);
									EventEffect("DieAlly");
									EffectCurrEnnemy[self.position] = {};
								else
									PlaySound(currFight[self.position].sound.hit, "sfx", false);
									_G["FightSceneEnnemy"..self.position]:SetAnimation(9);
									core.cpgn.AddEffectOnUnit(self.position, core.DBSpells[selectedSpell].effectId);
									if (isPassifEffectvar == 0 or unitIsStun(self.position) == true) then
										core.cpgn.WC_wait(0.50, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..self.position], currFight[self.position].animBase, self.position);
									end
								end
							end
							CdSpellPlayer[core.DBSpells[selectedSpell].positionId] = core.DBSpells[selectedSpell].cooldown;
								core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim+0.2, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0);
							if (isDieEffectvar == 0) then
								endTurnPlayer(false);
							else
								core.cpgn.WC_wait(3, endTurnPlayer, false);	
							end
						end
						core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim/3, afterHit);
					elseif (selectedSpell == 10) then
						core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundCharac, "sfx");
						_G["FightScenePlayer"]:SetAnimation(core.DBSpells[selectedSpell].animation);
						local function afterHit()
							local isUnderAttackEffect = 0;
							local isUnderAttackEffect2 = 0;
							if (unitIsStun(self.position) == false) then
								isUnderAttackEffect = EventEffect("UnderAttackHimself", self.position);
								isUnderAttackEffect2 = EventEffect("UnderAttackAlly");
							end
							if (isUnderAttackEffect == 0 and isUnderAttackEffect2 == 0) then
								core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundTarget);
								core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..self.position], core.DBSpells[selectedSpell].fxTarget, true);
								_G["FightSceneEnnemy"..self.position]:SetAnimation(14);
								core.cpgn.AddEffectOnUnit(self.position, core.DBSpells[selectedSpell].effectId);
							end
							CdSpellPlayer[core.DBSpells[selectedSpell].positionId] = core.DBSpells[selectedSpell].cooldown;
							endTurnPlayer(false, "CanStartOver");
						end
						core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim/3, afterHit);
						core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0);
					elseif (selectedSpell == 43) then
						core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundCharac, "sfx");
						_G["FightScenePlayer"]:SetAnimation(core.DBSpells[selectedSpell].animation);
						local function afterHit()
							core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundTarget);
							local isUnderAttackEffect = 0;
							local isUnderAttackEffect2 = 0;
							local Custom1 = 0;
							if (unitIsStun(self.position) == false) then
								isUnderAttackEffect = EventEffect("UnderAttackHimself", self.position);
								isUnderAttackEffect2 = EventEffect("UnderAttackAlly");
								Custom1 = EventEffect("CustomParadeDuelliste", self.position);
							end
							if (isUnderAttackEffect == 0 and isUnderAttackEffect2 == 0 and Custom1 == 0) then
								_G["FightSceneEnnemy"..self.position]:SetAnimation(121);
								for i = 1, #CdSpellCurrEnnemy[self.position] do
									if (core.DBSpells[CdSpellCurrEnnemy[self.position][i].idSpell].BasicAttack == nil and CdSpellCurrEnnemy[self.position][i].cdSpell ~= -1) then
										CdSpellCurrEnnemy[self.position][i].cdSpell = CdSpellCurrEnnemy[self.position][i].cdSpell + 1;
									end
								end
								refreshCurrentEnnemyTooltip();
								core.cpgn.WC_wait(1.5, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..self.position], currFight[self.position].animBase, self.position);
							end
							CdSpellPlayer[core.DBSpells[selectedSpell].positionId] = core.DBSpells[selectedSpell].cooldown;
							endTurnPlayer(false, "CanStartOver");
						end
						core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim/3, afterHit);
						core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0);
					elseif (selectedSpell == 32) then
						DisabledSpells(core.DBSpells[selectedSpell].positionId);
						core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundCharac, "sfx");
						_G["FightScenePlayer"]:SetAnimation(core.DBSpells[selectedSpell].animation);
						local function afterHit()
							local isUnderAttackEffect = 0;
							local isUnderAttackEffect2 = 0;
							local Custom1 = 0;
							if (unitIsStun(self.position) == false) then
								isUnderAttackEffect = EventEffect("UnderAttackHimself", self.position);
								isUnderAttackEffect2 = EventEffect("UnderAttackAlly");
								Custom1 = EventEffect("CustomParadeDuelliste", self.position);
							end
							local isPassifEffectvar = 0;
							local isDieEffectvar = 0;
							if (isUnderAttackEffect == 0 and isUnderAttackEffect2 == 0 and Custom1 == 0) then
								PdvCurrEnnemy[self.position] = PdvCurrEnnemy[self.position] - DBProfile.damage;
								core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundTarget);
								core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..self.position], core.DBSpells[selectedSpell].fxTarget, true);
								refreshEnnemyLifeFrame(self.position);
								EventEffect("SufferDamageHimself", self.position);
								EventEffect("SufferDamageAlly");
								if (PdvCurrEnnemy[self.position] > 0) then
									isPassifEffectvar = EventEffect("PASSIFSufferDamageHimself", self.position);
								end
								if (PdvCurrEnnemy[self.position] <= 0) then
									PlaySound(currFight[self.position].sound.death, "sfx", false);
									if (currFight[self.position].typeUnit == "human") then
										_G["FightSceneEnnemy"..self.position]:SetAnimation(1);
									elseif (currFight[self.position].typeUnit == "object") then
										core.cpgn.WC_wait(0.5, core.cpgn.DieObjectUnit, _G["FightSceneEnnemy"..self.position]);
									end
									isDieEffectvar = EventEffect("DieHimself", self.position);
									EventEffect("DieAlly");
									EffectCurrEnnemy[self.position] = {};
								else
									PlaySound(currFight[self.position].sound.hit, "sfx", false);
									_G["FightSceneEnnemy"..self.position]:SetAnimation(9);
									if (isPassifEffectvar == 0 or unitIsStun(self.position) == true) then
										core.cpgn.WC_wait(0.50, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..self.position], currFight[self.position].animBase, self.position);
									end
								end
							end
								core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim+0.2, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0);
							canPlay = 1;
							if (isDieEffectvar == 0 and nbPlay == 2) then
								canPlay = 0;
								CdSpellPlayer[core.DBSpells[selectedSpell].positionId] = core.DBSpells[selectedSpell].cooldown;
								endTurnPlayer(false);
							elseif (nbPlay == 2) then
								canPlay = 0;
								CdSpellPlayer[core.DBSpells[selectedSpell].positionId] = core.DBSpells[selectedSpell].cooldown;
								core.cpgn.WC_wait(3, endTurnPlayer, false);	
							end
							if (isDieEffectvar == 0 and checkVictory() and dontCheckVictory == false and nbPlay == 1) then
								PlayerWin();	
							end
							nbPlay = nbPlay + 1;
						end
						core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim/2, afterHit);
					end
					core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim+0.2, setWaitRotation, _G["FightScenePlayer"], core.DBPlayer.rotation);
				end
			end
		elseif (core.DBSpells[selectedSpell].target == false) then
			canPlay = 0;
			if (selectedSpell == 6) then
				core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundCharac, "sfx");
				_G["FightScenePlayer"]:SetAnimation(core.DBSpells[selectedSpell].animation);
				local function afterHit()
					core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundTarget);
					core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], core.DBSpells[selectedSpell].fxHimself, true);
					if ((DBProfile.pdv + core.DBSpells[selectedSpell].damage) > DBProfile.pdvMax) then
						DBProfile.pdv = DBProfile.pdvMax;
					else
						DBProfile.pdv = DBProfile.pdv + core.DBSpells[selectedSpell].damage;
					end
					core.cpgn.refreshPlayerLifeFrame(_G["FSPlayerLifeText"], false);
					core.cpgn.WC_wait(core.DBSpells[6].timeAnim, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0);
					endTurnPlayer(false);
				end
				core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim/2, afterHit);
				core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0);
			elseif (selectedSpell == 11) then
				_G["FightScenePlayer"]:SetAnimation(core.DBSpells[selectedSpell].animation);
				core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundCharac);
				core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], core.DBSpells[selectedSpell].fxHimself, true);
				local isPassifEffectvar = 0;
				local isDieEffectvar = 0;
				local isUnderAttackEffect = 0;
				local isUnderAttackEffect2 = 0;
				local Custom1 = 0;
				local thereIsDieEffect = false;
				local noOneStun = true;
				for i = 1, 5 do
					if (currFight[i] ~= 0 and PdvCurrEnnemy[i] > 0) then
						if (unitIsStun(i) == false) then
							isUnderAttackEffect = EventEffect("UnderAttackHimself", i);
							isUnderAttackEffect2 = EventEffect("UnderAttackAlly");
							Custom1 = EventEffect("CustomParadeDuelliste", i);
						end
						if (isUnderAttackEffect == 0 and isUnderAttackEffect2 == 0 and Custom1 == 0) then
							PdvCurrEnnemy[i] = PdvCurrEnnemy[i] - core.DBSpells[selectedSpell].damage;
							core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundTarget);
							core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneEnnemy"..i], core.DBSpells[selectedSpell].fxTarget, true);
							EventEffect("SufferDamageHimself", i);
							EventEffect("SufferDamageAlly");
							if (PdvCurrEnnemy[i] > 0) then
								isPassifEffectvar = EventEffect("PASSIFSufferDamageHimself", i);
							end
							if (PdvCurrEnnemy[i] <= 0) then
								PlaySound(currFight[i].sound.death, "sfx", false);
								if (currFight[i].typeUnit == "human") then
									_G["FightSceneEnnemy"..i]:SetAnimation(1);
								elseif (currFight[i].typeUnit == "object") then
									core.cpgn.WC_wait(0.25, core.cpgn.DieObjectUnit, _G["FightSceneEnnemy"..i]);
								end
								isDieEffectvar = EventEffect("DieHimself", i);
								EventEffect("DieAlly");
								if (thereIsDieEffect == false and isDieEffectvar ~= 0) then
									thereIsDieEffect = true;
								end							
								EffectCurrEnnemy[i] = {};
							else
								PlaySound(currFight[i].sound.hit, "sfx", false);
								_G["FightSceneEnnemy"..i]:SetAnimation(9);
								if (isPassifEffectvar == 0 or unitIsStun(i) == true) then
									core.cpgn.WC_wait(0.50, core.cpgn.setWaitAnimation, _G["FightSceneEnnemy"..i], currFight[i].animBase, i);
								end
							end
							refreshEnnemyLifeFrame(i);
						end
					end
				end
				CdSpellPlayer[core.DBSpells[selectedSpell].positionId] = core.DBSpells[selectedSpell].cooldown;
				core.cpgn.WC_wait(core.DBSpells[11].timeAnim, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0);
				if (thereIsDieEffect == false) then
					endTurnPlayer(false);
				else
					core.cpgn.WC_wait(3, endTurnPlayer, false);
				end
			end
		end
	end
end

local function PlayerSpell_OnClick(self)
	selectedSpellAnytime = self.spell.id;
	if (core.DBSpells[selectedSpellAnytime].anytime ~= nil) then
		if (CdSpellPlayer[core.DBSpells[self.spell.id].positionId] == 0) then
			selectedSpellAnytime = self.spell.id;
			PlaySound("83", "sfx", false);
			if (selectedSpellAnytime == 33) then
				core.cpgn.ManyPlaySound(core.DBSpells[selectedSpellAnytime].soundCharac, "sfx");
				_G["FightScenePlayer"]:SetAnimation(core.DBSpells[selectedSpellAnytime].animation);
				core.cpgn.ApplySpellVisualOnUnit(_G["FightScenePlayer"], core.DBSpells[selectedSpellAnytime].fxHimself, true);
				core.cpgn.AddEffectOnUnit(0, core.DBSpells[selectedSpellAnytime].effectId);
				_G["FSPlayerEffectText"]:SetText(core.cpgn.NbEffectsPlayer());
				CdSpellPlayer[core.DBSpells[selectedSpellAnytime].positionId] = core.DBSpells[selectedSpellAnytime].cooldown;
				refreshPlayerSpellFrameForCD();
				core.cpgn.WC_wait(core.DBSpells[selectedSpellAnytime].timeAnim, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0);
				selectedSpellAnytime = 0;
			end
		end
	else
		if (canPlay == 1) then
			if (self.spell.id == selectedSpell) then
				CancelSpellPlayer(self);
			else
				if (CdSpellPlayer[core.DBSpells[self.spell.id].positionId] == 0) then
					selectedSpell = self.spell.id;
					PlaySound("83", "sfx", false);
					self:SetNormalTexture(self.spell.iconPath.."_selected");
					self:SetHighlightTexture("");
					if (self.spell.target == true) then
						self:SetScript("OnEnter", OnEnterPlayerSpell_Selected);
						OnEnterPlayerSpell_Selected(self);
					elseif (self.spell.id == selectedSpell) then
						InteractSpellPlayer();
					end
					for i = 1, #core.DBPlayer.spells do
						if (i ~= self.spell.positionId) then
							if (_G["FSPlayerSpell"..i] ~= nil) then
								_G["FSPlayerSpell"..i]:SetNormalTexture(_G["FSPlayerSpell"..i].spell.iconPath);
								_G["FSPlayerSpell"..i]:SetHighlightTexture(_G["FSPlayerSpell"..i].spell.iconPath);
								if (i ~= selectedSpell and _G["FSPlayerSpell"..i].spell.target == true) then
									_G["FSPlayerSpell"..i]:SetScript("OnEnter", OnEnterPlayerSpell);
								end
							end
						end
					end
				end
			end
		end
	end
end
core.cpgn.PlayerSpell_OnClick = PlayerSpell_OnClick;

local function ResetFight()
	DBProfile.skillPoints = DBProfile.skillPoints - 1;
	_G["ProfileContentCurrencyText"]:SetText(DBProfile.skillPoints);
	local frame = CreateFrame("FRAME");
	frame.zoneId = zoneIdFight;
	frame.fightsId = fightId;
	frame.exceptionLevel = checkVictoryException;
	frame.mobs = {};
	for i = 1, 5 do
		_G["FightSceneEnnemy"..i]:Hide();
		_G["FSFrameTooltipEnnemy"..i]:Hide();
		currFight[i] = 0;
		if (core.DBcampaign[zoneIdFight].fights[fightId].mobs[i] ~= 0) then
			frame.mobs[i] = 1;
		else
			frame.mobs[i] = 0;
			_G["FightSceneEnnemy"..i]:Hide();
		end
	end
	DBProfile.pdv = DBProfile.pdvMax;
	EffectCurrEnnemy = { {}, {}, {}, {}, {} };
	currFight = {0, 0, 0, 0, 0};
	BeginFight(frame);
end


local function AbandonFight()
	endTurnPlayer(true);
	CdSpellCurrEnnemy = { {}, {}, {}, {}, {} };
	EffectCurrEnnemy = { {}, {}, {}, {}, {} };
	CdSpellPlayer = {};
	for i = 1, 5 do
		_G["FightSceneEnnemy"..i]:Hide();
		_G["FSFrameTooltipEnnemy"..i]:Hide();
		currFight[i] = 0;
	end
	core.cpgn.refreshPlayerLifeFrame(_G["BtnProfilePlayerLifeText"], false);
	_G["FightScenePlayer"]:Hide();
	_G["FightScene"]:Hide();
	core.locationPlayer = 0;
end

local function OnClick_SelectLevel(self)
	PlaySound("73279", "sfx", false);
	core.cpgn.checkFightDone(self.zoneId);
	if (self.done == true) then
		_G["Camp"..self.zoneId.."pointFights"..self.id]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..self.zoneId.."_"..self.id.."_done_selected");
		_G["Camp"..self.zoneId.."pointFights"..self.id]:SetHighlightTexture("");
		_G["Camp"..self.zoneId.."pointFights"..self.id]:SetScript("OnClick", nothing);
	else
		_G["Camp"..self.zoneId.."pointFights"..self.id]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..self.zoneId.."_"..self.id.."_selected");
		_G["Camp"..self.zoneId.."pointFights"..self.id]:SetHighlightTexture("");		
		_G["Camp"..self.zoneId.."pointFights"..self.id]:SetScript("OnClick", nothing);
	end
	_G["btnPlayLevel"..self.zoneId].zoneId = self.zoneId;
	_G["btnPlayLevel"..self.zoneId].fightsId = self.fightsId;
	_G["btnPlayLevel"..self.zoneId].mobs = self.mobs;
	_G["btnPlayLevel"..self.zoneId].exceptionLevel = self.exceptionLevel;
	_G["btnPlayLevel"..self.zoneId]:Enable();
end

local function OnClick_SelectZone(self)
	PlaySound("73279", "sfx", false);
	
end

local function checkFightDone(idZone)
	if (idZone == 1) then
		for i = 1, #Campaign1 do
			if (Campaign1[i].done == true) then
				_G["Camp"..idZone.."pointFights"..i]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i.."_done");
				_G["Camp"..idZone.."pointFights"..i]:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i.."_done");
				_G["Camp"..idZone.."pointFights"..i].done = true;
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnEnter", OnEnterPointFights);
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnClick", OnClick_SelectLevel);
			elseif (i == 1 or Campaign1[i - 1].done == true) then
				_G["Camp"..idZone.."pointFights"..i]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i);
				_G["Camp"..idZone.."pointFights"..i]:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i);			
				_G["Camp"..idZone.."pointFights"..i].done = false;
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnEnter", OnEnterPointFights);
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnClick", OnClick_SelectLevel);
			else
				_G["Camp"..idZone.."pointFights"..i]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i.."_lock");
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnEnter", OnEnterPointFights);
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnClick", nothing);
			end
		end
	elseif (idZone == 2) then
		for i = 1, #Campaign2 do
			if (Campaign2[i].done == true) then
				_G["Camp"..idZone.."pointFights"..i]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i.."_done");
				_G["Camp"..idZone.."pointFights"..i]:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i.."_done");
				_G["Camp"..idZone.."pointFights"..i].done = true;
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnEnter", OnEnterPointFights);
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnClick", OnClick_SelectLevel);
			elseif (i == 1 or Campaign2[i - 1].done == true) then
				_G["Camp"..idZone.."pointFights"..i]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i);
				_G["Camp"..idZone.."pointFights"..i]:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i);			
				_G["Camp"..idZone.."pointFights"..i].done = false;
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnEnter", OnEnterPointFights);
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnClick", OnClick_SelectLevel);
			else
				_G["Camp"..idZone.."pointFights"..i]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i.."_lock");
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnEnter", OnEnterPointFights);
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnClick", nothing);
			end
		end
	elseif (idZone == 3) then
		for i = 1, #Campaign3 do
			if (Campaign3[i].done == true) then
				_G["Camp"..idZone.."pointFights"..i]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i.."_done");
				_G["Camp"..idZone.."pointFights"..i]:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i.."_done");
				_G["Camp"..idZone.."pointFights"..i].done = true;
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnEnter", OnEnterPointFights);
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnClick", OnClick_SelectLevel);
			elseif (i == 1 or Campaign3[i - 1].done == true) then
				_G["Camp"..idZone.."pointFights"..i]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i);
				_G["Camp"..idZone.."pointFights"..i]:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i);			
				_G["Camp"..idZone.."pointFights"..i].done = false;
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnEnter", OnEnterPointFights);
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnClick", OnClick_SelectLevel);
			else
				_G["Camp"..idZone.."pointFights"..i]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/campaign/design_campaign/icon_fight"..idZone.."_"..i.."_lock");
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnEnter", OnEnterPointFights);
				_G["Camp"..idZone.."pointFights"..i]:SetScript("OnClick", nothing);
			end
		end
	end
end
core.cpgn.checkFightDone = checkFightDone;

StaticPopupDialogs["FFfight"] = {
	text = core.L["Vos points de vie ne se régénéreront pas si vous abandonnez, continuer ?"],
	button1 = core.L["Oui"],
	button2 = core.L["Non"],
	OnAccept = function()
      AbandonFight()
	end,
	hideOnEscape = false,
	whileDead = true,
}

StaticPopupDialogs["ConfirmReset"] = {
	text = core.L["Cela vous coûtera 1 point de compétence pour être soigné, continuer ?"],
	button1 = core.L["BtnRez"],
	button2 = core.L["Abandonner"],
	OnAccept = function()
      ResetFight();
	end,
	OnCancel = function()
      AbandonFight()
	end,
	hideOnEscape = false,
	whileDead = true,
}

StaticPopupDialogs["DefeatPopup"] = {
	text = core.L["Defeat"],
	button1 = core.L["Restart"],
	button2 = core.L["Abandonner"],
	OnAccept = function()
      StaticPopup_Show("ConfirmReset");
	end,
	OnCancel = function()
      AbandonFight()
	end,
	hideOnEscape = false,
	whileDead = true,
}

StaticPopupDialogs["DefeatPopupNoSkill"] = {
	text = core.L["Defeat"],
	button1 = core.L["Abandonner"],
	OnAccept = function()
      AbandonFight();
	end,
	hideOnEscape = false,
	whileDead = true,
}

StaticPopupDialogs["VictoryPopup"] = {
	text = core.L["Victory"],
	button1 = core.L["Next"],
	OnAccept = function()
		if (zoneIdFight == 1) then
			Campaign1[fightId].done = true;
			core.cpgn.BtnCampaign_OnClick(_G["CampaignButton1"]);
		elseif (zoneIdFight == 2) then
			Campaign2[fightId].done = true;
			core.cpgn.BtnCampaign_OnClick(_G["CampaignButton2"]);
		elseif (zoneIdFight == 3) then
			Campaign3[fightId].done = true;
			core.cpgn.BtnCampaign_OnClick(_G["CampaignButton3"]);
		end
		core.cpgn.checkFightDone(zoneIdFight);
		_G["btnPlayLevel"..zoneIdFight]:Disable();
		AbandonFight();
	end,
	hideOnEscape = false,
	whileDead = true,
}

StaticPopupDialogs["CantFight"] = {
	text = core.L["Vous devez vous soigner pour commencer ce combat."],
	button1 = core.L["Next"],
	hideOnEscape = false,
	whileDead = true,
}

local function ScrollFrame_OnMouseWheel(self, delta)
	local newValue = self:GetVerticalScroll() - (delta * 100);

	if (newValue < 0) then
		newValue = 0;
	elseif (newValue > self:GetVerticalScrollRange()) then
		newValue = self:GetVerticalScrollRange();
	end
	self:SetVerticalScroll(newValue);
end

local function BtnCampaign_Unselected_all()
	if (Campaign1[#Campaign1].done == true) then
		_G["CampaignButton1"]:SetNormalTexture(core.DBcampaign[1].icon.."_done");
		_G["CampaignButton1"]:SetHighlightTexture(core.DBcampaign[1].icon.."_done");
		_G["CampaignButton1"]:SetScript("OnClick", core.cpgn.BtnCampaign_OnClick);
	else
		_G["CampaignButton1"]:SetNormalTexture(core.DBcampaign[1].icon);
		_G["CampaignButton1"]:SetHighlightTexture(core.DBcampaign[1].icon);
		_G["CampaignButton1"]:SetScript("OnClick", core.cpgn.BtnCampaign_OnClick);
	end
	if (Campaign2[#Campaign2].done == true) then
		_G["CampaignButton2"]:SetNormalTexture(core.DBcampaign[2].icon.."_done");
		_G["CampaignButton2"]:SetHighlightTexture(core.DBcampaign[2].icon.."_done");
		_G["CampaignButton2"]:SetScript("OnClick", core.cpgn.BtnCampaign_OnClick);
	elseif (Campaign1[#Campaign1].done == false) then
		_G["CampaignButton2"]:SetNormalTexture(core.DBcampaign[2].icon.."_lock");
		_G["CampaignButton2"]:SetHighlightTexture("");
		_G["CampaignButton2"]:SetScript("OnClick", nothing);
	else
		_G["CampaignButton2"]:SetNormalTexture(core.DBcampaign[2].icon);
		_G["CampaignButton2"]:SetHighlightTexture(core.DBcampaign[2].icon);
		_G["CampaignButton2"]:SetScript("OnClick", core.cpgn.BtnCampaign_OnClick);
	end
	if (Campaign3[#Campaign3].done == true) then
		_G["CampaignButton3"]:SetNormalTexture(core.DBcampaign[3].icon.."_done");
		_G["CampaignButton3"]:SetHighlightTexture(core.DBcampaign[3].icon.."_done");
		_G["CampaignButton3"]:SetScript("OnClick", core.cpgn.BtnCampaign_OnClick);
	elseif (Campaign2[#Campaign2].done == false) then
		_G["CampaignButton3"]:SetNormalTexture(core.DBcampaign[3].icon.."_lock");
		_G["CampaignButton3"]:SetHighlightTexture("");
		_G["CampaignButton3"]:SetScript("OnClick", nothing);
	else
		_G["CampaignButton3"]:SetNormalTexture(core.DBcampaign[3].icon);
		_G["CampaignButton3"]:SetHighlightTexture(core.DBcampaign[3].icon);
		_G["CampaignButton3"]:SetScript("OnClick", core.cpgn.BtnCampaign_OnClick);
	end
end
	
local function BtnCampaign_OnClick(self)
	BtnCampaign_Unselected_all();
	if (self.isProfile == nil) then
		_G["ProfileScenePlayerModel"]:Hide();
		core.locationPlayer = 0;
		PlaySound("681", "sfx");
		if (self.campaignId == 1) then
			if (Campaign1[#Campaign1].done == true) then
				self:SetNormalTexture(self.icon.."_done_selected");
				self:SetHighlightTexture(self.icon.."_done_selected");
			else
				self:SetNormalTexture(self.icon.."_selected");
				self:SetHighlightTexture(self.icon.."_selected");
			end
		elseif (self.campaignId == 2) then
			if (Campaign2[#Campaign2].done == true) then
				self:SetNormalTexture(self.icon.."_done_selected");
				self:SetHighlightTexture(self.icon.."_done_selected");
			else
				self:SetNormalTexture(self.icon.."_selected");
				self:SetHighlightTexture(self.icon.."_selected");
			end
		elseif (self.campaignId == 3) then
			if (Campaign3[#Campaign3].done == true) then
				self:SetNormalTexture(self.icon.."_done_selected");
				self:SetHighlightTexture(self.icon.."_done_selected");
			else
				self:SetNormalTexture(self.icon.."_selected");
				self:SetHighlightTexture(self.icon.."_selected");
			end
		end
	else
		core.cpgn.checkItemUnlock();
		PlaySound("21966", "sfx", false);
		_G["ProfileScenePlayerModel"]:Show();
		_G["ProfileScenePlayerModel"]:SetDisplayInfo(core.DBPlayer.displayId);
		_G["ProfileScenePlayerModel"]:EquipItem(core.DBPlayer.weaponId);
		_G["ProfileScenePlayerModel"]:EquipItem(core.DBPlayer.weaponId);
		core.locationPlayer = 1;
	end
	self.content:Show();
	local scrollChild = _G["bgCampaignFrame"].ScrollFrame:GetScrollChild();
	if (scrollChild) then
		scrollChild:Hide();
	end
	_G["bgCampaignFrame"].ScrollFrame:SetScrollChild(self.content);
	self.content:Show();
end
core.cpgn.BtnCampaign_OnClick = BtnCampaign_OnClick;

local function OnClick_MoveLevelCampaign(self)
	PlaySound("83", "sfx", false);
	if (self:GetText() == ">>>") then
		self.position = self.position + 1;
		_G["btnLeftArrowC"..self.campaignId].position = _G["btnLeftArrowC"..self.campaignId].position + 1;
		_G["btnLeftArrowC"..self.campaignId]:Enable();
		if ((self.position + 5) == self.nbLvL) then
			_G["btnRightArrowC"..self.campaignId]:Disable();
		end
	elseif (self:GetText() == "<<<") then
		self.position = self.position - 1;
		_G["btnRightArrowC"..self.campaignId].position = _G["btnRightArrowC"..self.campaignId].position - 1;
		_G["btnRightArrowC"..self.campaignId]:Enable();
		if (self.position == 0) then
			_G["btnLeftArrowC"..self.campaignId]:Disable();
		end
	end
	for i = 1, self.nbLvL do
		if (i == 1) then
			_G["Camp"..self.campaignId.."pointFights"..i]:SetPoint("BOTTOMLEFT",  "CampaignButton"..self.campaignId.."Content", "BOTTOMLEFT", 15+(-self.position*128), 10);
		else
			_G["Camp"..self.campaignId.."pointFights"..i]:SetPoint("LEFT", _G["Camp"..self.campaignId.."pointFights"..(i - 1)], "RIGHT");
		end
	end
end

local function CreateButtonCampaign(CampaignInfo, frameParent, bgCampaign)
		local CampaignButton = CreateFrame("Button", "CampaignButton"..CampaignInfo.id, frameParent);
		CampaignButton:SetFrameLevel(10);
		CampaignButton:SetSize(140, 140);
		if (CampaignInfo.id == 1) then
			CampaignButton:SetPoint("TOP", frameParent, "TOP", -10, 0);
		else
			CampaignButton:SetPoint("BOTTOM", _G["CampaignButton"..(CampaignInfo.id - 1)], "BOTTOM", 0, -110);
		end
		CampaignButton:SetNormalTexture(CampaignInfo.icon);
		CampaignButton:SetHighlightTexture(CampaignInfo.icon);
		CampaignButton.campaignId = CampaignInfo.id;
		CampaignButton.icon = CampaignInfo.icon;
		CampaignButton.text = CampaignButton:CreateFontString("CampaignButton"..CampaignInfo.id.."Text", "OVERLAY", CampaignButton);
		if GetLocale() == "ruRU" then
			CampaignButton.text:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\MORPHEUS__CYR.TTF", 16);
		else
			CampaignButton.text:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\MORPHEUS.TTF", 16);
		end
		CampaignButton.text:SetTextColor(1, 0.82, 0);
		CampaignButton.text:SetSize(CampaignButton:GetWidth(), 0);
		CampaignButton.text:SetText(CampaignInfo.title);
		CampaignButton.text:SetPoint("BOTTOM", CampaignButton, "BOTTOM", 0, 18);

		CampaignButton.content = CreateFrame("Frame", "CampaignButton"..CampaignInfo.id.."Content", bgCampaign, BackdropTemplateMixin and "BackdropTemplate");
		CampaignButton.content:SetSize(650,450);
		CampaignButton.content:Hide();
		CampaignButton.content:SetBackdrop({bgFile = CampaignInfo.bgCampaign,
								edgeFile = "",
								tile = false, edgeSize = 16, 
								insets = { left = 3, right = 0, top = 0, bottom = 0 }});
								
		CampaignButton.content.book = CreateFrame("Frame", nil, CampaignButton.content, BackdropTemplateMixin and "BackdropTemplate");
		CampaignButton.content.book:SetSize(650,450);
		CampaignButton.content.book:SetPoint("TOPLEFT", CampaignButton.content, "TOPLEFT");
		CampaignButton.content.book:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/design_campaign/bg_page_campaign",
								edgeFile = "",
								tile = false, edgeSize = 16, 
								insets = { left = 3, right = -195, top = 0, bottom = -45 }});						
								
				
					
		CampaignButton.content.bgtitle = CreateFrame("Frame", nil, CampaignButton.content, BackdropTemplateMixin and "BackdropTemplate");
		CampaignButton.content.bgtitle:SetPoint("TOP", CampaignButton.content, "TOP", 0, -6);
		CampaignButton.content.bgtitle:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/design_campaign/bg_title_campaign",
								edgeFile = "",
								tile = false, edgeSize = 16, 
								insets = { left = 3, right = 0, top = 0, bottom = 0 }});
		CampaignButton.content.bgtitle:SetSize(300,100);
		
		CampaignButton.content.title = CampaignButton.content.bgtitle:CreateFontString(nil, "OVERLAY", CampaignButton.content.bgtitle);
		if GetLocale() == "ruRU" then
			CampaignButton.content.title:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\MORPHEUS__CYR.TTF", 30);
		else
			CampaignButton.content.title:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\MORPHEUS.TTF", 30);
		end
		CampaignButton.content.title:SetPoint("CENTER", CampaignButton.content.bgtitle, "CENTER");
		CampaignButton.content.title:SetTextColor(1, 0.82, 0);
		CampaignButton.content.title:SetText(CampaignInfo.title);		
		
		

		for i = 1, (core.libs.tablelength(CampaignInfo.fights)) do
			local pointFights = CreateFrame("Button", "Camp"..CampaignInfo.id.."pointFights"..CampaignInfo.fights[i].id, CampaignButton.content);
			pointFights:SetSize(128,128);
			if (i == 1) then
				pointFights:SetPoint("BOTTOMLEFT",  CampaignButton.content, "BOTTOMLEFT", 15, 10);
			else
				pointFights:SetPoint("LEFT", _G["Camp"..CampaignInfo.id.."pointFights"..(i - 1)], "RIGHT");
			end
			pointFights.boss = CampaignInfo.fights[i].boss;
			pointFights.id = CampaignInfo.fights[i].id;
			pointFights.fightsId = i;
			pointFights.mobs = {};
			pointFights.zoneId = CampaignInfo.id;
			pointFights.exceptionLevel = CampaignInfo.fights[i].winException;
			
			for i2 = 1, 5 do
				if (CampaignInfo.fights[i].mobs[i2] ~= 0 and CampaignInfo.fights[i].mobs[i2] ~= nil) then
					pointFights.mobs[i2] = core.DBMobs[CampaignInfo.fights[i].mobs[i2]];
				else
					pointFights.mobs[i2] = 0;
				end
			end
			pointFights:SetScript("OnLeave", OnLeave);
		end
		
		CampaignButton.content.bar_play = CreateFrame("Frame", nil, CampaignButton.content, BackdropTemplateMixin and "BackdropTemplate");
		CampaignButton.content.bar_play:SetSize(670, 45);
		CampaignButton.content.bar_play:SetPoint("BOTTOM", CampaignButton.content, "BOTTOM", 8, -3);
		CampaignButton.content.bar_play:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/mounts/fond_progress_page"});
		CampaignButton.content.bar_play:SetFrameLevel(_G["Camp"..CampaignInfo.id.."pointFights1"]:GetFrameLevel() + 1);
		
		
		CampaignButton.content.bar_play.btnLeft = CreateFrame("Button", "btnLeftArrowC"..CampaignInfo.id, CampaignButton.content.bar_play, "UIPanelButtonTemplate");
		CampaignButton.content.bar_play.btnLeft:SetSize(75, 32);
		CampaignButton.content.bar_play.btnLeft:SetText("<<<");
		CampaignButton.content.bar_play.btnLeft.campaignId = CampaignInfo.id;
		CampaignButton.content.bar_play.btnLeft.nbLvL = #CampaignInfo.fights;
		CampaignButton.content.bar_play.btnLeft.position = 0;
		CampaignButton.content.bar_play.btnLeft:SetPoint("LEFT", CampaignButton.content.bar_play, "LEFT", 22, 0);
		CampaignButton.content.bar_play.btnLeft:Disable();
		CampaignButton.content.bar_play.btnLeft:SetScript("OnClick", OnClick_MoveLevelCampaign);
		
		CampaignButton.content.bar_play.btnRight = CreateFrame("Button", "btnRightArrowC"..CampaignInfo.id, CampaignButton.content.bar_play, "UIPanelButtonTemplate");
		CampaignButton.content.bar_play.btnRight:SetSize(75, 32);
		CampaignButton.content.bar_play.btnRight:SetText(">>>");
		CampaignButton.content.bar_play.btnRight.campaignId = CampaignInfo.id;
		CampaignButton.content.bar_play.btnRight.nbLvL = #CampaignInfo.fights;
		CampaignButton.content.bar_play.btnRight.position = 0;
		CampaignButton.content.bar_play.btnRight:SetPoint("RIGHT", CampaignButton.content.bar_play, "RIGHT", -20, 0);
		CampaignButton.content.bar_play.btnRight:SetScript("OnClick", OnClick_MoveLevelCampaign);
		if (#CampaignInfo.fights > 5) then
			CampaignButton.content.bar_play.btnRight:Enable();
		else
			CampaignButton.content.bar_play.btnRight:Disable();
		end
		
		CampaignButton.content.bar_play.btnPlayLevel = CreateFrame("Button", "btnPlayLevel"..CampaignInfo.id, CampaignButton.content.bar_play, "UIPanelButtonTemplate");
		CampaignButton.content.bar_play.btnPlayLevel:SetSize(100, 32);
		CampaignButton.content.bar_play.btnPlayLevel:SetText(core.L["Combattre"]);
		CampaignButton.content.bar_play.btnPlayLevel:SetPoint("CENTER", CampaignButton.content.bar_play, "CENTER");
		CampaignButton.content.bar_play.btnPlayLevel:Disable();
		CampaignButton.content.bar_play.btnPlayLevel:SetScript("OnClick", BeginFight);
		core.cpgn.checkFightDone(CampaignInfo.id);
		
end

local function CreateCampaignTab()

	core.cpgn.CheckLimitSkillPoints();

	local bgTab1 = CreateFrame("Frame", "bgTab1", core.Tab.t1.content, BackdropTemplateMixin and "BackdropTemplate");
	bgTab1:SetSize(800,450);
	bgTab1:SetPoint("CENTER", core.Tab.t1.content, "CENTER");
	bgTab1:SetFrameLevel(5);
	bgTab1:SetBackdrop({bgFile = "Interface/FrameGeneral/UI-Background-Marble",
							 edgeFile = "", 
							 tile = false, edgeSize = 16,
							 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	
	core.cpgn.CreateFrameFirstVisit();
	

	--- FIGHT SCENE ---
	local FightScene = CreateFrame("Frame", "FightScene", core.Tab.t1.content, BackdropTemplateMixin and "BackdropTemplate");
	FightScene:SetSize(800,450);
	FightScene:SetPoint("CENTER", core.Tab.t1.content, "CENTER");
	FightScene:SetFrameLevel(11);
	FightScene:EnableMouse(true)
	FightScene:Hide();
	
	FightScene.FightChat = CreateFrame("Frame", "FightScene", FightScene, nil);
	FightScene.FightChat:SetSize(300, 175);
	FightScene.FightChat:SetPoint("BOTTOMLEFT", FightScene, "BOTTOMLEFT");

	
	
	FightScene.TurnText = FightScene:CreateFontString("FSTurnText", "OVERLAY", FightScene);
	FightScene.TurnText:SetFontObject("GameFontNormalLarge");
	FightScene.TurnText:SetPoint("BOTTOMRIGHT", FightScene, "BOTTOMRIGHT", -3, 180);
	FightScene.TurnText:SetText(core.L["YouTurn"]);
	
	FightScene.FFbtn = CreateFrame("Button", "FightSceneFFbtn", FightScene, "UIPanelButtonTemplate");
	FightScene.FFbtn:SetSize(90, 30);
	FightScene.FFbtn:SetText(core.L["Abandonner"]);
	FightScene.FFbtn:SetPoint("BOTTOMRIGHT", FightScene, "BOTTOMRIGHT", -5, 5);
	FightScene.FFbtn:SetScript("OnClick", function(self)
		StaticPopup_Show("FFfight");
	end);

	local FSEnnemy1 = CreateFrame("CinematicModel", "FightSceneEnnemy1", UIParent, nil);
	FSEnnemy1:SetSize(800,450);
	FSEnnemy1:SetPoint("TOPLEFT", FightScene, "TOPLEFT", 0, 0);
	FSEnnemy1:SetFrameLevel(11);
	FSEnnemy1:Hide();
	local InteractEnnemy1 = CreateFrame("Button", "InteractEnnemy1", FightScene, nil);
	InteractEnnemy1:SetSize(160,250);
	InteractEnnemy1:SetPoint("TOPLEFT", FightScene, "TOPLEFT", 0, 0);
	InteractEnnemy1:SetFrameLevel(12);
	InteractEnnemy1.position = 1;
	InteractEnnemy1:SetScript("OnEnter", OnEnterEnnemyScene);
	InteractEnnemy1:SetScript("OnLeave", OnLeaveEnnemyScene);
	InteractEnnemy1:SetScript("OnClick", InteractSpellPlayer);
	
	local FSEnnemy2 = CreateFrame("CinematicModel", "FightSceneEnnemy2", UIParent, nil);
	FSEnnemy2:SetSize(800,450);
	FSEnnemy2:SetPoint("TOPLEFT", FightScene, "TOPLEFT", 0, 0);
	FSEnnemy2:SetFrameLevel(11);
	FSEnnemy2:Hide();
	local InteractEnnemy2 = CreateFrame("Button", "InteractEnnemy2", FightScene, nil);
	InteractEnnemy2:SetSize(160,250);
	InteractEnnemy2:SetPoint("TOPLEFT", InteractEnnemy1, "TOPRIGHT", 0, 0);
	InteractEnnemy2:SetFrameLevel(12);
	InteractEnnemy2.position = 2;
	InteractEnnemy2:SetScript("OnEnter", OnEnterEnnemyScene);
	InteractEnnemy2:SetScript("OnLeave", OnLeaveEnnemyScene);
	InteractEnnemy2:SetScript("OnClick", InteractSpellPlayer);
	
	local FSEnnemy3 = CreateFrame("CinematicModel", "FightSceneEnnemy3", UIParent, nil);
	FSEnnemy3:SetSize(800,450);
	FSEnnemy3:SetPoint("TOPLEFT", FightScene, "TOPLEFT", 0, 0);
	FSEnnemy3:SetFrameLevel(11);
	FSEnnemy3:Hide();
	local InteractEnnemy3 = CreateFrame("Button", "InteractEnnemy3", FightScene, nil);
	InteractEnnemy3:SetSize(160,250);
	InteractEnnemy3:SetPoint("TOPLEFT", InteractEnnemy2, "TOPRIGHT", 0, 0);
	InteractEnnemy3:SetFrameLevel(12);
	InteractEnnemy3.position = 3;
	InteractEnnemy3:SetScript("OnEnter", OnEnterEnnemyScene);
	InteractEnnemy3:SetScript("OnLeave", OnLeaveEnnemyScene);
	InteractEnnemy3:SetScript("OnClick", InteractSpellPlayer);
	
	local FSEnnemy4 = CreateFrame("CinematicModel", "FightSceneEnnemy4", UIParent, nil);
	FSEnnemy4:SetSize(800,450);
	FSEnnemy4:SetPoint("TOPLEFT", FightScene, "TOPLEFT", 0, 0);
	FSEnnemy4:SetFrameLevel(11);
	FSEnnemy4:Hide();
	local InteractEnnemy4 = CreateFrame("Button", "InteractEnnemy4", FightScene, nil);
	InteractEnnemy4:SetSize(160,250);
	InteractEnnemy4:SetPoint("TOPLEFT", InteractEnnemy3, "TOPRIGHT", 0, 0);
	InteractEnnemy4:SetFrameLevel(12);
	InteractEnnemy4.position = 4;
	InteractEnnemy4:SetScript("OnEnter", OnEnterEnnemyScene);
	InteractEnnemy4:SetScript("OnLeave", OnLeaveEnnemyScene);
	InteractEnnemy4:SetScript("OnClick", InteractSpellPlayer);
	
	local FSEnnemy5 = CreateFrame("CinematicModel", "FightSceneEnnemy5", UIParent, nil);
	FSEnnemy5:SetSize(800,450);
	FSEnnemy5:SetPoint("TOPLEFT", FightScene, "TOPLEFT", 0, 0);
	FSEnnemy5:SetFrameLevel(11);
	FSEnnemy5:Hide();
	local InteractEnnemy5 = CreateFrame("Button", "InteractEnnemy5", FightScene, nil);
	InteractEnnemy5:SetSize(160,250);
	InteractEnnemy5:SetPoint("TOPLEFT", InteractEnnemy4, "TOPRIGHT", 0, 0);
	InteractEnnemy5:SetFrameLevel(12);
	InteractEnnemy5.position = 5;
	InteractEnnemy5:SetScript("OnEnter", OnEnterEnnemyScene);
	InteractEnnemy5:SetScript("OnLeave", OnLeaveEnnemyScene);
	InteractEnnemy5:SetScript("OnClick", InteractSpellPlayer);
	
	for i = 1, 5 do
		local FrameTooltipEnnemy = CreateFrame("Frame", "FSFrameTooltipEnnemy"..i, _G["InteractEnnemy"..i], BackdropTemplateMixin and "BackdropTemplate");
		
		FrameTooltipEnnemy:SetSize(100,30);
		FrameTooltipEnnemy:SetPoint("TOPRIGHT", _G["InteractEnnemy"..i], "TOPRIGHT", 0, 0);
		FrameTooltipEnnemy:SetScript("OnEnter", OnEnterTooltipEnnemy);
		FrameTooltipEnnemy:SetScript("OnLeave", OnLeave);
		FrameTooltipEnnemy.id = i;
		FrameTooltipEnnemy:Hide();
	
		local DamageText = _G["FSFrameTooltipEnnemy"..i]:CreateFontString("FSEnnemyDamageText"..i, "OVERLAY", _G["FSFrameTooltipEnnemy"..i]);
		DamageText:SetFontObject("GameFontHighlightLarge");
		DamageText:SetPoint("TOPRIGHT", _G["FSFrameTooltipEnnemy"..i], "TOPRIGHT", -15, -10);
		local DamageIcon = CreateFrame("Frame", "FSEnnemyDamageIcon"..i, _G["FSFrameTooltipEnnemy"..i], BackdropTemplateMixin and "BackdropTemplate");
		DamageIcon:SetSize(16,16);
		DamageIcon:SetPoint("TOPRIGHT", "FSEnnemyDamageText"..i, "TOPLEFT", 0, 0);
		DamageIcon:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/spells/icon_attack",
							 edgeFile = "", 
							 tile = false, edgeSize = 16,
							 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
		
		local LifeIcon = CreateFrame("Frame", "FSEnnemyLifeIcon"..i, _G["FSFrameTooltipEnnemy"..i], BackdropTemplateMixin and "BackdropTemplate");
		LifeIcon:SetSize(16,16);
		LifeIcon:SetPoint("TOPRIGHT", "FSEnnemyDamageIcon"..i, "TOPLEFT", -10, 0);
		LifeIcon:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/spells/icon_life",
							 edgeFile = "", 
							 tile = false, edgeSize = 16,
							 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
		local LifeText = _G["FSFrameTooltipEnnemy"..i]:CreateFontString("FSEnnemyLifeText"..i, "OVERLAY", _G["FSFrameTooltipEnnemy"..i]);
		LifeText:SetFontObject("GameFontHighlightLarge");
		LifeText:SetPoint("RIGHT", _G["FSEnnemyLifeIcon"..i], "RIGHT", -20, 0);
	end

	local FSPlayer = CreateFrame("CinematicModel", "FightScenePlayer", UIParent, nil);
	FSPlayer:SetSize(800,450);
	FSPlayer:SetPoint("TOPLEFT", FightScene, "TOPLEFT", 0, 0);
	FSPlayer:SetFrameLevel(11);
	FSPlayer:Hide();
	
	local FSPDamageText = FightScene:CreateFontString("FSPlayerDamageText", "OVERLAY", FightScene);
	FSPDamageText:SetFontObject("GameFontHighlightLarge");
	FSPDamageText:SetPoint("BOTTOM", FightScene, "BOTTOM", 228, 10);
	FSPDamageText:SetText(DBProfile.damage);
	local FSPDamageIcon = CreateFrame("Frame", "FSPlayerDamageIcon", FightScene, BackdropTemplateMixin and "BackdropTemplate");
	FSPDamageIcon:SetSize(20,20);
	FSPDamageIcon:SetPoint("RIGHT", "FSPlayerDamageText", "LEFT", 0, 0);
	FSPDamageIcon:SetScript("OnEnter", OnEnterPointsAttack);
	FSPDamageIcon:SetScript("OnLeave", OnLeave);
	FSPDamageIcon:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/spells/icon_attack",
						 edgeFile = "", 
						 tile = false, edgeSize = 16,
						 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	
	local FSPLifeIcon = CreateFrame("Frame", "FSPlayerLifeIcon", FightScene, BackdropTemplateMixin and "BackdropTemplate");
	FSPLifeIcon:SetSize(24,24);
	FSPLifeIcon:SetPoint("RIGHT", "FSPlayerDamageIcon", "LEFT", -10, 0);
	FSPLifeIcon:SetScript("OnEnter", OnEnterPointsLife);
	FSPLifeIcon:SetScript("OnLeave", OnLeave);
	FSPLifeIcon:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/spells/icon_life",
						 edgeFile = "", 
						 tile = false, edgeSize = 16,
						 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	local FSPLifeText = FightScene:CreateFontString("FSPlayerLifeText", "OVERLAY", FightScene);
	FSPLifeText:SetFontObject("GameFontHighlightLarge");
	FSPLifeText:SetPoint("RIGHT", "FSPlayerLifeIcon", "RIGHT", -30, 0);
	FSPLifeText:SetText(DBProfile.pdv);
	
	local FSPEffectIcon = CreateFrame("Frame", "FSPlayerEffectIcon", FightScene, BackdropTemplateMixin and "BackdropTemplate");
	FSPEffectIcon:SetSize(24,24);
	FSPEffectIcon:SetPoint("LEFT", FSPDamageText, "RIGHT", 13, 0);
	FSPEffectIcon:SetScript("OnEnter", OnEnterEffectPlayer);
	FSPEffectIcon:SetScript("OnLeave", OnLeave);
	FSPEffectIcon:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/spells/icon_effect",
						 edgeFile = "", 
						 tile = false, edgeSize = 16,
						 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	local FSPEffectText = FightScene:CreateFontString("FSPlayerEffectText", "OVERLAY", FightScene);
	FSPEffectText:SetFontObject("GameFontHighlightLarge");
	FSPEffectText:SetPoint("LEFT", FSPEffectIcon, "RIGHT", 3, 0);
	FSPEffectText:SetText(core.cpgn.NbEffectsPlayer());

	local ipos = 1;
	for i = 1, core.libs.tablelength(core.DBPlayer.spells) do
		if (DBProfile.itemUnlock[3][i] == 0) then
				local FSPlayerSpell = CreateFrame("Button", "FSPlayerSpell"..ipos, FightScene);
				FSPlayerSpell:SetSize(40, 40);
				if (ipos == 1) then
					FSPlayerSpell:SetPoint("BOTTOM", FightScene, "BOTTOM", 150, 50);
				elseif (ipos%6 == 0) then
					FSPlayerSpell:SetPoint("BOTTOM", _G["FSPlayerSpell"..(ipos - 5)], "TOP", 0, 10);	
				else
					FSPlayerSpell:SetPoint("RIGHT", _G["FSPlayerSpell"..(ipos - 1)], "RIGHT", 50, 0);
				end
				ipos = ipos + 1;
		end
	end
	
	local Separator = CreateFrame("Frame", "bgTab1", core.Tab.t1.content, BackdropTemplateMixin and "BackdropTemplate");
	Separator:SetSize(32,450);
	Separator:SetPoint("LEFT", core.Tab.t1.content, "LEFT", 150, 0);
	Separator:SetFrameLevel(9);
	Separator:SetBackdrop({bgFile = "Interface/Garrison/ClassHallFrame2",
							 edgeFile = "", 
							 tile = false, edgeSize = 16,
							 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	
	ListCampaign = CreateFrame("Frame", "ListCampaignFrame", bgTab1);
	ListCampaign:SetSize(150,455);
	ListCampaign:SetPoint("LEFT", bgTab1, "LEFT");
	
	----- BUTTON PROFILE -----
	ListCampaign.bgProfile = CreateFrame("Button", "BtnProfileFrame", ListCampaign, BackdropTemplateMixin and "BackdropTemplate");
	ListCampaign.bgProfile:SetSize(ListCampaign:GetWidth(), 75);
	ListCampaign.bgProfile:SetPoint("TOP", ListCampaign, "TOP", 1, -1);
	ListCampaign.bgProfile:SetScript("OnEnter", OnEnterProfile);
	ListCampaign.bgProfile:SetScript("OnLeave", OnLeaveProfile);
	ListCampaign.bgProfile:SetScript("OnClick", core.cpgn.BtnCampaign_OnClick);
	ListCampaign.bgProfile.isProfile = true;
	ListCampaign.bgProfile:SetBackdrop({bgFile = "Interface/TALENTFRAME/spec-blue-bg",
										edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
										tile = false, edgeSize = 16, 
												insets = { left = 0, right = 0, top = 0, bottom = -15 }
												});
												
	ListCampaign.bgProfile.PlayerDamageText = ListCampaign.bgProfile:CreateFontString("BtnProfilePlayerDamageText", "OVERLAY", ListCampaign.bgProfile);
	ListCampaign.bgProfile.PlayerDamageText:SetFontObject("GameFontHighlightLarge");
	ListCampaign.bgProfile.PlayerDamageText:SetPoint("TOPRIGHT", ListCampaign.bgProfile, "TOPRIGHT", -8, -10);
	ListCampaign.bgProfile.PlayerDamageText:SetText(DBProfile.damageMax);
	ListCampaign.bgProfile.PlayerDamageIcon = CreateFrame("Frame", "BtnProfilePlayerDamageIcon", ListCampaign.bgProfile, BackdropTemplateMixin and "BackdropTemplate");
	ListCampaign.bgProfile.PlayerDamageIcon:SetSize(18,17);
	ListCampaign.bgProfile.PlayerDamageIcon:SetPoint("RIGHT", "BtnProfilePlayerDamageText", "LEFT", 0, 0);
	ListCampaign.bgProfile.PlayerDamageIcon:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/spells/icon_attack",
						 edgeFile = "", 
						 tile = false, edgeSize = 16,
						 insets = { left = 0, right = 3, top = 0, bottom = 0 }});	
	
	ListCampaign.bgProfile.PlayerLifeIcon = CreateFrame("Frame", "BtnProfilePlayerLifeIcon", ListCampaign.bgProfile, BackdropTemplateMixin and "BackdropTemplate");
	ListCampaign.bgProfile.PlayerLifeIcon:SetSize(16,16);
	ListCampaign.bgProfile.PlayerLifeIcon:SetPoint("RIGHT", "BtnProfilePlayerDamageIcon", "LEFT", -3, 0);
	ListCampaign.bgProfile.PlayerLifeIcon:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/spells/icon_life",
						 edgeFile = "", 
						 tile = false, edgeSize = 16,
						 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	ListCampaign.bgProfile.PlayerLifeText = ListCampaign.bgProfile:CreateFontString("BtnProfilePlayerLifeText", "OVERLAY", ListCampaign.bgProfile);
	ListCampaign.bgProfile.PlayerLifeText:SetFontObject("GameFontHighlightLarge");
	ListCampaign.bgProfile.PlayerLifeText:SetPoint("RIGHT", "BtnProfilePlayerLifeIcon", "RIGHT", -20, 0);
	core.cpgn.refreshPlayerLifeFrame(ListCampaign.bgProfile.PlayerLifeText, false);
	
	ListCampaign.bgProfile.PlayerIcon = CreateFrame("Frame", "BtnProfilePlayerIcon", ListCampaign.bgProfile, BackdropTemplateMixin and "BackdropTemplate");
	ListCampaign.bgProfile.PlayerIcon:SetSize(32,32);
	ListCampaign.bgProfile.PlayerIcon:SetPoint("BOTTOMLEFT", ListCampaign.bgProfile, "BOTTOMLEFT", 5, 6);
	ListCampaign.bgProfile.PlayerIcon:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/icon_character",
						 edgeFile = "", 
						 tile = false, edgeSize = 16,
						 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	ListCampaign.bgProfile.PlayerText = ListCampaign.bgProfile:CreateFontString("BtnProfilePlayerText", "OVERLAY", ListCampaign.bgProfile);
	ListCampaign.bgProfile.PlayerText:SetFontObject("GameFontHighlightLarge");
	ListCampaign.bgProfile.PlayerText:SetPoint("BOTTOMRIGHT", ListCampaign.bgProfile, "BOTTOMRIGHT", -13, 13);
	ListCampaign.bgProfile.PlayerText:SetText(core.L["Personnage"]);
	
	if (DBProfile.pdv >= 100 and DBProfile.pdvMax >= 100) then
		_G["BtnProfilePlayerLifeIcon"]:SetPoint("TOPRIGHT", _G["BtnProfileFrame"], "TOPRIGHT", -10, -7);
		_G["BtnProfilePlayerDamageIcon"]:SetPoint("TOPRIGHT", _G["BtnProfileFrame"], "TOPRIGHT", -10, -27);
		_G["BtnProfilePlayerDamageText"]:SetPoint("TOPRIGHT", _G["BtnProfileFrame"], "TOPRIGHT",-33, -27);
	end
	----- ----- ------
	
	ListCampaign.ScrollFrame = CreateFrame("ScrollFrame", nil, ListCampaign, "UIPanelScrollFrameTemplate");
	ListCampaign.ScrollFrame:SetPoint("TOPLEFT", ListCampaign, "TOPLEFT", 0, -75);
	ListCampaign.ScrollFrame:SetPoint("BOTTOMRIGHT", ListCampaign, "BOTTOMRIGHT", 0, 4);							
	
	ListCampaign.ScrollFrame:SetClipsChildren(true);
	local child = CreateFrame("Frame", nil, ListCampaign.ScrollFrame);
	child:SetSize(170, 250);	
	ListCampaign.ScrollFrame:SetScrollChild(child);
	ListCampaign.ScrollFrame:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel);
	
	local bgCampaign = CreateFrame("Frame", "bgCampaignFrame", core.Tab.t1.content, BackdropTemplateMixin and "BackdropTemplate");
	bgCampaign:SetSize(650,454);
	bgCampaign:SetPoint("TOPRIGHT", core.Tab.t1.content, "TOPRIGHT");
	bgCampaign:SetFrameLevel(7);
	bgCampaign:SetBackdrop({bgFile = "Interface/TALENTFRAME/spec-paper-bg",
							edgeFile = "", 
							tile = false, edgeSize = 16,
							insets = { left = 3, right = -198, top = 3, bottom = -73 }});
							
	bgCampaign.ScrollFrame = CreateFrame("ScrollFrame", nil, bgCampaign, "UIPanelScrollFrameTemplate");
	bgCampaign.ScrollFrame:SetPoint("TOPLEFT", bgCampaign, "TOPLEFT", 0, 0);
	bgCampaign.ScrollFrame:SetPoint("BOTTOMRIGHT", bgCampaign, "BOTTOMRIGHT", 0, -43);
	bgCampaign.ScrollFrame:SetClipsChildren(true);
	local childbgCampaign = CreateFrame("Frame", nil, bgCampaign.ScrollFrame);
	childbgCampaign:SetSize(650, 450);
	bgCampaign.ScrollFrame:SetScrollChild(childbgCampaign);
	bgCampaign.ScrollFrame:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel);
	
	bgCampaign.ScrollFrame.text = bgCampaign.ScrollFrame:CreateFontString(nil, "OVERLAY", bgCampaign.ScrollFrame);
	bgCampaign.ScrollFrame.text:SetFontObject("GameFontHighlightLarge");
	bgCampaign.ScrollFrame.text:SetText(core.L["SelectCampaignText"]);
	bgCampaign.ScrollFrame.text:SetPoint("CENTER", bgCampaign.ScrollFrame, "CENTER");


	----- PAGE PROFIL PERSONNAGE -----
	ListCampaign.bgProfile.content = CreateFrame("Frame", "ProfileContent", childbgCampaign, BackdropTemplateMixin and "BackdropTemplate");
	ListCampaign.bgProfile.content:SetSize(650,450);
	ListCampaign.bgProfile.content:Hide();
	ListCampaign.bgProfile.content:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/bg_step_challenge",
							edgeFile = "",
							tile = false, edgeSize = 16, 
							insets = { left = 3, right = 0, top = 0, bottom = 0 }});				
							
	ListCampaign.bgProfile.content.title = ListCampaign.bgProfile.content:CreateFontString(nil, "OVERLAY", ListCampaign.bgProfile.content);
	if GetLocale() == "ruRU" then
		ListCampaign.bgProfile.content.title:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\FRIZQT__CYR.TTF", 20, "OUTLINE");
	else
		ListCampaign.bgProfile.content.title:SetFont("Fonts\\FRIZQT__.TTF", 20, "OUTLINE");
	end
	ListCampaign.bgProfile.content.title:SetText(core.L["Infos Personnage"]);
	ListCampaign.bgProfile.content.title:SetPoint("TOP", ListCampaign.bgProfile.content, "TOP", 0, -15);
	
	--- CURRENCY ---
	local currencyFrame = CreateFrame("Frame", nil, ListCampaign.bgProfile.content, BackdropTemplateMixin and "BackdropTemplate");
	currencyFrame:SetSize(65, 64);
	currencyFrame:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/player_profile/flagCurrency"});
	currencyFrame:SetPoint("TOPLEFT", ListCampaign.bgProfile.content, "TOPLEFT",30, 10);
	currencyFrame.text = currencyFrame:CreateFontString("ProfileContentCurrencyText", "OVERLAY");
	currencyFrame.text:SetFontObject("GameFontHighlightLarge");
	currencyFrame.text:SetText(DBProfile.skillPoints);
	currencyFrame.text:SetPoint("CENTER", currencyFrame, "CENTER", 0, -20);
	currencyFrame.tooltip = CreateFrame("Frame", nil, ListCampaign.bgProfile.content);
	currencyFrame.tooltip:SetSize(40, 64);
	currencyFrame.tooltip:SetPoint("CENTER", currencyFrame, "CENTER");
	currencyFrame.tooltip:SetScript("OnEnter", OnEnterCurrency);
	currencyFrame.tooltip:SetScript("OnLeave", OnLeave);
	
	--- DISPLAY PLAYER MODEL ---
	local ProfileScenePlayerModel = CreateFrame("CinematicModel", "ProfileScenePlayerModel", UIParent, nil);
	ProfileScenePlayerModel:SetSize(650,450);
	ProfileScenePlayerModel:SetPoint("TOPLEFT", ListCampaign.bgProfile.content, "TOPLEFT");
	ProfileScenePlayerModel:SetFrameLevel(11);
	ProfileScenePlayerModel:Hide();
	ProfileScenePlayerModel:SetAnimation(0);
	ProfileScenePlayerModel:SetRotation(0.3);
	if (UIParent:GetScale() < 0.8) then
		ProfileScenePlayerModel:SetPosition(-2, 0.5, 0.6)
	else
		ProfileScenePlayerModel:SetPosition(-2, 0.5, 0.4)
	end
	
	
	--- FRAME PLAYER UPGRADE ---
	ListCampaign.bgProfile.content.upgradeFrame = CreateFrame("Frame", "ProfileContentUpgrade", ListCampaign.bgProfile.content);
	ListCampaign.bgProfile.content.upgradeFrame:SetSize(300,390);
	ListCampaign.bgProfile.content.upgradeFrame:SetPoint("BOTTOMRIGHT", ListCampaign.bgProfile.content, "BOTTOMRIGHT", -65, 10);
	CreateAllUpgrade()
	
	--- PDV ATTACK AND REZ BUTTON ---
	ListCampaign.bgProfile.content.PlayerDamageText = ListCampaign.bgProfile.content:CreateFontString("ProfilePlayerDamageText", "OVERLAY", ListCampaign.bgProfile.content);
	ListCampaign.bgProfile.content.PlayerDamageText:SetFontObject("GameFontHighlightLarge");
	ListCampaign.bgProfile.content.PlayerDamageText:SetPoint("BOTTOMLEFT", ListCampaign.bgProfile.content, "BOTTOMLEFT", 200, 90);
	ListCampaign.bgProfile.content.PlayerDamageText:SetText(DBProfile.damageMax);
	ListCampaign.bgProfile.content.PlayerDamageIcon = CreateFrame("Frame", "ProfilePlayerDamageIcon", ListCampaign.bgProfile.content, BackdropTemplateMixin and "BackdropTemplate");
	ListCampaign.bgProfile.content.PlayerDamageIcon:SetSize(18,17);
	ListCampaign.bgProfile.content.PlayerDamageIcon:SetPoint("RIGHT", "ProfilePlayerDamageText", "LEFT", 0, 0);
	ListCampaign.bgProfile.content.PlayerDamageIcon:SetScript("OnEnter", OnEnterPointsAttack);
	ListCampaign.bgProfile.content.PlayerDamageIcon:SetScript("OnLeave", OnLeave);
	ListCampaign.bgProfile.content.PlayerDamageIcon:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/spells/icon_attack",
						 edgeFile = "", 
						 tile = false, edgeSize = 16,
						 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	
	ListCampaign.bgProfile.content.PlayerLifeIcon = CreateFrame("Frame", "ProfilePlayerLifeIcon", ListCampaign.bgProfile.content, BackdropTemplateMixin and "BackdropTemplate");
	ListCampaign.bgProfile.content.PlayerLifeIcon:SetSize(16,16);
	ListCampaign.bgProfile.content.PlayerLifeIcon:SetPoint("RIGHT", "ProfilePlayerDamageIcon", "LEFT", -10, 0);
	ListCampaign.bgProfile.content.PlayerLifeIcon:SetScript("OnEnter", OnEnterPointsLife);
	ListCampaign.bgProfile.content.PlayerLifeIcon:SetScript("OnLeave", OnLeave);
	ListCampaign.bgProfile.content.PlayerLifeIcon:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/spells/icon_life",
						 edgeFile = "", 
						 tile = false, edgeSize = 16,
						 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	ListCampaign.bgProfile.content.PlayerLifeText = ListCampaign.bgProfile.content:CreateFontString("ProfilePlayerLifeText", "OVERLAY", ListCampaign.bgProfile.content);
	ListCampaign.bgProfile.content.PlayerLifeText:SetFontObject("GameFontHighlightLarge");
	ListCampaign.bgProfile.content.PlayerLifeText:SetPoint("RIGHT", "ProfilePlayerLifeIcon", "RIGHT", -20, 0);
	ListCampaign.bgProfile.content.PlayerLifeText:SetText(DBProfile.pdv.."/"..DBProfile.pdvMax);
	
	ListCampaign.bgProfile.content.btnRez = CreateFrame("Button", "ProfileBtnRez", ListCampaign.bgProfile.content, "UIPanelButtonTemplate");
	ListCampaign.bgProfile.content.btnRez:SetSize(170, 30);
	ListCampaign.bgProfile.content.btnRez:SetText(core.L["BtnRez"]);
	ListCampaign.bgProfile.content.btnRez:SetPoint("TOP", ProfilePlayerDamageIcon, "BOTTOM", -30, -25);
	ListCampaign.bgProfile.content.btnRez:SetScript("OnEnter", OnEnterBtnRez);
	ListCampaign.bgProfile.content.btnRez:SetScript("OnLeave", OnLeave);
	ListCampaign.bgProfile.content.btnRez:SetScript("OnClick", ProfileHealPlayer);
	if (DBProfile.pdvMax == DBProfile.pdv) then
		ListCampaign.bgProfile.content.btnRez:Disable();
	else
		ListCampaign.bgProfile.content.btnRez:Enable();
	end
	----- ----- -----

	for i = 1, (core.libs.tablelength(campaignDB)) do
		CreateButtonCampaign(campaignDB[i], child, childbgCampaign);
	end
	BtnCampaign_Unselected_all();
end
core.CampaignTab = CreateCampaignTab;
