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 g_playerSpellDisabled = false;
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 and EffectCurrPlayer[i].chargeEffect < core.DBEffects[EffectCurrPlayer[i].idEffect].chargeMax) 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);
	GameTooltip:AddDoubleLine(core.L["Points de vie"]..core.L["2points"].."|cffffffff"..currFight[self.id].pdv.."|r", core.L["Points d'attaque"]..core.L["2points"].."|cffffffff"..currFight[self.id].damage.."|r");
	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 and EffectCurrEnnemy[self.id][i].chargeEffect < core.DBEffects[EffectCurrEnnemy[self.id][i].idEffect].chargeMax) 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);
			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
				elseif (core.DBUpgrade[i].list[i2].levelUnlock[1] == 4) then
					if (Campaign4[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
				elseif (DBProfile.bonusLvL[i2] < core.DBUpgrade[i].list[i2].levelMax and core.DBUpgrade[i].list[i2].levelUnlock[DBProfile.bonusLvL[i2] + 1][1] == 4) then
					if (Campaign4[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["FightSceneCharacter1"]:Hide();
		_G["FightSceneCharacter2"]:Hide();
		_G["FightSceneCharacter3"]:Hide();
		_G["FightSceneCharacter4"]:Hide();
		_G["FightSceneCharacter5"]:Hide();
		_G["FightSceneCharacter0"]: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["FightSceneCharacter0"]:Show();
				_G["FightSceneCharacter0"]:EquipItem(core.DBPlayer.weaponId);
				_G["FightSceneCharacter0"]:EquipItem(core.DBPlayer.weaponId);
				if (currFight[3] ~= 0) then
					_G["FightSceneCharacter3"]:Show();
				end
				if (currFight[2] ~= 0) then
					_G["FightSceneCharacter2"]:Show();
				end
				if (currFight[4] ~= 0) then
					_G["FightSceneCharacter4"]:Show();
				end
				if (currFight[1] ~= 0) then
					_G["FightSceneCharacter1"]:Show();
				end
				if (currFight[5] ~= 0) then
					_G["FightSceneCharacter5"]:Show();
				end
				for i = 1, 5 do
					if (currFight[i] ~= 0) then
						if (currFight[i].weaponId[1] ~= nil) then
							_G["FightSceneCharacter"..i]:EquipItem(currFight[i].weaponId[1]);
						end
						if (currFight[i].weaponId[2] ~= nil) then
							_G["FightSceneCharacter"..i]:EquipItem(currFight[i].weaponId[2]);
						end
						if (currFight[i].fxPassif ~= nil) then
							core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneCharacter"..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 and currFight[randVar].typeUnit ~= "object") then
			i = 1;
			return randVar;
		end
	end
end

local function randomBtwnEnnemyDeadOrEmpty(position)
	local i = 0;
	while (i == 0) do
		local tmp = math.random(1, 5)
		if ((PdvCurrEnnemy[tmp] == "nil" or PdvCurrEnnemy[tmp] <= 0) and position ~= tmp) then
			i = 1;
			return tmp;
		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, noBeginPlayer)
	SpellEnCours = boolean;
	if (boolean == true) then
		core.cpgn.ToggleCanPlayPlayerSpell(false);
	else
		core.cpgn.ToggleCanPlayPlayerSpell(true);
		if (noBeginPlayer == nil) then
			core.cpgn.BeginPlayerTurn();
		end
	end
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 ClearBoard(exception)
	for i = 1, 5 do
		if (core.cpgn.findInArray(i, exception) == false) then
			_G["FightSceneCharacter"..i]:Hide();
			currFight[i] = 0;
			PdvCurrEnnemy[i] = "nil";
			DamageCurrEnnemy[i] = "nil";
			CdSpellCurrEnnemy[i] = "nil";
			_G["FSFrameTooltipEnnemy"..i]:Hide();
		end
	end
end

local function checkConditionsSpell(conditions, posSelf)
	local ret = true;
	if (conditions ~= nil) then
		for i = 1, #conditions do
			local pos = nil;
			if (conditions[i].posTarget == -1) then
				pos = posSelf;
			else
				pos = conditions[i].posTarget;
			end
			if (conditions[i].about == "Life") then
				if (pos == 0) then
					if (DBProfile.pdv <= conditions[i].whenArriveAt) then
						ret = true;
					else
						ret = false;
					end
				else
					if (PdvCurrEnnemy[pos] <= conditions[i].whenArriveAt) then
						ret = true;
					else
						ret = false;
					end
				end
			elseif (conditions[i].about == "BoardCleared") then
				if (thereIsAnAlly(posSelf, conditions[i].idException)) then
					ret = false;
				else
					ret = true;
				end
			elseif (conditions[i].about == "NoPositiveEffect") then
				for i = 1, #EffectCurrEnnemy[posSelf] do
					if (EffectCurrEnnemy[posSelf][i] ~= "nil" and EffectCurrEnnemy[posSelf][i].durationEffect ~= -1 and core.DBEffects[EffectCurrEnnemy[posSelf][i].idEffect].positive == true) then
						ret = false;
					end
				end
			end			
		end
	end
	return ret;
end

local function checkNbAllies(checkDeadAllies, posTarget)
	local ret = 0;
	if (checkDeadAllies == true) then
		for i = 1, 5 do
			if (PdvCurrEnnemy[i] ~= "nil" and PdvCurrEnnemy[i] <= 0 and i ~= posTarget) then
				ret = ret + 1;
			end
		end
	else
		for i = 1, 5 do
			if (PdvCurrEnnemy[i] ~= "nil" and PdvCurrEnnemy[i] > 0 and i ~= posTarget) then
				ret = ret + 1;
			end
		end
	end
	return ret;
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 SameNameEvent(nameEvent, whenActive)
	for i = 1, #whenActive do
		if (nameEvent == whenActive[i]) then
			return true;
		end
	end
	return false;
end

local function isGetEffect(nameEvent)
    local i, j = string.find(nameEvent, "GetEffect")
	if (i ~= nil and j ~= nil) then
		if(string.sub(nameEvent, i, j) == "GetEffect") then
			return true
		end
	end
	return false;
end

local function EventEffect(nameEvent, ...)
	local timePlay = 0;
	local args = {...};
	for j = 1, 5 do
		if (EffectCurrEnnemy[j] ~= nil) then
			for i = 1, #EffectCurrEnnemy[j] do
				local currentEffect = core.DBEffects[EffectCurrEnnemy[j][i].idEffect];
				if (EffectCurrEnnemy[j][i] ~= nil and EffectCurrEnnemy[j][i] ~= "nil" and SameNameEvent(nameEvent, currentEffect.whenActive) == true and currentEffect.duration ~= -1) then
				--print("checking effect : "..currentEffect.name);
				--[[if (core.DBEffects[EffectCurrEnnemy[j][i].idEffect].NoTimeplay ~= nil) then
						timePlay = timePlay - 1;
					end]]
					
					if (checkConditionsSpell(currentEffect.conditions, j)) then
						if (nameEvent == "SufferDamageByDotHimself" or 
							nameEvent == "SufferDamageHimself" or 
							nameEvent == "PASSIFSufferDamageHimself" or 
							nameEvent == "DieHimself" or
							nameEvent == "DieHimselfNoWait" or
							nameEvent == "GetEffect"..currentEffect.id or
							--nameEvent == "DieAlly" or
							nameEvent == "UnderAttackHimself" or
							nameEvent == "CustomParadeDuelliste" or
							nameEvent == "HimselfAttacking") then
							if (args[1] == j) then
								if (nameEvent == "PASSIFSufferDamageHimself") then
									core.cpgn.WC_wait(0, core.cpgn.ActionSpell, currentEffect, j);
								else
									core.cpgn.WC_wait(timePlay, core.cpgn.ActionSpell, currentEffect, j);
								end
								if (currentEffect.NoTimeplay == nil) then
									timePlay = timePlay + 1;
								end
							end
						elseif (nameEvent == "DieAlly") then
							if (args[1] ~= j) then
								core.cpgn.WC_wait(timePlay, core.cpgn.ActionSpell, currentEffect, j);
								if (currentEffect.NoTimeplay == nil) then
									timePlay = timePlay + 1;
								end
							end
						else
							if (args[1] == j) then
								core.cpgn.WC_wait(timePlay, core.cpgn.ActionSpell, currentEffect, j);
								if (currentEffect.NoTimeplay == nil) then
									timePlay = timePlay + 1;
								end
							else
								core.cpgn.WC_wait(timePlay, core.cpgn.ActionSpell, currentEffect, j);
								if (currentEffect.NoTimeplay == nil) then
									timePlay = timePlay + 1;
								end
							end
						end
					end
				end
			end
			for i = 1, #EffectCurrEnnemy[j] do
				local currentEffect = core.DBEffects[EffectCurrEnnemy[j][i].idEffect];
				if (EffectCurrEnnemy[j][i] ~= nil and EffectCurrEnnemy[j][i] ~= "nil" and SameNameEvent(nameEvent, currentEffect.whenActive) == true and currentEffect.duration == -1) then
				--print("checking effect : "..currentEffect.name);
				--[[if (core.DBEffects[EffectCurrEnnemy[j][i].idEffect].NoTimeplay ~= nil) then
						timePlay = timePlay - 1;
					end]]
					--print(nameEvent);
					--print("GetEffect"..currentEffect.id);
					if (checkConditionsSpell(currentEffect.conditions, j)) then
						if (nameEvent == "SufferDamageByDotHimself" or
							nameEvent == "SufferDamageHimself" or
							nameEvent == "PASSIFSufferDamageHimself" or
							nameEvent == "DieHimself" or
							nameEvent == "DieHimselfNoWait" or
							--nameEvent == "DieAlly" or
							nameEvent == "UnderAttackHimself" or
							nameEvent == "CustomParadeDuelliste" or
							isGetEffect(nameEvent) or
							nameEvent == "HimselfAttacking") then
							if (args[1] == j) then
								if (nameEvent == "PASSIFSufferDamageHimself") then
									core.cpgn.WC_wait(0, core.cpgn.ActionSpell, currentEffect, j);
								else
									core.cpgn.WC_wait(timePlay, core.cpgn.ActionSpell, currentEffect, j);
								end
								if (currentEffect.NoTimeplay == nil) then
									timePlay = timePlay + 1;
								end
							end
						elseif (nameEvent == "DieAlly") then
							if (args[1] ~= j) then
								core.cpgn.WC_wait(timePlay, core.cpgn.ActionSpell, currentEffect, j);
								if (currentEffect.NoTimeplay == nil) then
									timePlay = timePlay + 1;
								end
							end
						else
							if (args[1] == j) then
								--print(j);
								core.cpgn.WC_wait(timePlay, core.cpgn.ActionSpell, currentEffect, j);
								if (currentEffect.NoTimeplay == nil) then
									timePlay = timePlay + 1;
								end
							else
								core.cpgn.WC_wait(timePlay, core.cpgn.ActionSpell, currentEffect, j);
								if (currentEffect.NoTimeplay == nil) then
									timePlay = timePlay + 1;
								end
							end
						end
					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.ActionSpell, core.DBEffects[EffectCurrPlayer[p].idEffect], j);
			if (core.DBEffects[EffectCurrPlayer[p].idEffect].NoTimeplay == nil) then
				timePlay = timePlay + 1;
			end
		end
	end
	return timePlay;
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["FightSceneCharacter"..i]:SetAnimation(currFight[i].animBase);
						end
						EventEffect("EndEffect"..EffectCurrEnnemy[i][i2].idEffect);
						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["FightSceneCharacter0"]:SetAnimation(core.DBPlayer.animBase);
				end
				EventEffect("EndEffect"..EffectCurrPlayer[j].idEffect);
				EffectCurrPlayer[j] = "nil";
				_G["FSPlayerEffectText"]:SetText(core.cpgn.NbEffectsPlayer());
			end
		end
	end
end

local function MinusCharge(j, id, nbMinus)
	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 - nbMinus;
					if (EffectCurrPlayer[i].chargeEffect <= 0) then
						EventEffect("EndEffect"..EffectCurrPlayer[i].idEffect);
						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 - nbMinus;
					if (EffectCurrEnnemy[j][i].chargeEffect <= 0) then
						EventEffect("EndEffect"..EffectCurrEnnemy[j][i].idEffect);
						EffectCurrEnnemy[j][i] = "nil";
					end
				end
			end
		end
	end
end

local function SummonUnit(posAI, tableSummonUnit, anim, sounds, fx, rez)
	currFight[posAI] = 0;
	iRandSummon = math.random(1, #tableSummonUnit);
	currFight[posAI] = core.DBMobs[tableSummonUnit[iRandSummon]];
	PdvCurrEnnemy[posAI] = core.DBMobs[tableSummonUnit[iRandSummon]].pdv;
	DamageCurrEnnemy[posAI] = core.DBMobs[tableSummonUnit[iRandSummon]].damage;
	CdSpellCurrEnnemy[posAI] = "nil";
	for ispell = 1, #core.DBMobs[tableSummonUnit[iRandSummon]].spells do
		local tmp = {
			idSpell = core.DBSpells[core.DBMobs[tableSummonUnit[iRandSummon]].spells[ispell]].id,
			cdSpell = core.DBSpells[core.DBMobs[tableSummonUnit[iRandSummon]].spells[ispell]].cooldown,
		};
		if (rez == false and core.DBSpells[tmp.idSpell].cooldown > 0) then
			tmp.cdSpell = tmp.cdSpell + 1;
		end
		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["FightSceneCharacter"..posAI]:SetPosition(currFight[posAI].scene[posAI].positionLittle[1], currFight[posAI].scene[posAI].positionLittle[2], currFight[posAI].scene[posAI].positionLittle[3])
	 else
		_G["FightSceneCharacter"..posAI]:SetPosition(currFight[posAI].scene[posAI].position[1], currFight[posAI].scene[posAI].position[2], currFight[posAI].scene[posAI].position[3])
	 end
	core.cpgn.DisplayMobs(currFight[posAI], posAI)
	_G["FSEnnemyDamageText"..posAI]:SetText(DamageCurrEnnemy[posAI]);
	refreshEnnemyLifeFrame(posAI);
	refreshEnnemyDamageFrame(posAI);
	_G["FSFrameTooltipEnnemy"..posAI]:Show();
	refreshCurrentEnnemyTooltip();
	if (fx ~= 0) then
		core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneCharacter"..posAI], fx, true);
	end
	if (sounds ~= 0) then
		core.cpgn.ManyPlaySound(sounds);
	end
	-- if (anim[1] ~= 0) then
		-- _G["FightSceneCharacter"..posAI]:SetAnimation(anim[1]);
	-- end
	EventEffect("OnSummonUnit");
	core.cpgn.WC_wait(anim[3], core.cpgn.setWaitAnimation, _G["FightSceneCharacter"..posAI], currFight[posAI].animBase, posAI, nil);	
	dontCheckVictory = false;
end

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

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 = core.cpgn.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 and (EffectCurrPlayer[res].chargeEffect + 1) <= core.DBEffects[effectId].chargeMax) 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 = core.cpgn.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 and (EffectCurrEnnemy[ennemyPos][res].chargeEffect + 1) <= core.DBEffects[effectId].chargeMax) then
					EffectCurrEnnemy[ennemyPos][res].chargeEffect = EffectCurrEnnemy[ennemyPos][res].chargeEffect + 1;
					if (EffectCurrEnnemy[ennemyPos][res].chargeEffect == EffectCurrEnnemy[ennemyPos][res].EventCharge) then
						EventEffect("ChargeNumber"..EffectCurrEnnemy[ennemyPos][res].idEffect);
					end
				end
			else
				EffectCurrEnnemy[ennemyPos][#EffectCurrEnnemy[ennemyPos] + 1] = tmp;
			end
		end
	end
	EventEffect("GetEffect"..effectId, ennemyPos);
end
core.cpgn.AddEffectOnUnit = AddEffectOnUnit;

local function DamageToSomeone(damage, pos)
	if (pos == 0) then
		core.cpgn.DamageToPlayer(damage);
	else
		PdvCurrEnnemy[pos] = PdvCurrEnnemy[pos] - damage;
		local isEvent = EventEffect("SufferDamageByDotHimself", pos);
		--EventEffect("SufferDamageAlly");
		if (isEvent == 0) then
			if (PdvCurrEnnemy[pos] <= 0) then
				PlaySound(currFight[pos].sound.death, "sfx", false);
				if (currFight[pos].typeUnit == "human") then
					_G["FightSceneCharacter"..pos]:SetAnimation(1);
				elseif (currFight[pos].typeUnit == "object") then
					core.cpgn.WC_wait(0.5, core.cpgn.DieObjectUnit, _G["FightSceneCharacter"..pos]);
				end
				EventEffect("DieHimself", pos);
				EventEffect("DieHimselfNoWait", pos);
				EventEffect("DieAlly", pos);
				EffectCurrEnnemy[pos] = {};
			else
				PlaySound(currFight[pos].sound.hit, "sfx", false);
			end
		end
		refreshEnnemyLifeFrame(pos);
	end
end

local function setWaitingAnimBase(target, anim)
	currFight[target].animBase = anim;
end

local function ActionSpell(spell, posSelf)--, posTarget)
	if (SpellEnCours == false) then
		StaticPopup_Hide("FFfight");
		for iAction = 1, #spell.action do
			if (unitIsStun(posSelf) == false or spell.activeDuringStun ~= nil) then
				local l_spellAction = spell.action[iAction];
				local l_name = spell.action[iAction].name;
				local l_posTarget = -3;
				if (l_spellAction.posTarget ~= nil) then
					if (l_spellAction.posTarget == -1) then
						l_posTarget = posSelf;
					elseif (l_spellAction.posTarget == -2) then
						if (l_name == "SummonUnit") then
							if (thereIsPlace(posSelf)) then
								l_posTarget = randomBtwnEnnemyDeadOrEmpty(posSelf);
							else
								l_posTarget = -3;
							end
						else
							l_posTarget = randomBtwnEnnemyAlive();
						end
					else
						l_posTarget = l_spellAction.posTarget;
					end
					if (l_spellAction.posTargetSide ~= nil) then
						if ((l_posTarget + l_spellAction.posTargetSide) >= 1 and (l_posTarget + l_spellAction.posTargetSide) <= 5) then
							l_posTarget = l_posTarget + l_spellAction.posTargetSide;
						else
							l_posTarget = -3;
						end
					end
				end
				local l_waiting = 0;
				if (l_spellAction.waiting ~= nil ) then
					l_waiting = l_spellAction.waiting;
				end
				local dmg;
				if (spell.action[iAction].damage ~= nil) then
					if (l_spellAction.damage == "current") then
						dmg = DamageCurrEnnemy[posSelf];
					else
						dmg = l_spellAction.damage;
					end
				end
				local continue = 1;
				if (spell.chanceSuccess ~= nil) then
					continue = math.random(spell.chanceSuccess[1], spell.chanceSuccess[2]);
				end
				if (continue == 1) then
					local isEventDodge = 0;
					if (l_spellAction.canBeDodge ~= nil) then
						isEventDodge = EventEffect("BeforeSufferDamagePlayer");
					end
					
					if (l_spellAction.animSelf ~= nil and l_spellAction.animSelf[1] ~= 0) then
						if  (l_spellAction.animSelf[1] == "base") then
							core.cpgn.WC_wait(l_spellAction.animSelf[2], core.cpgn.setWaitAnimation, _G["FightSceneCharacter"..posSelf], currFight[posSelf].animBase, posSelf, l_spellAction.varAnim);
						else
							core.cpgn.WC_wait(l_spellAction.animSelf[2], core.cpgn.setWaitAnimation, _G["FightSceneCharacter"..posSelf], l_spellAction.animSelf[1], posSelf, l_spellAction.varAnim);
						end
						if (l_spellAction.animSelf[3] ~= 0) then
							core.cpgn.WC_wait(l_spellAction.animSelf[3], core.cpgn.setWaitAnimation, _G["FightSceneCharacter"..posSelf], currFight[posSelf].animBase, posSelf, nil);
						end
					end
					if (l_spellAction.soundSelf ~= nil and l_spellAction.soundSelf ~= 0) then
						core.cpgn.WC_wait(l_spellAction.animSelf[2], core.cpgn.ManyPlaySound, l_spellAction.soundSelf);
					end
					if (l_spellAction.fxSelf ~= nil and l_spellAction.fxSelf ~= 0) then
						if (l_spellAction.notOneShotSelf ~= nil) then
							core.cpgn.WC_wait(l_spellAction.animSelf[2], core.cpgn.ApplySpellVisualOnUnit, _G["FightSceneCharacter"..posSelf], l_spellAction.fxSelf, false);
						else
							core.cpgn.WC_wait(l_spellAction.animSelf[2], core.cpgn.ApplySpellVisualOnUnit, _G["FightSceneCharacter"..posSelf], l_spellAction.fxSelf, true);
						end
					end
					if (isEventDodge == 0) then
						if (l_posTarget ~= -3 and l_spellAction.animTarget ~= nil and l_spellAction.animTarget[1] ~= 0) then-- and currFight[l_posTarget].noAnimWhenDamage == nil) then
							if  (l_spellAction.animTarget[1] == "base") then
								core.cpgn.WC_wait(l_spellAction.animTarget[2], core.cpgn.setWaitAnimation, _G["FightSceneCharacter"..l_posTarget], currFight[l_posTarget].animBase, l_posTarget, l_spellAction.varAnim);
							else
								core.cpgn.WC_wait(l_spellAction.animTarget[2], core.cpgn.setWaitAnimation, _G["FightSceneCharacter"..l_posTarget], l_spellAction.animTarget[1], l_posTarget, l_spellAction.varAnim);
							end
							if (l_spellAction.animTarget[3] ~= 0) then
								if (l_name ~= "SummonUnit") then
									core.cpgn.WC_wait(l_spellAction.animTarget[3], core.cpgn.setWaitAnimation, _G["FightSceneCharacter"..l_posTarget], currFight[l_posTarget].animBase, l_posTarget, nil);
								end
							end
						end
						if (l_name ~= "SummonUnit" and l_posTarget ~= -3 and l_spellAction.soundTarget ~= nil and l_spellAction.soundTarget ~= 0) then
							core.cpgn.WC_wait(l_spellAction.animTarget[2], core.cpgn.ManyPlaySound, l_spellAction.soundTarget);
						end
						if (l_name ~= "SummonUnit" and l_posTarget ~= -3 and l_spellAction.fxTarget ~= nil and l_spellAction.fxTarget ~= 0) then
							if (l_spellAction.notOneShotTarget ~= nil) then
								core.cpgn.WC_wait(l_spellAction.animTarget[2], core.cpgn.ApplySpellVisualOnUnit, _G["FightSceneCharacter"..l_posTarget], l_spellAction.fxTarget, false);
							else
								core.cpgn.WC_wait(l_spellAction.animTarget[2], core.cpgn.ApplySpellVisualOnUnit, _G["FightSceneCharacter"..l_posTarget], l_spellAction.fxTarget, true);
							end
						end

						--- EVENT ---
						if (l_name == "HimselfAttacking") then
							core.cpgn.WC_wait(l_waiting, EventEffect,"HimselfAttacking", posSelf);
							--EventEffect("HimselfAttacking", posSelf);
						end
						--- // FIN EVENT //---

						if (l_name == "Nothing") then
							refreshCurrentEnnemyTooltip();
						elseif (l_name == "ResetVisual") then
							if (l_posTarget == 0) then
								_G["FightSceneCharacter0"]:SetDisplayInfo(core.DBPlayer.displayId);
								_G["FightSceneCharacter0"]:EquipItem(core.DBPlayer.weaponId);
								_G["FightSceneCharacter0"]:EquipItem(core.DBPlayer.weaponId);
							else
								DisplayMobs(currFight[l_posTarget].id, l_posTarget);
							end
						elseif (l_name == "AddEffect") then --- // ADD_EFFECT
							core.cpgn.WC_wait(l_waiting, core.cpgn.AddEffectOnUnit, l_posTarget, l_spellAction.idEffect);
							refreshCurrentEnnemyTooltip();
						elseif (l_name == "SetAnimBase") then --- // SET_ANIM_BASE
							core.cpgn.WC_wait(l_waiting, setWaitingAnimBase, l_posTarget, l_spellAction.animBase);
						elseif (l_name == "DamageBasic") then --- // DAMAGE_BASIC
							core.cpgn.WC_wait(l_spellAction.animSelf[3]/2, DamageToSomeone, dmg, l_posTarget);
						elseif (l_name == "AddCD") then --- // ADD_CD
							if (l_posTarget == 0) then
								for i = 3, #CdSpellPlayer do
									if (CdSpellPlayer[i] ~= nil) then
										CdSpellPlayer[i] = CdSpellPlayer[i] + 1;
									end
								end
								RenverseThisLap = true;
								refreshPlayerSpellFrameForCD();
							else -- VERIFIER SI LE CODE EN DESSOUS MARCHE
								for i = 1, #CdSpellCurrEnnemy[l_posTarget] do
									if (core.DBSpells[CdSpellCurrEnnemy[l_posTarget][i].idSpell].cooldown > 0) then
										CdSpellCurrEnnemy[l_posTarget][i].cdSpell = CdSpellCurrEnnemy[l_posTarget][i].cdSpell + 1;
									end
								end
							end -- VERIFIER SI LE CODE AU DESSUS MARCHE
							refreshCurrentEnnemyTooltip();
						elseif (l_name == "ModifCDOnSpell") then --- // MODIF_CD_ON_SPELL
							if (l_posTarget == 0) then
								if (CdSpellPlayer[3] ~= nil and l_spellAction.idSpell == 9) then
									CdSpellPlayer[3] = CdSpellPlayer[3] + l_spellAction.nbModif;
								elseif (CdSpellPlayer[4] ~= nil and l_spellAction.idSpell == 10) then
									CdSpellPlayer[4] = CdSpellPlayer[4] + l_spellAction.nbModif;
								elseif (CdSpellPlayer[5] ~= nil and l_spellAction.idSpell == 11) then
									CdSpellPlayer[5] = CdSpellPlayer[5] + l_spellAction.nbModif;
								elseif (CdSpellPlayer[6] ~= nil and l_spellAction.idSpell == 32) then
									CdSpellPlayer[6] = CdSpellPlayer[6] + l_spellAction.nbModif;
								elseif (CdSpellPlayer[7] ~= nil and l_spellAction.idSpell == 43) then
									CdSpellPlayer[7] = CdSpellPlayer[7] + l_spellAction.nbModif;
								elseif (CdSpellPlayer[8] ~= nil and l_spellAction.idSpell == 33) then
									CdSpellPlayer[8] = CdSpellPlayer[8] + l_spellAction.nbModif;
								end
								RenverseThisLap = true;
								refreshPlayerSpellFrameForCD();
							else
								if (CdSpellCurrEnnemy[l_posTarget] ~= nil) then
									if (l_spellAction.idSpell == 0) then
										for i = 1, #CdSpellCurrEnnemy[l_posTarget] do
											if (CdSpellCurrEnnemy[l_posTarget][i] ~= nil and CdSpellCurrEnnemy[l_posTarget][i].cdSpell >= 0) then
												CdSpellCurrEnnemy[l_posTarget][i].cdSpell = CdSpellCurrEnnemy[l_posTarget][i].cdSpell + l_spellAction.nbModif;
												if (CdSpellCurrEnnemy[l_posTarget][i].cdSpell < 0) then
													CdSpellCurrEnnemy[l_posTarget][i].cdSpell = 0;
												end
											end
										end
									else
										for i = 1, #CdSpellCurrEnnemy[l_posTarget] do
											if (CdSpellCurrEnnemy[l_posTarget][i].idSpell == l_spellAction.idSpell) then
												CdSpellCurrEnnemy[l_posTarget][i].cdSpell = CdSpellCurrEnnemy[l_posTarget][i].cdSpell + l_spellAction.nbModif;
												if (CdSpellCurrEnnemy[l_posTarget][i].cdSpell < 0) then
													CdSpellCurrEnnemy[l_posTarget][i].cdSpell = 0;
												end
											end
										end
									end
								end
							end
							refreshCurrentEnnemyTooltip();
						elseif (l_name == "EndPlayerTurn") then --- // END_PLAYER_TURN
								if (canPlay == 1) then
									canPlay = 0;
									core.cpgn.endTurnPlayer(false);
								end
						elseif (l_name == "PauseAnim") then --- // PAUSE_ANIM
							if (l_posTarget ~= -3) then
								_G["FightSceneCharacter"..l_posTarget]:SetPaused(l_spellAction.value)
							end
						elseif (l_name == "MinusCharge") then --- // MINUS_CHARGE
							if (l_posTarget ~= -3) then
								MinusCharge(l_posTarget, l_spellAction.idEffect, l_spellAction.nbMinus);
							end
						elseif (l_name == "ResetCooldown") then --- // RESET_COOLDOWN
							for i = 1, #CdSpellCurrEnnemy[l_posTarget] do
								if (CdSpellCurrEnnemy[l_posTarget][i].idSpell == spell.id) then
									CdSpellCurrEnnemy[l_posTarget][i].cdSpell = spell.cooldown + 1;
								end
							end
						elseif (l_name == "SummonUnit" and l_posTarget ~= -3) then --- // SUMMON_UNIT
							if (l_spellAction.rez ~= nil) then
								core.cpgn.WC_wait(l_spellAction.timeToSummon, SummonUnit, l_posTarget, l_spellAction.idUnits, l_spellAction.animTarget, l_spellAction.soundTarget, l_spellAction.fxTarget, l_spellAction.rez);
							elseif (thereIsPlace(posSelf)) then
								core.cpgn.WC_wait(l_spellAction.timeToSummon, SummonUnit, l_posTarget, l_spellAction.idUnits, l_spellAction.animTarget, l_spellAction.soundTarget, l_spellAction.fxTarget, l_spellAction.rez);
							end
						elseif (l_name == "DieObject") then
							core.cpgn.WC_wait(1.15, core.cpgn.DieObjectUnit, _G["FightSceneCharacter"..l_posTarget]);
						elseif (l_name == "SetDamage") then  --- // SET_DAMAGE
							if (l_spellAction.setTo == "base") then
								DamageCurrEnnemy[l_posTarget] = core.DBMobs[currFight[l_posTarget].id].damage;
							else
								DamageCurrEnnemy[l_posTarget] = l_spellAction.setTo;
							end
							refreshEnnemyDamageFrame(l_posTarget);
						elseif (l_name == "ModifDamage") then --- // MODIF_DAMAGE
							DamageCurrEnnemy[l_posTarget] = DamageCurrEnnemy[l_posTarget] + l_spellAction.nbModif;
							refreshEnnemyDamageFrame(l_posTarget);
						elseif (l_name == "SetLife") then --- // SET_LIFE
							if (l_spellAction.setTo == "base") then
								PdvCurrEnnemy[l_posTarget] = core.DBMobs[currFight[l_posTarget].id].pdv;
							else
								PdvCurrEnnemy[l_posTarget] = l_spellAction.setTo;
							end
							refreshEnnemyLifeFrame(l_posTarget);
						elseif (l_name == "ModifLife") then --- // MODIF_LIFE
							if (l_posTarget == 0) then
								core.DBPlayer.pdv = core.DBPlayer.pdv + l_spellAction.nbModif;
								core.cpgn.refreshPlayerLifeFrame(_G["FSPlayerLifeText"], false);
							else
								local addingLife = 0;
								if (l_spellAction.nbModif == "current") then
									addingLife = DamageCurrEnnemy[l_posTarget];
								else
									addingLife = l_spellAction.nbModif;
								end
								PdvCurrEnnemy[l_posTarget] = PdvCurrEnnemy[l_posTarget] + addingLife;
								if (l_spellAction.passMax == nil and PdvCurrEnnemy[l_posTarget] > currFight[l_posTarget].pdv) then
									PdvCurrEnnemy[l_posTarget] = currFight[l_posTarget].pdv;
								end
								refreshEnnemyLifeFrame(l_posTarget);
							end
						elseif (l_name == "ToggleCanPlayPlayerSpell") then
							core.cpgn.ToggleCanPlayPlayerSpell(l_spellAction.value);
							g_playerSpellDisabled = (not value);
						elseif (l_name == "SpellEnCours") then --- // SPELL_EN_COURS
							core.cpgn.WC_wait(l_waiting, setSpellEnCours, l_spellAction.spellEnCours, l_spellAction.noBeginPlayer);
						elseif (l_name == "SetBg") then --- // SET_BG
							core.cpgn.WC_wait(l_waiting, core.cpgn.ChangeBgFight, l_spellAction.bg);
						elseif (l_name == "ClearBoard") then --- // CLEAR_BOARD
							core.cpgn.WC_wait(l_waiting, ClearBoard, l_spellAction.posException);
						elseif (l_name == "DoSomethingByNbAllies") then --- // DO_SOMETHING_BY_NB_ALLIES
							local nbLoop = checkNbAllies(l_spellAction.checkDeadAllies, l_posTarget);
							local addingWait = l_waiting;
							for i = 1, nbLoop do
								if (l_spellAction.idSpell ~= nil) then
									core.cpgn.WC_wait(l_waiting, ActionSpell, core.DBSpells[l_spellAction.idSpell], posSelf);
								elseif (l_spellAction.idEffect ~= nil) then
									core.cpgn.WC_wait(l_waiting, ActionSpell, core.DBEffects[l_spellAction.idEffect], posSelf);
								end
								l_waiting = l_waiting + addingWait;
							end
						end
					end
				end
			end
		end
		refreshCurrentEnnemyTooltip();
	end
end
core.cpgn.ActionSpell = ActionSpell;

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, varAnim)
	if (currFight[position] ~= 0) then
		local pausing = _G["FightSceneCharacter"..position]:GetPaused()
		if (pausing == false) then
			if (position == 0) then
				if (DBProfile.pdv > 0) then
						frameCharacter:SetAnimation(animId, varAnim);
				end
			else
				if (PdvCurrEnnemy[position] ~= "nil" and PdvCurrEnnemy[position] > 0) then
					if (unitIsStun(position) == true) then
						frameCharacter:SetAnimation(14);
					else
						if (currFight[position].noAnimWhenDamage ~= nil and anim == 9) then
							nothing();
						else
							frameCharacter:SetAnimation(animId, varAnim);
						end
					end
				end
			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
		currFight[0] = {};
		currFight[0].animBase = core.DBPlayer.animBase;
		_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["FightSceneCharacter0"]:SetDisplayInfo(core.DBPlayer.displayId);
		_G["FightSceneCharacter0"]:SetAnimation(core.DBPlayer.animBase);
		_G["FightSceneCharacter0"]:SetRotation(core.DBPlayer.rotation);
		if (UIParent:GetScale() < 0.8 and core.DBPlayer.positionLittle ~= nil) then
			_G["FightSceneCharacter0"]:SetPosition(core.DBPlayer.positionLittle[1], core.DBPlayer.positionLittle[2], core.DBPlayer.positionLittle[3])
		else
			_G["FightSceneCharacter0"]:SetPosition(core.DBPlayer.position[1], core.DBPlayer.position[2], core.DBPlayer.position[3])
		end
		_G["FightSceneCharacter0"]:Show();
		_G["FightSceneCharacter0"]:EquipItem(core.DBPlayer.weaponId);
		_G["FightSceneCharacter0"]: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;
		EventEffect("Beginning");
		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

----- ACTION MOB ----
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
			if (checkConditionsSpell(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].conditions, position)) then
				EventEffect("CastSpell"..CdSpellCurrEnnemy[position][i].idSpell);
				ActionSpell(core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell], position)--, 0)
			else
				CdSpellCurrEnnemy[position][i].cdSpell = core.DBSpells[CdSpellCurrEnnemy[position][i].idSpell].cooldown + 1;
				actionMob(position);
			end
			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
	elseif (zoneIdFight == 4) then
		if (Campaign4[fightId].done == false) then
			HealPlayerWhenWin();
		end
	end
	StaticPopup_Show("VictoryPopup");
end

local function BeginPlayerTurn()
	--print("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();
			g_playerSpellDisabled = false;
		end
		refreshCurrentEnnemyTooltip();
	end
end
core.cpgn.BeginPlayerTurn = BeginPlayerTurn;

local function MinusOneCDEnnemySpell()
	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
end

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 ---
	local function EndAITurn2()
		local timePhasing = 0;
		if (DBProfile.pdv > 0) then
			timePhasing = EventEffect("AfterAllPhasing");
		end
		core.cpgn.WC_wait(timePhasing, MinusOneCDEnnemySpell);
		core.cpgn.WC_wait(timePhasing+0.15, core.cpgn.BeginPlayerTurn);
	end
	local timeEffect = 0
	if (DBProfile.pdv > 0) then
		timeEffect = EventEffect("EndAllEnnemyTurn")
	end
	core.cpgn.WC_wait(timeEffect, EndAITurn2);
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
		if (g_playerSpellDisabled == false) then
			refreshPlayerSpellFrameForCD();
		end
		if (... == nil) then
			canPlay = 0;
			AITurn();
		else
			canPlay = 1;
		end
	end
end
core.cpgn.endTurnPlayer = endTurnPlayer;

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["FightSceneCharacter0"]:SetRotation(3.5);
					elseif (self.position == 2) then
						_G["FightSceneCharacter0"]:SetRotation(3.25);
					elseif (self.position == 3) then
						_G["FightSceneCharacter0"]:SetRotation(3);
					elseif (self.position == 4) then
						_G["FightSceneCharacter0"]:SetRotation(2.5);
					elseif (self.position == 5) then
						_G["FightSceneCharacter0"]:SetRotation(2.3);
					end
					if (selectedSpell == 4) then
						core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundCharac, "sfx");
						_G["FightSceneCharacter0"]: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["FightSceneCharacter"..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["FightSceneCharacter"..self.position]:SetAnimation(1);
									elseif (currFight[self.position].typeUnit == "object") then
										core.cpgn.WC_wait(0.5, core.cpgn.DieObjectUnit, _G["FightSceneCharacter"..self.position]);
									end
									isDieEffectvar = EventEffect("DieHimself", self.position);
									EventEffect("DieHimselfNoWait", self.position);
									EventEffect("DieAlly", self.position);
									EffectCurrEnnemy[self.position] = {};
								else
									PlaySound(currFight[self.position].sound.hit, "sfx", false);
									if (currFight[self.position].noAnimWhenDamage == nil) then
										_G["FightSceneCharacter"..self.position]:SetAnimation(9);
									end
									if ((isPassifEffectvar == 0 or unitIsStun(self.position) == true) and currFight[self.position].noAnimWhenDamage == nil) then
										core.cpgn.WC_wait(0.50, core.cpgn.setWaitAnimation, _G["FightSceneCharacter"..self.position], currFight[self.position].animBase, self.position, nil);
									end
								end
							end	
							
								core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim+0.2, core.cpgn.setWaitAnimation, _G["FightSceneCharacter0"], core.DBPlayer.animBase, 0, nil);
							if (isDieEffectvar == 0) then
								core.cpgn.endTurnPlayer(false);
							else
								core.cpgn.WC_wait(3, core.cpgn.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["FightSceneCharacter0"]: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["FightSceneCharacter"..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["FightSceneCharacter"..self.position]:SetAnimation(1);
									elseif (currFight[self.position].typeUnit == "object") then
										core.cpgn.WC_wait(0.5, core.cpgn.DieObjectUnit, _G["FightSceneCharacter"..self.position]);
									end
									isDieEffectvar = EventEffect("DieHimself", self.position);
									EventEffect("DieHimselfNoWait", self.position);
									EventEffect("DieAlly", self.position);
									EffectCurrEnnemy[self.position] = {};
								else
									PlaySound(currFight[self.position].sound.hit, "sfx", false);
									if (currFight[self.position].noAnimWhenDamage == nil) then
										_G["FightSceneCharacter"..self.position]:SetAnimation(9);
									end
									core.cpgn.AddEffectOnUnit(self.position, core.DBSpells[selectedSpell].effectId);
									if ((isPassifEffectvar == 0 or unitIsStun(self.position) == true) and currFight[self.position].noAnimWhenDamage == nil) then
										core.cpgn.WC_wait(0.50, core.cpgn.setWaitAnimation, _G["FightSceneCharacter"..self.position], currFight[self.position].animBase, self.position, nil);
									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["FightSceneCharacter0"], core.DBPlayer.animBase, 0, nil);
							if (isDieEffectvar == 0) then
								core.cpgn.endTurnPlayer(false);
							else
								core.cpgn.WC_wait(3, core.cpgn.endTurnPlayer, false);	
							end
							EventEffect("CastSpell9");
						end
						core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim/3, afterHit);
					elseif (selectedSpell == 10) then
						core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundCharac, "sfx");
						_G["FightSceneCharacter0"]: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["FightSceneCharacter"..self.position], core.DBSpells[selectedSpell].fxTarget, true);
								_G["FightSceneCharacter"..self.position]:SetAnimation(14);
								core.cpgn.AddEffectOnUnit(self.position, core.DBSpells[selectedSpell].effectId);
							end
							CdSpellPlayer[core.DBSpells[selectedSpell].positionId] = core.DBSpells[selectedSpell].cooldown;
							core.cpgn.endTurnPlayer(false, "CanStartOver");
							EventEffect("CastSpell10");
						end
						core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim/3, afterHit);
						core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim, core.cpgn.setWaitAnimation, _G["FightSceneCharacter0"], core.DBPlayer.animBase, 0, nil);
					elseif (selectedSpell == 43) then
						core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundCharac, "sfx");
						_G["FightSceneCharacter0"]: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["FightSceneCharacter"..self.position]:SetAnimation(121);
								for i = 1, #CdSpellCurrEnnemy[self.position] do
									if (core.DBSpells[CdSpellCurrEnnemy[self.position][i].idSpell].cooldown > 0) 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["FightSceneCharacter"..self.position], currFight[self.position].animBase, self.position, nil);
							end
							CdSpellPlayer[core.DBSpells[selectedSpell].positionId] = core.DBSpells[selectedSpell].cooldown;
							core.cpgn.endTurnPlayer(false, "CanStartOver");
							EventEffect("CastSpell43");
						end
						core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim/3, afterHit);
						core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim, core.cpgn.setWaitAnimation, _G["FightSceneCharacter0"], core.DBPlayer.animBase, 0, nil);
					elseif (selectedSpell == 32) then
						DisabledSpells(core.DBSpells[selectedSpell].positionId);
						core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundCharac, "sfx");
						_G["FightSceneCharacter0"]: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["FightSceneCharacter"..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["FightSceneCharacter"..self.position]:SetAnimation(1);
									elseif (currFight[self.position].typeUnit == "object") then
										core.cpgn.WC_wait(0.5, core.cpgn.DieObjectUnit, _G["FightSceneCharacter"..self.position]);
									end
									isDieEffectvar = EventEffect("DieHimself", self.position);
									EventEffect("DieHimselfNoWait", self.position);
									EventEffect("DieAlly", self.position);
									EffectCurrEnnemy[self.position] = {};
								else
									PlaySound(currFight[self.position].sound.hit, "sfx", false);
									if (currFight[self.position].noAnimWhenDamage == nil) then
										_G["FightSceneCharacter"..self.position]:SetAnimation(9);
									end
									if ((isPassifEffectvar == 0 or unitIsStun(self.position) == true) and currFight[self.position].noAnimWhenDamage == nil) then
										core.cpgn.WC_wait(0.50, core.cpgn.setWaitAnimation, _G["FightSceneCharacter"..self.position], currFight[self.position].animBase, self.position, nil);
									end
								end
							end
								core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim+0.2, core.cpgn.setWaitAnimation, _G["FightSceneCharacter0"], core.DBPlayer.animBase, 0, nil);
							canPlay = 1;
							if (isDieEffectvar == 0 and nbPlay == 2) then
								canPlay = 0;
								CdSpellPlayer[core.DBSpells[selectedSpell].positionId] = core.DBSpells[selectedSpell].cooldown;
								core.cpgn.endTurnPlayer(false);
							elseif (nbPlay == 2) then
								canPlay = 0;
								CdSpellPlayer[core.DBSpells[selectedSpell].positionId] = core.DBSpells[selectedSpell].cooldown;
								core.cpgn.WC_wait(3, core.cpgn.endTurnPlayer, false);	
							end
							if (isDieEffectvar == 0 and checkVictory() and dontCheckVictory == false and nbPlay == 1) then
								PlayerWin();	
							end
							nbPlay = nbPlay + 1;
							local test = EventEffect("CastSpell32");
							if (test > 0) then
								selectedSpell = 0;
								--canPlay = 0;
							end
						end
						core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim/2, afterHit);
					end
					core.cpgn.WC_wait(core.DBSpells[selectedSpell].timeAnim+0.2, setWaitRotation, _G["FightSceneCharacter0"], 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["FightSceneCharacter0"]:SetAnimation(core.DBSpells[selectedSpell].animation);
				local function afterHit()
					EventEffect("CastSpell6");
					core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundTarget);
					core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneCharacter0"], 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["FightSceneCharacter0"], core.DBPlayer.animBase, 0, nil);
					core.cpgn.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["FightSceneCharacter0"], core.DBPlayer.animBase, 0, nil);
			elseif (selectedSpell == 11) then
				_G["FightSceneCharacter0"]:SetAnimation(core.DBSpells[selectedSpell].animation);
				core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundCharac);
				core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneCharacter0"], 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;
				local damage = core.DBSpells[selectedSpell].damage + checkNbAllies(false, 0);
				if (damage > DBProfile.damage) then
					damage = DBProfile.damage;
				end
				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] - damage;
							core.cpgn.ManyPlaySound(core.DBSpells[selectedSpell].soundTarget);
							core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneCharacter"..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["FightSceneCharacter"..i]:SetAnimation(1);
								elseif (currFight[i].typeUnit == "object") then
									core.cpgn.WC_wait(0.25, core.cpgn.DieObjectUnit, _G["FightSceneCharacter"..i]);
								end
								isDieEffectvar = EventEffect("DieHimself", i);
								EventEffect("DieHimselfNoWait", i);
								EventEffect("DieAlly", i);
								if (thereIsDieEffect == false and isDieEffectvar ~= 0) then
									thereIsDieEffect = true;
								end							
								EffectCurrEnnemy[i] = {};
							else
								PlaySound(currFight[i].sound.hit, "sfx", false);
								if (currFight[i].noAnimWhenDamage == nil) then
									_G["FightSceneCharacter"..i]:SetAnimation(9);
								end
								if ((isPassifEffectvar == 0 or unitIsStun(i) == true) and currFight[i].noAnimWhenDamage == nil) then
									core.cpgn.WC_wait(0.50, core.cpgn.setWaitAnimation, _G["FightSceneCharacter"..i], currFight[i].animBase, i, nil);
								end
							end
							refreshEnnemyLifeFrame(i);
						end
					end
				end
				EventEffect("CastSpell11");
				CdSpellPlayer[core.DBSpells[selectedSpell].positionId] = core.DBSpells[selectedSpell].cooldown;
				core.cpgn.WC_wait(core.DBSpells[11].timeAnim, core.cpgn.setWaitAnimation, _G["FightSceneCharacter0"], core.DBPlayer.animBase, 0, nil);
				if (thereIsDieEffect == false) then
					core.cpgn.endTurnPlayer(false);
				else
					core.cpgn.WC_wait(3, core.cpgn.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["FightSceneCharacter0"]:SetAnimation(core.DBSpells[selectedSpellAnytime].animation);
				core.cpgn.ApplySpellVisualOnUnit(_G["FightSceneCharacter0"], 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["FightSceneCharacter0"], core.DBPlayer.animBase, 0, nil);
				selectedSpellAnytime = 0;
				EventEffect("CastSpell33");
			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["FightSceneCharacter"..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["FightSceneCharacter"..i]:Hide();
		end
	end
	DBProfile.pdv = DBProfile.pdvMax;
	EffectCurrEnnemy = { {}, {}, {}, {}, {} };
	currFight = {0, 0, 0, 0, 0};
	BeginFight(frame);
end


local function AbandonFight()
	core.cpgn.endTurnPlayer(true);
	CdSpellCurrEnnemy = { {}, {}, {}, {}, {} };
	EffectCurrEnnemy = { {}, {}, {}, {}, {} };
	CdSpellPlayer = {};
	for i = 1, 5 do
		_G["FightSceneCharacter"..i]:Hide();
		_G["FSFrameTooltipEnnemy"..i]:Hide();
		currFight[i] = 0;
	end
	core.cpgn.refreshPlayerLifeFrame(_G["BtnProfilePlayerLifeText"], false);
	_G["FightSceneCharacter0"]: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
	elseif (idZone == 4) then
		for i = 1, #Campaign4 do
			if (Campaign4[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 Campaign4[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"]);
		elseif (zoneIdFight == 4) then
			Campaign4[fightId].done = true;
			core.cpgn.BtnCampaign_OnClick(_G["CampaignButton4"]);
		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
	if (Campaign4[#Campaign4].done == true) then
		_G["CampaignButton4"]:SetNormalTexture(core.DBcampaign[4].icon.."_done");
		_G["CampaignButton4"]:SetHighlightTexture(core.DBcampaign[4].icon.."_done");
		_G["CampaignButton4"]:SetScript("OnClick", core.cpgn.BtnCampaign_OnClick);
	elseif (Campaign3[#Campaign3].done == false) then
		_G["CampaignButton4"]:SetNormalTexture(core.DBcampaign[4].icon.."_lock");
		_G["CampaignButton4"]:SetHighlightTexture("");
		_G["CampaignButton4"]:SetScript("OnClick", nothing);
	else
		_G["CampaignButton4"]:SetNormalTexture(core.DBcampaign[4].icon);
		_G["CampaignButton4"]:SetHighlightTexture(core.DBcampaign[4].icon);
		_G["CampaignButton4"]: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
		elseif (self.campaignId == 4) then
			if (Campaign4[#Campaign4].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"]:SetRotation(0.3);
		_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", "FightSceneCharacter1", 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", "FightSceneCharacter2", 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", "FightSceneCharacter3", 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", "FightSceneCharacter4", 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", "FightSceneCharacter5", 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", "FightSceneCharacter0", 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", -33, -27);
	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("TOPRIGHT", ListCampaign.bgProfile, "TOPRIGHT", -10, -27);
	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("TOPRIGHT", ListCampaign.bgProfile, "TOPRIGHT",  -10, -7);
	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("TOPRIGHT", ListCampaign.bgProfile.content, "TOPRIGHT", -20, 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(275,332);
	ProfileScenePlayerModel:SetPoint("TOPLEFT", ListCampaign.bgProfile.content, "TOPLEFT", 19, 0);
	ProfileScenePlayerModel:SetFrameLevel(11);
	ProfileScenePlayerModel:Hide();
	ProfileScenePlayerModel:SetAnimation(0);
	ProfileScenePlayerModel:SetRotation(0.3);
	ProfileScenePlayerModel.cursorX = 0;
	ProfileScenePlayerModel:SetScript("OnMouseDown", core.libs.ModelOnMouseDown)
	ProfileScenePlayerModel:SetScript("OnMouseUp", core.libs.ModelOnMouseUp)
	ProfileScenePlayerModel:SetPosition(0, 0.8, -0.25)
	
	
	--- 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;
