local _, core = ...;

StaticPopupDialogs["WC_ImpExport_NOACTIVE"] = {
	text = core.L["Veuillez activer l'addon WoWChallenger_ImpExport."],
	button1 = "Ok",
	hideOnEscape = false,
	whileDead = true,
}

StaticPopupDialogs["NotEnoughSkillPoints"] = {
	text = core.L["Vous n'avez plus de points de compétence."],
	button1 = "Ok",
	hideOnEscape = false,
	whileDead = true,
}

local OnEnterCurrencyAdventure = function(self, motion)
	GameTooltip:SetOwner(self, self.anchor, 8, -13);
	GameTooltip:AddLine(core.L["skillpointsMAJ"], 1, 1, 1);
	GameTooltip:AddLine(core.L["DescPointsCompetAdventures"], 1, 1, 1, true);
	GameTooltip:AddLine(" ");
	GameTooltip:AddLine(core.L["Points partagés avec les campagnes."]);
	GameTooltip:AddLine(" ");
	GameTooltip:AddLine(core.L["Total"]..core.L["2points"].."|cffffffff"..DBProfile.skillPoints.."/50|r");
	GameTooltip:Show();
end

btnFightSelected = {
	WC  = nil,
	WCC = nil
};
local btnListUnitEncouterSelected = {
	WC  = nil,
	WCC = nil
};

function RefreshPortraitFight(frame, id, addon)
	if not frame and btnFightSelected[addon] then
		frame = btnFightSelected[addon].portrait.creature;
	end
	if frame then
		frame:SetColorTexture(1, 1, 1, 0);
		if id and id ~= "" then
			local unit = WC_Lib.SearchThingInAdventureByID(id, 2, addon);
			SetPortraitTextureFromCreatureDisplayID(frame, unit.displayId);
		end
	end
end

local function ShowInfoModelUnitAdventure(infoUnit, addon)
	if not _G[addon.."_InfoModelUnitAdventure"] then
		local mainFrame = CreateFrame("Frame", addon.."_InfoModelUnitAdventure", _G[addon.."_CharactersInfoAdventure"], BackdropTemplateMixin and "BackdropTemplate");		
		mainFrame:SetSize(_G[addon.."_CharactersInfoAdventure"]:GetWidth(), _G[addon.."_CharactersInfoAdventure"]:GetHeight() - 3);
		mainFrame:SetPoint("BOTTOMRIGHT", _G[addon.."_CharactersInfoAdventure"], "BOTTOMRIGHT", 0, 3);
		
		mainFrame.model = CreateFrame("DressUpModel", nil, mainFrame, BackdropTemplateMixin and "BackdropTemplate")
		mainFrame.model:SetSize(_G[addon.."_CharactersInfoAdventure"]:GetWidth(), _G[addon.."_CharactersInfoAdventure"]:GetHeight());
		mainFrame.model:SetPoint("TOPLEFT");
		mainFrame.model:SetKeepModelOnHide(true);
		mainFrame.model:SetCamDistanceScale(1);
		mainFrame.model:SetFrameLevel(_G[addon.."_CharactersInfoAdventure"]:GetFrameLevel());
		mainFrame.model.cursorX = 0;
		mainFrame.model:SetScript("OnMouseDown", WC_Lib.ModelOnMouseDown);
		mainFrame.model:SetScript("OnMouseUp", WC_Lib.ModelOnMouseUp);
	
		mainFrame.spellsIcon = CreateFrame("Button", nil, mainFrame, BackdropTemplateMixin and "BackdropTemplate");		
		mainFrame.spellsIcon:SetSize(32, 32);
		mainFrame.spellsIcon:SetPoint("TOPRIGHT", mainFrame, "TOPRIGHT", -20, -20);
		mainFrame.spellsIcon.Anchor = {"ANCHOR_BOTTOMRIGHT", 19, 52};
		mainFrame.spellsIcon.addon  = addon;
		mainFrame.spellsIcon:SetScript("OnLeave", WC_Lib.OnLeave);
		mainFrame.spellsIcon:SetScript("OnEnter", OnEnterTooltipEnnemyAdventureNotFight);
		mainFrame.spellsIcon:SetNormalTexture("Interface/AddOns/WoWChallengerCreator/images/adventures/spellIconUnit");
		mainFrame.spellsIcon:SetHighlightTexture("Interface/AddOns/WoWChallengerCreator/images/adventures/spellIconUnit");
	
		mainFrame.bgtitle = CreateFrame("Frame", nil, mainFrame, BackdropTemplateMixin and "BackdropTemplate");		
		mainFrame.bgtitle:SetSize(mainFrame:GetWidth(), 75);
		mainFrame.bgtitle:SetPoint("BOTTOM");
		mainFrame.bgtitle:SetBackdrop({bgFile = "Interface/AddOns/WoWChallengerCreator/images/adventures/shadow_nameUnit",
						edgeFile = "", 
						tile = false, edgeSize = 0,
						insets = { left = -50, right = -50, top = 0, bottom = 0 }});
		mainFrame.bgtitle.title = mainFrame.bgtitle:CreateFontString(nil, "OVERLAY", nil, mainFrame.bgtitle);
		if GetLocale() == "ruRU" then
			mainFrame.bgtitle.title:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\MORPHEUS__CYR.TTF", 18);
		else
			mainFrame.bgtitle.title:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\MORPHEUS.TTF", 18);
		end
		mainFrame.bgtitle.title:SetPoint("BOTTOM", mainFrame.bgtitle, "BOTTOM", 0, 10);
		mainFrame.bgtitle.title:SetTextColor(1, 0.82, 0);
		mainFrame.bgtitle.title:SetSize(mainFrame.bgtitle:GetWidth(), 0);
	end
	
	_G[addon.."_InfoModelUnitAdventure"].spellsIcon.idUnit = infoUnit.id;
	_G[addon.."_InfoModelUnitAdventure"].bgtitle.title:SetText(infoUnit.name);
	
	_G[addon.."_InfoModelUnitAdventure"]:Show();
	
	_G[addon.."_InfoModelUnitAdventure"].model:SetDisplayInfo(infoUnit.displayId);
end

function CreateBtnListUnitEncounter(infoUnit, parent, i, addon)
	local mainFrame = CreateFrame("Button", addon.."_BtnListUnitEncounter"..i, parent);
	mainFrame:SetSize(64, 64);
	mainFrame:SetNormalTexture("Interface/AddOns/WoWChallenger/images/adventures/PortraitUnitEncounter");
	mainFrame:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/adventures/PortraitUnitEncounter");
	mainFrame:SetFrameLevel(parent:GetFrameLevel() + 2);
	mainFrame:RegisterForClicks("LeftButtonUp", "RightButtonDown");
	mainFrame:SetScript("OnClick", function (self, button)
		if (IsShiftKeyDown() and button == "LeftButton" and addon == "WCC") then
			if self.i ~= 1 then
				local tmp  = self.listUnits[self.i];
				self.listUnits[self.i] = self.listUnits[self.i - 1];
				self.listUnits[self.i - 1] = tmp;
				if btnListUnitEncouterSelected[addon] and btnListUnitEncouterSelected[addon] == self then
					btnListUnitEncouterSelected[addon] = _G[addon.."_BtnListUnitEncounter"..i - 1];
				elseif btnListUnitEncouterSelected[addon] and btnListUnitEncouterSelected[addon] == _G[addon.."_BtnListUnitEncounter"..i - 1] then
					btnListUnitEncouterSelected[addon] = self;
				end
				RefreshListUnitsEncounter(self.listUnits, addon);
				WC_Lib.OnLeave(self);
				UpdateVersionAdventure(_G[addon.."_MainInfoAdventure"].infoBtn.infoAdventure, 1);
				PlaySound("62540", "sfx");
			end
		elseif (IsShiftKeyDown() and button == "RightButton" and addon == "WCC") then
			if self.i ~= #self.listUnits then
				local tmp  = self.listUnits[self.i];
				self.listUnits[self.i] = self.listUnits[self.i + 1];
				self.listUnits[self.i + 1] = tmp;
				if btnListUnitEncouterSelected[addon] and btnListUnitEncouterSelected[addon] == self then
					btnListUnitEncouterSelected[addon] = _G[addon.."_BtnListUnitEncounter"..i + 1];
				elseif btnListUnitEncouterSelected[addon] and btnListUnitEncouterSelected[addon] == _G[addon.."_BtnListUnitEncounter"..i + 1] then
					btnListUnitEncouterSelected[addon] = self;
				end
				RefreshListUnitsEncounter(self.listUnits, addon);
				WC_Lib.OnLeave(self);
				UpdateVersionAdventure(_G[addon.."_MainInfoAdventure"].infoBtn.infoAdventure, 1);
				PlaySound("62540", "sfx");
			end
		elseif (button == "LeftButton") then
			if btnListUnitEncouterSelected[addon] then
				btnListUnitEncouterSelected[addon]:UnlockHighlight();
			end
			self:LockHighlight();
			btnListUnitEncouterSelected[addon] = self;
			ShowInfoModelUnitAdventure(self.infoUnit, addon);			
			PlaySound("624", "sfx");
		end
	end);
	
	mainFrame.frame = CreateFrame("Frame", nil, mainFrame);
	mainFrame.frame:SetSize(64, 64);
	mainFrame.frame:SetPoint("CENTER", mainFrame, "CENTER", 3, 0);
	mainFrame.frame:SetFrameLevel(mainFrame:GetFrameLevel() - 1);
	
	mainFrame.creature = mainFrame.frame:CreateTexture(nil, "BACKGROUND")
	mainFrame.creature:SetSize(40, 40);
	mainFrame.creature:SetPoint("CENTER", mainFrame.frame, "CENTER");
	SetPortraitTextureFromCreatureDisplayID(mainFrame.creature, infoUnit.displayId);
	
	if addon == "WCC" then
		mainFrame:SetScript("OnLeave", function (self)
			self.delBtn:Hide();
			WC_Lib.OnLeave(self)
		end);
		mainFrame:SetScript("OnEnter", function (self)
			self.delBtn:Show();
			GameTooltip:SetOwner(self, "ANCHOR_RIGHT", -5, -5);
			GameTooltip:AddLine(self.infoUnit.name, 1, 1, 1);
			GameTooltip:AddLine(" ");
			GameTooltip:AddLine(core.L["MAJ + Clique-gauche pour déplacer vers le haut"], 0, 1, 0);
			GameTooltip:AddLine(core.L["MAJ + Clique-droit pour déplacer vers le bas"], 0, 1, 0);
			GameTooltip:Show();
		end);
	
		mainFrame.delBtn = CreateFrame("Button", nil, mainFrame);
		mainFrame.delBtn:SetSize(20, 20);
		mainFrame.delBtn:SetPushedTexture("Interface/Buttons/UI-GroupLoot-Pass-Up");
		mainFrame.delBtn:SetNormalTexture("Interface/Buttons/UI-GroupLoot-Pass-Down");
		mainFrame.delBtn:SetHighlightTexture("Interface/Buttons/UI-GROUPLOOT-PASS-HIGHLIGHT");
		mainFrame.delBtn:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 0, 0);
		mainFrame.delBtn:SetScript("OnClick", function (self)
			local confirmDialog = StaticPopup_Show("WC_ConfirmDialog", core.L["Voulez-vous supprimer"].." |cFFF5CC00"..self:GetParent().infoUnit.name.."|r "..core.L["du combat ?"]);
			confirmDialog.func  = function()
				if btnListUnitEncouterSelected[addon] == self:GetParent() then
					btnListUnitEncouterSelected[addon]:UnlockHighlight();
					btnListUnitEncouterSelected[addon] = nil;
					if _G[addon.."_InfoModelUnitAdventure"] and _G[addon.."_InfoModelUnitAdventure"]:IsShown() then
						_G[addon.."_InfoModelUnitAdventure"]:Hide()
					end
				end
				table.remove(self:GetParent().listUnits, self:GetParent().i);
				RefreshListUnitsEncounter(self:GetParent().listUnits, addon);
				UpdateVersionAdventure(_G[addon.."_MainInfoAdventure"].infoBtn.infoAdventure, 1);
			end
		end);
		mainFrame.delBtn:SetScript("OnLeave", function (self)
			WC_Lib.OnLeave(self);
			self:Hide();
		end);
		mainFrame.delBtn:SetScript("OnEnter", function (self)
			GameTooltip:SetOwner(self, "ANCHOR_RIGHT", 0, 0);
			GameTooltip:AddLine(core.L["Supprimer"]);
			GameTooltip:Show();
			self:Show();
		end);
		mainFrame.delBtn:Hide();
	else
		mainFrame:SetScript("OnLeave", function (self)
			WC_Lib.OnLeave(self)
		end);
		mainFrame:SetScript("OnEnter", function (self)
			GameTooltip:SetOwner(self, "ANCHOR_RIGHT", -5, -5);
			GameTooltip:AddLine(self.infoUnit.name, 1, 1, 1);
			GameTooltip:Show();
		end);
	end
	
	return mainFrame;
end

function RefreshListUnitsEncounter(listUnits, addon)
	local k = 0;
	if listUnits ~= nil then
		for i = 1, #listUnits do
			local infoUnit = WC_Lib.SearchThingInAdventureByID(listUnits[i], 2, addon);
			local portraitUnit = _G[addon.."_BtnListUnitEncounter"..i];
			if portraitUnit == nil then
				portraitUnit = CreateBtnListUnitEncounter(infoUnit, _G[addon.."_ListUnitEncounter"], i, addon);
			end
			
			portraitUnit.infoUnit  = infoUnit;
			portraitUnit.listUnits = listUnits;
			portraitUnit.i = i;

			portraitUnit.creature:SetColorTexture(1, 1, 1, 0);
			SetPortraitTextureFromCreatureDisplayID(portraitUnit.creature, infoUnit.displayId);			
			portraitUnit:UnlockHighlight();
			if btnListUnitEncouterSelected[addon] and btnListUnitEncouterSelected[addon] == portraitUnit then
				btnListUnitEncouterSelected[addon]:LockHighlight();
			end
			
			if (i == 1) then
				portraitUnit:SetPoint("TOPLEFT", _G[addon.."_ListUnitEncounter"], "TOPLEFT", 0, 0);
			else
				portraitUnit:SetPoint("TOP", _G[addon.."_BtnListUnitEncounter"..i - 1], "BOTTOM", 0, 5);
			end	
			portraitUnit:Show();
			k = i;
		end
	end
	
	if listUnits[1] then
		RefreshPortraitFight(nil, listUnits[1], addon)
	end
	
	-- HIDE LES FRAMES EN TROP
	for k = k + 1, 999 do
		if not _G[addon.."_BtnListUnitEncounter"..k] then
			break;
		else
			if btnListUnitEncouterSelected[addon] == _G[addon.."_BtnListUnitEncounter"..k] or btnListUnitEncouterSelected[addon] == _G[addon.."_BtnListUnitEncounter"..k] then
				btnListUnitEncouterSelected[addon]:UnlockHighlight();
				btnListUnitEncouterSelected[addon] = nil;
				_G[addon.."_AdventurePanel"].titleFrame:Click();
			end
			_G[addon.."_BtnListUnitEncounter"..k]:Hide();
		end
	end
end

function ShowCharactersInfoAdventure(infoFight, isPlayer, addon)
	local adventure = WC_Lib.SearchThingInAdventureByID(infoFight.idAdventure, 1, addon);
	
	-- CREATION DES FRAMES
	if not _G[addon.."_CharactersInfoAdventure"] then
		local mainFrame = CreateFrame("Frame", addon.."_CharactersInfoAdventure", _G[addon.."_AdventurePanel"], BackdropTemplateMixin and "BackdropTemplate");		
		mainFrame:SetSize(400, _G[addon.."_AdventurePanel"]:GetHeight());
		mainFrame:SetPoint("TOPRIGHT", _G[addon.."_AdventurePanel"], "TOPRIGHT");
		
		if addon == "WCC" then
			mainFrame.tb_AddUnit = WCCreator_Lib.CreateCustomFrame(WCCreator_Lib.structures.adventures.SE_AddUnit, mainFrame);
			mainFrame.tb_AddUnit:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 15, -20);
			mainFrame.addBtn = CreateFrame("Button", nil, mainFrame, "UIPanelButtonTemplate");
			mainFrame.addBtn:SetSize(20, 20);
			mainFrame.addBtn:SetText("+");
			mainFrame.addBtn:SetPoint("LEFT", mainFrame.tb_AddUnit, "RIGHT", 5, 0);
			mainFrame.addBtn:SetScript("OnClick", function (self)
				local idText = _G[addon.."_CharactersInfoAdventure"].tb_AddUnit:GetText();
				if idText == "" then return end
				if not WC_Lib.SearchThingInAdventureByID(idText, 2, addon) then
					UIErrorsFrame:AddMessage(core.L["Personnage introuvable"], 1, 0, 0);
					PlaySound("47355", "sfx", true);
					return;
				end
				if self.isPlayer then
					if WC_Lib.findInArray(self.infoFight.idPlayer, idText) ~= -1 then
						UIErrorsFrame:AddMessage(core.L["Ce personnage est déjà présent."], 1, 0, 0);
						PlaySound("47355", "sfx", true);
					else
						table.insert(self.infoFight.idPlayer, idText);
						RefreshListUnitsEncounter(self.infoFight.idPlayer, addon);
						_G[addon.."_CharactersInfoAdventure"].tb_AddUnit:SetText("");
						UpdateVersionAdventure(self.infoAdventure, 1);
						PlaySound("39673", "sfx");
					end
				else
					if WC_Lib.findInArray(self.infoFight.idEnnemies, idText) ~= -1 then
						UIErrorsFrame:AddMessage(core.L["Ce personnage est déjà présent."], 1, 0, 0);
						PlaySound("47355", "sfx", true);
					else
						table.insert(self.infoFight.idEnnemies, idText);
						RefreshListUnitsEncounter(self.infoFight.idEnnemies, addon);
						_G[addon.."_CharactersInfoAdventure"].tb_AddUnit:SetText("");
						UpdateVersionAdventure(self.infoAdventure, 1);
						PlaySound("39673", "sfx");
					end				
				end
			end);
		end
		
		mainFrame.listUnit = CreateFrame("ScrollFrame", nil, mainFrame, "UIPanelScrollFrameTemplate");
		if addon == "WCC" then
			mainFrame.listUnit:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 5, -50);
		else
			mainFrame.listUnit:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 5, -25);
		end
		mainFrame.listUnit:SetPoint("BOTTOMLEFT", mainFrame, "BOTTOMLEFT", 5, 5);
		mainFrame.listUnit:SetWidth(80);
		mainFrame.listUnit:SetClipsChildren(true);
		mainFrame.listUnit.ScrollBar:ClearAllPoints();
		local child = CreateFrame("Frame", addon.."_ListUnitEncounter", mainFrame.listUnit);
		child:SetSize(mainFrame.listUnit:GetWidth(), mainFrame.listUnit:GetHeight());
		mainFrame.listUnit:SetScrollChild(child);
		mainFrame.listUnit:SetScript("OnMouseWheel", WC_Lib.ScrollFrame_OnMouseWheel);
	end
	
	local list = infoFight.idEnnemies;
	if isPlayer then list = infoFight.idPlayer end
	RefreshListUnitsEncounter(list, addon);
	
	if addon == "WCC" then
		_G[addon.."_CharactersInfoAdventure"].addBtn.infoAdventure = adventure;
		_G[addon.."_CharactersInfoAdventure"].addBtn.infoFight     = infoFight;
		_G[addon.."_CharactersInfoAdventure"].addBtn.isPlayer      = isPlayer;
	end
	_G[addon.."_CharactersInfoAdventure"]:SetBackdrop({bgFile = "Interface/ENCOUNTERJOURNAL/UI-EJ-BACKGROUND-"..adventure.picture,
							tile = false, edgeFile = "",
							insets = { left = 0, right = -115, top = 0, bottom = -78 }});
	_G[addon.."_CharactersInfoAdventure"]:Show();
	
	-- HIDE MAIN INFO
	if _G[addon.."_MainInfoAdventure"] and _G[addon.."_MainInfoAdventure"]:IsShown() then
		_G[addon.."_MainInfoAdventure"]:Hide();
	end
end

function CreateBtnFight(infoFight, parent, i, isPlayer, addon)
	local frameName = "";
	if isPlayer then
		frameName = addon.."_BtnFightPlayer"..i;
	else
		frameName = addon.."_BtnFightEnnemies"..i;
	end
	
	local mainFrame = CreateFrame("Button", frameName, parent);
	mainFrame:SetSize(300, 58);
	
	local title = mainFrame:CreateFontString(nil, "OVERLAY", nil, mainFrame);
	title:SetFontObject("GameFontHighlightMed2");
	title:SetTextColor(0.87, 0.69, 0.49);
	title:SetPoint("LEFT", mainFrame, "LEFT", 75, 0);
	title:SetJustifyH("LEFT");
	title:SetSize(mainFrame:GetWidth() - 85, 0);	
	mainFrame:SetFontString(title);
	
	-- PORTRAIT --
	mainFrame.portrait = CreateFrame("Frame", nil, mainFrame);
	mainFrame.portrait:SetSize(53, mainFrame:GetHeight() - 17);
	mainFrame.portrait:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 7, -10);
	mainFrame.portrait:SetFrameLevel(mainFrame:GetFrameLevel() + 1);
	mainFrame.portrait:SetClipsChildren(true);
	
	mainFrame.portrait.creature = mainFrame.portrait:CreateTexture(nil, "BACKGROUND")
	mainFrame.portrait.creature:SetSize(53, 56);
	mainFrame.portrait.creature:SetPoint("TOP");	
	
	if isPlayer then
		mainFrame:SetPoint("TOP", parent, "TOP");
		mainFrame:SetText((infoFight.namePlayer or "").." ("..core.L["Joueur"]..")");
	else
		mainFrame:SetPoint("BOTTOM", parent, "BOTTOM");
		mainFrame:SetText(infoFight.nameEnnemies);
	end
	
	local ntex = mainFrame:CreateTexture()
	ntex:SetTexture("Interface/ENCOUNTERJOURNAL/UI-ENCOUNTERJOURNALTEXTURES")
	ntex:SetTexCoord(0, 0.64, 0.212, 0.269)
	ntex:SetAllPoints()	
	mainFrame:SetNormalTexture(ntex)
	
	local htex = mainFrame:CreateTexture()
	htex:SetTexture("Interface/ENCOUNTERJOURNAL/UI-ENCOUNTERJOURNALTEXTURES")
	htex:SetTexCoord(0, 0.64, 0.1555, 0.214)
	htex:SetAllPoints()
	mainFrame:SetHighlightTexture(htex)
	
	local ptex = mainFrame:CreateTexture()
	ptex:SetTexture("Interface/ENCOUNTERJOURNAL/UI-ENCOUNTERJOURNALTEXTURES")
	ptex:SetTexCoord(0, 0.64, 0.1016, 0.157)
	ptex:SetAllPoints()
	mainFrame:SetPushedTexture(ptex)
	
	return mainFrame;
end

function CreateFightFrames(infoFights, parent, i, addon)
	if not _G[addon.."_FightFrames"..i] then
		local mainFrame = CreateFrame("Frame", addon.."_FightFrames"..i, parent);
		mainFrame:SetSize(300, 125);
		
		local btnPlayer = CreateBtnFight(infoFights[i], mainFrame, i, true, addon);
		
		-- MIDDLE CONTENT --
		mainFrame.decoLeft = CreateFrame("Frame", nil, mainFrame, BackdropTemplateMixin and "BackdropTemplate");
		mainFrame.decoLeft:SetSize(50, 20);
		mainFrame.decoLeft:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/adventures/decoration_left",
								tile = false, edgeFile = "",
								insets = { left = 0, right = 0, top = 0, bottom = 0 }});
		mainFrame.decoRight = CreateFrame("Frame", nil, mainFrame, BackdropTemplateMixin and "BackdropTemplate");
		mainFrame.decoRight:SetSize(50, 20);
		mainFrame.decoRight:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/adventures/decoration_right",
								tile = false, edgeFile = "",
								insets = { left = 0, right = 0, top = 0, bottom = 0 }});
		
		mainFrame.VStext = mainFrame:CreateFontString(nil, "OVERLAY", nil, mainFrame);
		mainFrame.VStext:SetFontObject("GameFontBlack");
		mainFrame.VStext:SetPoint("CENTER", mainFrame, "CENTER", 0, -3);
		mainFrame.VStext:SetSize(mainFrame:GetWidth(), 0);
		mainFrame.VStext:SetText("VS");
		
		mainFrame.lockFrame = CreateFrame("Frame", nil, mainFrame, BackdropTemplateMixin and "BackdropTemplate");
		mainFrame.lockFrame:SetSize(16, 16);
		mainFrame.lockFrame:SetPoint("CENTER", mainFrame, "CENTER", 0, -2);
		mainFrame.lockFrame:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/lockIconStep",
								tile = false, edgeFile = "",
								insets = { left = 0, right = 0, top = 0, bottom = 0 }});
		mainFrame.lockFrame:Hide();
		mainFrame.validFrame = CreateFrame("Frame", nil, mainFrame, BackdropTemplateMixin and "BackdropTemplate");
		mainFrame.validFrame:SetSize(32, 32);
		mainFrame.validFrame:SetPoint("CENTER", mainFrame, "CENTER", 0, -2);
		mainFrame.validFrame:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/greenCheckButton",
								tile = false, edgeFile = "",
								insets = { left = 0, right = 0, top = 0, bottom = 0 }});
		mainFrame.validFrame:Hide();
		
		-- EDIT PART
		if addon == "WCC" then
			mainFrame.decoLeft:SetPoint("CENTER", mainFrame, "CENTER", -60, -2);
			mainFrame.decoRight:SetPoint("CENTER", mainFrame, "CENTER", 60, -2);
			
			mainFrame.editNameBtn = CreateFrame("Button", nil, mainFrame);
			mainFrame.editNameBtn:SetSize(20, 20);
			mainFrame.editNameBtn:SetPoint("CENTER", mainFrame, "CENTER", -25, -2);
			mainFrame.editNameBtn:SetNormalTexture("Interface/WorldMap/Gear_64Grey");
			mainFrame.editNameBtn:SetHighlightTexture("Interface/Buttons/UI-Common-MouseHilight");
			mainFrame.editNameBtn:SetScript("OnLeave", WC_Lib.OnLeave);
			mainFrame.editNameBtn:SetScript("OnEnter", function(self) GameTooltip:SetOwner(self, "ANCHOR_LEFT", 0, 0);
														GameTooltip:AddLine(core.L["Paramètres du combat"]);
														GameTooltip:Show(); end);
			
			mainFrame.delBtn = CreateFrame("Button", nil, mainFrame);
			mainFrame.delBtn:SetSize(20, 20);
			mainFrame.delBtn:SetPushedTexture("Interface/Buttons/UI-GroupLoot-Pass-Up");
			mainFrame.delBtn:SetNormalTexture("Interface/Buttons/UI-GroupLoot-Pass-Down");
			mainFrame.delBtn:SetHighlightTexture("Interface/Buttons/UI-GROUPLOOT-PASS-HIGHLIGHT");
			mainFrame.delBtn:SetPoint("CENTER", mainFrame, "CENTER", 25, -2);
			mainFrame.delBtn:SetScript("OnLeave", WC_Lib.OnLeave);
			mainFrame.delBtn:SetScript("OnEnter", function (self)
				GameTooltip:SetOwner(self, "ANCHOR_RIGHT", 0, 0);
				GameTooltip:AddLine(core.L["Supprimer"]);
				GameTooltip:Show();
				self:Show();
			end);
			
			mainFrame.btn_arrowUp = CreateFrame("Button", nil, mainFrame);
			mainFrame.btn_arrowUp:SetSize(26, 26);
			mainFrame.btn_arrowUp:SetPoint("RIGHT", mainFrame.decoLeft, "LEFT", 0, 0);
			mainFrame.btn_arrowUp:SetNormalTexture("Interface/Buttons/UI-Panel-CollapseButton-Up");
			mainFrame.btn_arrowUp:SetHighlightTexture("Interface/Buttons/UI-Panel-MinimizeButton-Highlight");
			mainFrame.btn_arrowUp:SetPushedTexture("Interface/Buttons/UI-Panel-CollapseButton-Down");
			
			mainFrame.btn_arrowDown = CreateFrame("Button", nil, mainFrame);
			mainFrame.btn_arrowDown:SetSize(26, 26);
			mainFrame.btn_arrowDown:SetPoint("LEFT", mainFrame.decoRight, "RIGHT", 0, 0);
			mainFrame.btn_arrowDown:SetNormalTexture("Interface/Buttons/UI-Panel-ExpandButton-Up");
			mainFrame.btn_arrowDown:SetHighlightTexture("Interface/Buttons/UI-Panel-MinimizeButton-Highlight");
			mainFrame.btn_arrowDown:SetPushedTexture("Interface/Buttons/UI-Panel-ExpandButton-Down");
		else
			mainFrame.decoLeft:SetPoint("CENTER", mainFrame, "CENTER", -30, -2);
			mainFrame.decoRight:SetPoint("CENTER", mainFrame, "CENTER", 30, -2);
		end
		-- END MIDDLE CONTENT --
		
		local btnEnnemies = CreateBtnFight(infoFights[i], mainFrame, i, false, addon);

		if (i == 1) then		
			mainFrame:SetPoint("TOP", parent, "TOP");
		else		
			mainFrame:SetPoint("TOP", _G[addon.."_FightFrames"..i - 1], "BOTTOM", 0, -35);
		end
		
		mainFrame.uselessFrame = CreateFrame("Frame", nil, mainFrame);
		mainFrame.uselessFrame:SetSize(64, 64);
		mainFrame.uselessFrame:SetPoint("TOP", mainFrame, "BOTTOM", 0, 15);
	end

	_G[addon.."_FightFrames"..i].VStext:Show();
	_G[addon.."_FightFrames"..i].lockFrame:Hide();
	_G[addon.."_FightFrames"..i].validFrame:Hide();
	_G[addon.."_BtnFightPlayer"..i].index   = i;
	_G[addon.."_BtnFightEnnemies"..i].index = i;
	_G[addon.."_BtnFightPlayer"..i]:SetText((infoFights[i].namePlayer or "").." ("..core.L["Joueur"]..")");
	_G[addon.."_BtnFightEnnemies"..i]:SetText(infoFights[i].nameEnnemies);
	
		
	if addon == "WC" then
		if WC_AdventuresData.victories[infoFights[i].idAdventure] and WC_AdventuresData.victories[infoFights[i].idAdventure] >= i then
			_G[addon.."_FightFrames"..i].VStext:Hide();
			_G[addon.."_FightFrames"..i].validFrame:Show();
			_G[addon.."_BtnFightPlayer"..i].isLock = false;
			_G[addon.."_BtnFightEnnemies"..i].isLock = false;
		elseif i > 1 then
			if not WC_AdventuresData.victories[infoFights[i].idAdventure] or WC_AdventuresData.victories[infoFights[i].idAdventure] + 1 < i then
				_G[addon.."_BtnFightPlayer"..i]:SetText(core.L["Lock"].." ("..core.L["Joueur"]..")");
				_G[addon.."_BtnFightEnnemies"..i]:SetText(core.L["Lock"]);
				_G[addon.."_FightFrames"..i].VStext:Hide();
				_G[addon.."_FightFrames"..i].lockFrame:Show();
				_G[addon.."_BtnFightPlayer"..i].isLock = true;
				_G[addon.."_BtnFightEnnemies"..i].isLock = true;
			else
				_G[addon.."_BtnFightPlayer"..i].isLock = false;
				_G[addon.."_BtnFightEnnemies"..i].isLock = false;
			end
		else
			_G[addon.."_BtnFightPlayer"..i].isLock = false;
			_G[addon.."_BtnFightEnnemies"..i].isLock = false;
		end
	elseif addon == "WCC" then
		_G[addon.."_FightFrames"..i].editNameBtn:SetScript("OnClick", function(self)
			if not _G[addon.."_SettingsFight"] or not _G[addon.."_SettingsFight"]:IsShown() then
				ShowSettingsFight(infoFights[i]);
			end
		end);
		_G[addon.."_FightFrames"..i].delBtn:SetScript("OnClick", function (self)
			local confirmDialog = StaticPopup_Show("WC_ConfirmDialog", core.L["Voulez-vous supprimer ce combat ?"]);
			confirmDialog.func  = function()
				local aventure = WC_Lib.SearchThingInAdventureByID(infoFights[i].idAdventure, 1, "WCC");
				if aventure then
					UpdateVersionAdventure(aventure, 1);
					table.remove(infoFights, i);
					RefreshListFights(infoFights, "WCC");
				end
			end;
		end);
		_G[addon.."_FightFrames"..i].btn_arrowUp:SetScript("OnClick", function (self)
			if i > 1 then
				local tmp         = infoFights[i];
				infoFights[i]     = infoFights[i - 1];
				infoFights[i - 1] = tmp;
				RefreshListFights(infoFights, "WCC");
				PlaySound("62540", "sfx");
			end
		end);
		_G[addon.."_FightFrames"..i].btn_arrowDown:SetScript("OnClick", function (self)
			if i < #infoFights then
				local tmp         = infoFights[i];
				infoFights[i]     = infoFights[i + 1];
				infoFights[i + 1] = tmp;
				RefreshListFights(infoFights, "WCC");
				PlaySound("62540", "sfx");
			end
		end);
	end
	
	
	
	
	_G[addon.."_BtnFightPlayer"..i]:SetScript("OnClick", function(self)
		if btnFightSelected[addon] then
			btnFightSelected[addon]:UnlockHighlight();
		end
		self:LockHighlight();
		btnFightSelected[addon] = self;
		ShowCharactersInfoAdventure(infoFights[i], true, addon);
		if _G[addon.."_BtnListUnitEncounter1"] then
			if _G[addon.."_BtnListUnitEncounter1"]:IsShown() then		
				_G[addon.."_BtnListUnitEncounter1"]:Click();
			else
				if _G[addon.."_InfoModelUnitAdventure"] and _G[addon.."_InfoModelUnitAdventure"]:IsShown() then
					_G[addon.."_InfoModelUnitAdventure"]:Hide()
					btnListUnitEncouterSelected[addon] = nil;
				end
			end
		end
		
		if self.isLock then
			_G[addon.."_AdventurePanel"].btnFight:SetText(core.L["Lock"]);
			_G[addon.."_AdventurePanel"].btnFight:Disable();
		else
			_G[addon.."_AdventurePanel"].btnFight:SetText(core.L["Combattre"]);
			_G[addon.."_AdventurePanel"].btnFight.infoFight = infoFights[i];
			_G[addon.."_AdventurePanel"].btnFight:Enable();
			_G[addon.."_AdventurePanel"].btnFight:SetScript("OnClick", function(selfBtn)
				if addon == "WC" then
					if DBProfile.skillPoints > 0 or (_G[addon.."_FightFrames"..self.index].validFrame and _G[addon.."_FightFrames"..self.index].validFrame:IsShown()) then
						if not _G[addon.."_FightFrames"..self.index].validFrame or not _G[addon.."_FightFrames"..self.index].validFrame:IsShown() then
							DBProfile.skillPoints = DBProfile.skillPoints - 1;
							_G["SkillPointsCampaignsAdventure"]:SetText(DBProfile.skillPoints);
						end
						ShowFightSceneAdventure(selfBtn.infoFight, nil, addon);
					else
						StaticPopup_Show("NotEnoughSkillPoints");
					end
				elseif addon == "WCC" then
					ShowFightSceneAdventure(selfBtn.infoFight, nil, addon);
				end
			end);
		end
	end);
	_G[addon.."_BtnFightEnnemies"..i]:SetScript("OnClick", function(self)
		if btnFightSelected[addon] then
			btnFightSelected[addon]:UnlockHighlight();
		end
		self:LockHighlight();
		btnFightSelected[addon] = self;
		ShowCharactersInfoAdventure(infoFights[i], false, addon)
		if _G[addon.."_BtnListUnitEncounter1"] then
			if _G[addon.."_BtnListUnitEncounter1"]:IsShown() then		
				_G[addon.."_BtnListUnitEncounter1"]:Click();
			else
				if _G[addon.."_InfoModelUnitAdventure"] and _G[addon.."_InfoModelUnitAdventure"]:IsShown() then
					_G[addon.."_InfoModelUnitAdventure"]:Hide()
					btnListUnitEncouterSelected[addon] = nil;
				end
			end
		end
		if self.isLock then
			_G[addon.."_AdventurePanel"].btnFight:SetText(core.L["Lock"]);
			_G[addon.."_AdventurePanel"].btnFight:Disable();
		else
			_G[addon.."_AdventurePanel"].btnFight:SetText(core.L["Combattre"]);
			_G[addon.."_AdventurePanel"].btnFight.infoFight = infoFights[i];
			_G[addon.."_AdventurePanel"].btnFight:Enable();
			_G[addon.."_AdventurePanel"].btnFight:SetScript("OnClick", function(selfBtn)
				if addon == "WC" then
					if DBProfile.skillPoints > 0 or (_G[addon.."_FightFrames"..self.index].validFrame and _G[addon.."_FightFrames"..self.index].validFrame:IsShown()) then
						if not _G[addon.."_FightFrames"..self.index].validFrame or not _G[addon.."_FightFrames"..self.index].validFrame:IsShown() then
							DBProfile.skillPoints = DBProfile.skillPoints - 1;
							_G["SkillPointsCampaignsAdventure"]:SetText(DBProfile.skillPoints);
						end
						ShowFightSceneAdventure(selfBtn.infoFight, nil, addon);
					else
						StaticPopup_Show("NotEnoughSkillPoints");
					end
				elseif addon == "WCC" then
					ShowFightSceneAdventure(selfBtn.infoFight, nil, addon);
				end
			end);
		end
	end);
	
	RefreshPortraitFight(_G[addon.."_BtnFightPlayer"..i].portrait.creature, infoFights[i].idPlayer[1], addon);
	RefreshPortraitFight(_G[addon.."_BtnFightEnnemies"..i].portrait.creature, infoFights[i].idEnnemies[1], addon);
	
	_G[addon.."_BtnFightEnnemies"..i]:UnlockHighlight();
	_G[addon.."_BtnFightPlayer"..i]:UnlockHighlight();
	if btnFightSelected[addon] and btnFightSelected[addon] == _G[addon.."_BtnFightEnnemies"..i] or btnFightSelected[addon] == _G[addon.."_BtnFightPlayer"..i] then
		btnFightSelected[addon]:LockHighlight();
	end
	
	_G[addon.."_FightFrames"..i]:Show();
end

function RefreshListFights(infoFights, addon)
	local k = 0;
	for i = 1, #infoFights do
		CreateFightFrames(infoFights, _G[addon.."_ListFigths"], i, addon);
		k = i;
	end
	if addon == "WCC" then
		if k == 0 then
			_G[addon.."_AdventurePanel"].listFight.btnNewFight:SetPoint("TOP", _G[addon.."_ListFigths"], "TOP", 0, -10);
		else
			_G[addon.."_AdventurePanel"].listFight.btnNewFight:SetPoint("TOP", _G[addon.."_FightFrames"..k], "BOTTOM", 0, -10);
		end
	end
	
	-- HIDE LES FRAMES EN TROP
	for k = k + 1, 999 do
		if not _G[addon.."_FightFrames"..k] then
			break;
		else
			if btnFightSelected[addon] == _G[addon.."_BtnFightEnnemies"..k] or btnFightSelected[addon] == _G[addon.."_BtnFightPlayer"..k] then
				btnFightSelected[addon]:UnlockHighlight();
				btnFightSelected[addon] = nil;
				_G[addon.."_AdventurePanel"].titleFrame:Click();
			end
			_G[addon.."_FightFrames"..k]:Hide();
		end
	end
end

function ShowMainInfoAdventure(infoAdventure, addon)
	if not _G[addon.."_AdventurePanel"] then return end;

	if _G[addon.."_CharactersInfoAdventure"] then _G[addon.."_CharactersInfoAdventure"]:Hide() end;

	-- CREATION DES FRAMES
	if not _G[addon.."_MainInfoAdventure"] then
		local mainFrame = CreateFrame("Frame", addon.."_MainInfoAdventure", _G[addon.."_AdventurePanel"]);		
		mainFrame:SetSize(400, _G[addon.."_AdventurePanel"]:GetHeight());
		mainFrame:SetPoint("TOPRIGHT", _G[addon.."_AdventurePanel"], "TOPRIGHT");
		
		-- IMAGE
		mainFrame.picture = CreateFrame("Frame", nil, _G[addon.."_MainInfoAdventure"], BackdropTemplateMixin and "BackdropTemplate");
		mainFrame.picture:SetSize(_G[addon.."_MainInfoAdventure"]:GetWidth(), 300);
		mainFrame.picture:SetPoint("TOP", _G[addon.."_MainInfoAdventure"], "TOP");
		mainFrame.picture:SetBackdrop({bgFile = "Interface/ENCOUNTERJOURNAL/UI-EJ-LOREBG-Default",
							tile = false, edgeFile = "",
							insets = { left = 0, right = -115, top = 0, bottom = -175 }});

		mainFrame.bgtitle = CreateFrame("Frame", nil, mainFrame, BackdropTemplateMixin and "BackdropTemplate");
		mainFrame.bgtitle:SetPoint("TOP", mainFrame, "TOP", 0, -20);
		mainFrame.bgtitle:SetFrameLevel(mainFrame.picture:GetFrameLevel() + 1);
		mainFrame.bgtitle:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/design_campaign/bg_title_campaign",
								edgeFile = "",
								tile = false, edgeSize = 16, 
								insets = { left = 0, right = 0, top = 0, bottom = 0 }});
		mainFrame.bgtitle:SetSize(mainFrame.picture:GetWidth(),75);
		
		-- TITRE
		mainFrame.bgtitle.title = mainFrame.bgtitle:CreateFontString(nil, "OVERLAY", nil, mainFrame.bgtitle);
		if GetLocale() == "ruRU" then
			mainFrame.bgtitle.title:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\MORPHEUS__CYR.TTF", 26);
		else
			mainFrame.bgtitle.title:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\MORPHEUS.TTF", 26);
		end
		mainFrame.bgtitle.title:SetPoint("CENTER", mainFrame.bgtitle, "CENTER");
		mainFrame.bgtitle.title:SetTextColor(1, 0.82, 0);
		mainFrame.bgtitle.title:SetSize(mainFrame.picture:GetWidth(), 0);
		
		-- DESCRIPTION
		mainFrame.description = CreateFrame("ScrollFrame", nil, mainFrame, "ScrollFrameTemplate");
		mainFrame.description:SetFrameLevel(mainFrame.picture:GetFrameLevel() + 1);
		mainFrame.description:SetPoint("TOPLEFT", mainFrame.picture, "BOTTOMLEFT", 20, 0);
		mainFrame.description:SetPoint("BOTTOMRIGHT", mainFrame, "BOTTOMRIGHT", -10, 10);
		mainFrame.description:SetClipsChildren(true);
		mainFrame.description.ScrollBar:ClearAllPoints();
		mainFrame.description.ScrollBar:SetPoint("TOPRIGHT", mainFrame.description, "TOPRIGHT", -8, 0);
		mainFrame.description.ScrollBar:SetPoint("BOTTOMRIGHT", mainFrame.description, "BOTTOMRIGHT", -8, 0);
		local child = CreateFrame("Frame", nil, mainFrame.description);
		child:SetSize(mainFrame.description:GetWidth(), mainFrame.description:GetHeight());
		mainFrame.description:SetScrollChild(child);
		mainFrame.description:SetScript("OnMouseWheel", WC_Lib.ScrollFrame_OnMouseWheel);
		mainFrame.description.text = child:CreateFontString(nil, "OVERLAY", nil, child);
		mainFrame.description.text:SetFontObject("GameFontBlack");
		mainFrame.description.text:SetPoint("TOPLEFT", child, "TOPLEFT");
		mainFrame.description.text:SetSize(child:GetWidth() - 20, 0);
		mainFrame.description.text:SetJustifyH("LEFT");
		mainFrame.description.text:SetText(core.L["Description de l'aventure"]);
		
		mainFrame.infoBtn = CreateFrame("Button", nil, mainFrame);
		mainFrame.infoBtn:SetSize(40, 40);
		mainFrame.infoBtn:SetPoint("TOPRIGHT", mainFrame, "TOPRIGHT", -10, -7);
		mainFrame.infoBtn:SetNormalTexture("Interface/COMMON/help-i");
		mainFrame.infoBtn:SetScript("OnLeave", WC_Lib.OnLeave);
		mainFrame.infoBtn:SetScript("OnEnter", function(self)
			GameTooltip:SetOwner(self, "ANCHOR_TOP", 0, 7);
			GameTooltip:AddLine(self.infoAdventure.title, 1, 1, 1);
			GameTooltip:AddLine("ID"..core.L["2points"].."|cff00c8ff"..self.infoAdventure.id.."|r");
			GameTooltip:AddLine(core.L["Version"]..core.L["2points"].."|cffffffff"..self.infoAdventure.av_settings.version.."|r");
			GameTooltip:AddLine(core.L["Créé par"]..core.L["2points"].."|cffffffff"..self.infoAdventure.av_settings.createBy.."|r");
			GameTooltip:AddLine(core.L["Dernière modif"]..core.L["2points"].."|cffffffff"..self.infoAdventure.av_settings.lastModified.."|r");
			GameTooltip:Show();
		end);
		
		if addon == "WCC" then
			mainFrame.description:SetSize(mainFrame:GetWidth() - 30, 129);
			
			mainFrame.btnEditAdventure = CreateFrame("Button", nil, mainFrame);
			mainFrame.btnEditAdventure:SetSize(24, 24);
			mainFrame.btnEditAdventure:SetPoint("TOPRIGHT", mainFrame, "TOPRIGHT", -45, -15);
			mainFrame.btnEditAdventure:SetNormalTexture("Interface/WorldMap/Gear_64Grey");
			mainFrame.btnEditAdventure:SetHighlightTexture("Interface/Buttons/UI-Common-MouseHilight");
			mainFrame.btnEditAdventure:SetScript("OnClick", function(self) ShowSettingsAdventure(infoAdventure) end);
			mainFrame.btnEditAdventure:SetScript("OnLeave", WC_Lib.OnLeave);
			mainFrame.btnEditAdventure:SetScript("OnEnter", function(self)
				GameTooltip:SetOwner(self, "ANCHOR_RIGHT", 0, 0);
				GameTooltip:AddLine(core.L["Éditer"]);
				GameTooltip:Show();
			end);
		end
	end
	
	-- REMPLISSAGE DES INFOS
	_G[addon.."_MainInfoAdventure"].infoBtn.infoAdventure = infoAdventure;
	_G[addon.."_MainInfoAdventure"].bgtitle.title:SetText(infoAdventure.title);
	_G[addon.."_MainInfoAdventure"].description.text:SetText(infoAdventure.info);
	--local pictureName = WCCreator_Lib.structures.adventures.exceptionInstanceName[infoAdventure.picture];
	--if not pictureName then
		--pictureName = infoAdventure.picture;
	--end
	_G[addon.."_MainInfoAdventure"].picture:SetBackdrop({bgFile = "Interface/ENCOUNTERJOURNAL/UI-EJ-LOREBG-"..infoAdventure.picture,--pictureName,
						tile = false, edgeFile = "",
						insets = { left = 0, right = -115, top = 0, bottom = -175 }});
	if addon == "WCC" then
		_G[addon.."_MainInfoAdventure"].btnEditAdventure:SetScript("OnClick", function(self) ShowSettingsAdventure(infoAdventure) end);
	end
	PlaySound("605", "sfx", true);
	_G[addon.."_MainInfoAdventure"]:Show();
end

function ShowAdventurePanel(infoAdventure, addon)
	local parent = nil;
	if addon == "WC" then
		parent = _G["AdventuresContentWC"];
	elseif addon == "WCC" then
		parent = _G["WCC_AdventuresTab1"].content;
	end
	-- CREATION DES FRAMES
	if not _G[addon.."_AdventurePanel"] then
		local mainFrame = CreateFrame("Frame", addon.."_AdventurePanel", parent, BackdropTemplateMixin and "BackdropTemplate");
		mainFrame:SetPoint("TOPLEFT", parent, "TOPLEFT");
		mainFrame:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", 0, -1);
		mainFrame:SetFrameLevel(parent:GetFrameLevel() + 5);
		mainFrame:EnableMouse(true);
		mainFrame:SetScript("OnHide", function(self)
			if _G[addon.."_SettingsAdventure"] then
				_G[addon.."_SettingsAdventure"]:Hide();
			end
		end)
		
		mainFrame.btnClosePanel = CreateFrame("Button", nil, mainFrame, "UIPanelCloseButton");
		mainFrame.btnClosePanel:SetSize(25, 25);
		mainFrame.btnClosePanel:SetFrameLevel(mainFrame:GetFrameLevel() + 1);
		mainFrame.btnClosePanel:SetScript("OnClick", function(self)
			btnFightSelected[addon] = nil;
			_G[addon.."_AdventurePanel"].btnFight.infoFight = nil;
			_G[addon.."_AdventurePanel"].btnFight:Disable();
			self:GetParent():Hide();
			PlaySound("830", "sfx");
		end);
		
		mainFrame.titleFrame = CreateFrame("Button", nil, mainFrame);
		mainFrame.titleFrame:SetSize(325, 25);
		mainFrame.titleFrame:SetPoint("LEFT", mainFrame.btnClosePanel, "RIGHT", 10, 0);
		mainFrame.titleFrame:SetScript("OnEnter", function(self)
			self.text:SetTextColor(1, 0.86, 0.83);
		end);
		mainFrame.titleFrame:SetScript("OnLeave", function(self)
			self.text:SetTextColor(0.89, 0.78, 0.65);
		end);
		
		mainFrame.titleFrame.text = mainFrame.titleFrame:CreateFontString(nil, "OVERLAY", nil, mainFrame.titleFrame);
		mainFrame.titleFrame.text:SetFontObject("GameFontHighlightLarge");
		mainFrame.titleFrame.text:SetTextColor(0.89, 0.78, 0.65);
		mainFrame.titleFrame.text:SetSize(325, 0);
		mainFrame.titleFrame.text:SetJustifyH("LEFT");
		mainFrame.titleFrame.text:SetPoint("LEFT", mainFrame.btnClosePanel, "RIGHT", 10, 0);
		
		mainFrame.listFight = CreateFrame("ScrollFrame", nil, mainFrame, "ScrollFrameTemplate");
		mainFrame.listFight:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 0, -50);
		mainFrame.listFight:SetPoint("BOTTOMLEFT", mainFrame, "BOTTOMLEFT", 0, 0);
		mainFrame.listFight:SetWidth(405);
		mainFrame.listFight:SetClipsChildren(true);
		mainFrame.listFight.ScrollBar:ClearAllPoints();
		mainFrame.listFight.ScrollBar:SetPoint("TOPRIGHT", mainFrame.listFight, "TOPRIGHT", -20, -20);
		mainFrame.listFight.ScrollBar:SetPoint("BOTTOMRIGHT", mainFrame.listFight, "BOTTOMRIGHT", -20, 20);
		local child = CreateFrame("Frame", addon.."_ListFigths", mainFrame.listFight, BackdropTemplateMixin and "BackdropTemplate");
		child:SetSize(mainFrame.listFight:GetWidth(), mainFrame.listFight:GetHeight() - 3);
		child:SetBackdrop({bgFile = "",
							edgeFile = "",
							tile = false, edgeSize = 16, 
							insets = { left = 0, right = 0, top = 0, bottom = 0 }});
		mainFrame.listFight:SetScrollChild(child);
		mainFrame.listFight:SetScript("OnMouseWheel", WC_Lib.ScrollFrame_OnMouseWheel);
		
		local bottomBG = -80;
		if addon == "WCC" then
			mainFrame:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", 0, -1);
		
			mainFrame.btnClosePanel:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 20, -20);
		
			mainFrame.listFight.btnNewFight = CreateFrame("Button", nil, child, "UIPanelButtonTemplate");
			mainFrame.listFight.btnNewFight:SetSize(256, 27);
			mainFrame.listFight.btnNewFight:SetPoint("TOP", child, "TOP", 0, 0);
			mainFrame.listFight.btnNewFight:SetText(core.L["Nouveau combat"]);
			
			mainFrame.listFight.uselessFrame = CreateFrame("Frame", nil, child);
			mainFrame.listFight.uselessFrame:SetSize(10, 40);
			mainFrame.listFight.uselessFrame:SetPoint("TOP", mainFrame.listFight.btnNewFight, "BOTTOM", 0, 0);
		else
			mainFrame:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", 0, 1);
			mainFrame.btnClosePanel:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 50, -20);
			bottomBG = -90;
		end
		
		mainFrame:SetBackdrop({bgFile = "Interface/ENCOUNTERJOURNAL/UI-EJ-JournalBG",
							tile = false, edgeFile = "",
							insets = { left = 5, right = -240, top = 5, bottom = bottomBG }});
		
		mainFrame.btnFight = CreateFrame("Button", nil, mainFrame, "UIPanelButtonTemplate");
		mainFrame.btnFight:SetSize(150, 30);
		mainFrame.btnFight:SetPoint("BOTTOM", mainFrame.listFight, "BOTTOM", 0, 1);
		mainFrame.btnFight:SetFrameLevel(mainFrame.listFight:GetFrameLevel() + 1);
		mainFrame.btnFight:SetText(core.L["Combattre"]);
		mainFrame.btnFight:Disable();
	end
	-- REMPLISSAGE DES INFOS
	_G[addon.."_AdventurePanel"].titleFrame.text:SetText(infoAdventure.title);
	_G[addon.."_AdventurePanel"].titleFrame:SetScript("OnClick", function(self)
		if btnFightSelected[addon] then
			btnFightSelected[addon]:UnlockHighlight();
			btnFightSelected[addon] = nil;
		end		
		_G[addon.."_AdventurePanel"].btnFight.infoFight = nil;
		_G[addon.."_AdventurePanel"].btnFight:Disable();
		ShowMainInfoAdventure(infoAdventure, addon);
	end);
	
	if addon == "WCC" then
		_G[addon.."_AdventurePanel"].listFight.btnNewFight:SetScript("OnClick", function(self)
			local newFight = {
				idAdventure  = infoAdventure.id,
				namePlayer   = "",
				nameEnnemies = "",
				idPlayer     = {},
				idEnnemies   = {},
				mobs         = {"0", "0", "0", "0", "0", "0"},
				background   = "",
			};
			table.insert(infoAdventure.fights, newFight)			
			RefreshListFights(infoAdventure.fights, addon);		
			UpdateVersionAdventure(infoAdventure, 1);
		end);
	end
	
	_G[addon.."_AdventurePanel"]:Show();
	ShowMainInfoAdventure(infoAdventure, addon);
	RefreshListFights(infoAdventure.fights, addon);
end

function RefreshListAdventures(addon)
	local list = {};
	if addon == "WC" then
		list = WC_AdventuresData.adventures;
	elseif addon == "WCC" then
		list = YourAdventures;
	end
	local k = -1;
	for i = 1, #list do
		local j = i - 1;
		local btn = _G[addon.."_BtnAdventure"..j];
		if not btn then
			btn = CreateBtnAdventures(list[i], _G[addon.."_ListAdventures"], j, addon);
			btn:ClearAllPoints();
		else
			btn.infoAdventure = list[i];
			btn.title:SetText(list[i].title);
			btn.bg:SetBackdrop({bgFile = "Interface/ENCOUNTERJOURNAL/UI-EJ-DUNGEONBUTTON-"..list[i].picture,
								tile = false, edgeFile = "",
								insets = { left = 0, right = -85, top = 0, bottom = -35 }});
		end
		if (j == 0) then
			btn:SetPoint("TOPLEFT", _G[addon.."_ListAdventures"], "TOPLEFT", 10, -10);
		elseif (j%4 == 0) then
			btn:SetPoint("TOP", _G[addon.."_BtnAdventure"..j - 4], "BOTTOM", 0, -10);
		else
			btn:SetPoint("LEFT", _G[addon.."_BtnAdventure"..j - 1], "RIGHT", 10, 0);
		end
		btn:Show();
		k = j;
	end
	-- HIDE LES FRAMES EN TROP
	for k = k + 1, 999 do
		if not _G[addon.."_BtnAdventure"..k] then
			break;
		else
			_G[addon.."_BtnAdventure"..k]:Hide();
		end
	end
end

function CreateBtnAdventures(infoAdventure, parent, i, addon)
	local mainFrame = CreateFrame("Button", addon.."_BtnAdventure"..i, parent, nil);
	mainFrame:SetSize(180, 100);
	mainFrame:SetFrameLevel(parent:GetFrameLevel() + 2);
	mainFrame:SetNormalTexture("Interface/AddOns/WoWChallenger/images/adventures/AdventureBtn");
	mainFrame:SetPushedTexture("Interface/AddOns/WoWChallenger/images/adventures/AdventureBtn_Pushed");
	mainFrame:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/adventures/AdventureBtn_Hover");
	mainFrame:RegisterForClicks("LeftButtonUp", "RightButtonDown");
	mainFrame.infoAdventure = infoAdventure;
	mainFrame:SetScript("OnClick", function (self, button)
		if button == "LeftButton" then
			if _G[addon.."_SettingsAdventure"] then
				_G[addon.."_SettingsAdventure"]:Hide();
			end
			ShowAdventurePanel(self.infoAdventure, addon);
		elseif button == "RightButton" then
			if addon == "WC" then
				WC_Lib.structures.CDMenuAdventures.infoAdventure = self.infoAdventure;
				WC_Lib.ShowContextMenu(WC_Lib.structures.CDMenuAdventures.contextMenu);
			elseif addon == "WCC" then
				WCCreator_Lib.structures.adventures.CDMenu.infoAdventure = self.infoAdventure;
				WC_Lib.ShowContextMenu(WCCreator_Lib.structures.adventures.CDMenu.contextMenu);
			end
			PlaySound("83", "sfx");
		end
	end);

	mainFrame.bg = CreateFrame("Frame", nil, mainFrame, BackdropTemplateMixin and "BackdropTemplate");
	mainFrame.bg:SetSize(mainFrame:GetWidth(), mainFrame:GetHeight());
	mainFrame.bg:SetFrameLevel(mainFrame:GetFrameLevel() - 1);
	mainFrame.bg:SetPoint("TOPLEFT", mainFrame, "TOPLEFT");
	mainFrame.bg:SetBackdrop({bgFile = "Interface/ENCOUNTERJOURNAL/UI-EJ-DUNGEONBUTTON-"..infoAdventure.picture,
								tile = false, edgeFile = "",
								insets = { left = 0, right = -85, top = 0, bottom = -35 }});
	-- TITRE
	mainFrame.title = mainFrame:CreateFontString(nil, "OVERLAY", nil, mainFrame);
	if GetLocale() == "ruRU" then
		mainFrame.title:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\MORPHEUS__CYR.TTF", 20);
	else
		mainFrame.title:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\MORPHEUS.TTF", 20);
	end
	mainFrame.title:SetPoint("TOP", mainFrame, "TOP", 0, -15);
	mainFrame.title:SetTextColor(1, 0.82, 0);
	mainFrame.title:SetSize(mainFrame:GetWidth(), 0);
	mainFrame.title:SetText(infoAdventure.title);
	
	return mainFrame;
end

function WC_Lib.CreateAdventuresTab(parent, addon)
	local titleTab = parent:CreateFontString(nil, "OVERLAY", nil, parent);
	titleTab:SetFont("Fonts\\FRIZQT__.TTF", 20);
	titleTab:SetPoint("TOPLEFT", parent, "TOPLEFT", 15, -15);
	titleTab:SetTextColor(1, 0.82, 0);
	titleTab:SetText(core.L["Aventures"]);
	
	local BtnImportAdventure = CreateFrame("Button", nil, parent, "UIPanelButtonTemplate");
	BtnImportAdventure:SetSize(150, 32);
	BtnImportAdventure:SetText(core.L["Importer Aventure"]);
	BtnImportAdventure:SetScript("OnClick", function (self, button)
		WC_Lib.structures.CDMenuImport.addon = addon;
		WC_Lib.ShowContextMenu(WC_Lib.structures.CDMenuImport.contextMenu);
		PlaySound("83", "sfx");
	end);
	
	
	
	
	if addon == "WCC" then	
		local BtnCreateAdventure = CreateFrame("Button", nil, parent, "UIPanelButtonTemplate");
		BtnCreateAdventure:SetSize(150, 32);
		BtnCreateAdventure:SetText(core.L["Nouvelle Aventure"]);
		BtnCreateAdventure:SetPoint("TOPRIGHT", parent, "TOPRIGHT", -10, -9);
		BtnCreateAdventure:SetScript("OnClick", function (self)
			ShowSettingsAdventure();
		end);
		
		BtnImportAdventure:SetPoint("RIGHT", BtnCreateAdventure, "LEFT", 0, 0);
	else
		local currencyFrame = CreateFrame("Frame", nil, parent, BackdropTemplateMixin and "BackdropTemplate");
		currencyFrame:SetSize(65, 64);
		currencyFrame:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/player_profile/flagCurrency"});
		currencyFrame:SetPoint("TOPLEFT", parent, "TOPLEFT", 43, 10);
		currencyFrame.text = currencyFrame:CreateFontString("SkillPointsCampaignsAdventure", "OVERLAY");
		currencyFrame.text:SetFontObject("GameFontHighlightLarge");
		currencyFrame.text:SetText(DBProfile.skillPoints);
		currencyFrame.text:SetPoint("CENTER", currencyFrame, "CENTER", 1, -20);
		currencyFrame.tooltip = CreateFrame("Frame", nil, currencyFrame);
		currencyFrame.tooltip:SetSize(40, 64);
		currencyFrame.tooltip:SetPoint("CENTER", currencyFrame, "CENTER");
		currencyFrame.tooltip:SetScript("OnEnter", OnEnterCurrencyAdventure);
		currencyFrame.tooltip:SetScript("OnLeave", WC_Lib.OnLeave);
	
		local btnBack = CreateFrame("Button", nil, parent, "UIPanelButtonTemplate");
		btnBack:SetSize(75, 32);
		btnBack:SetPoint("TOPRIGHT", parent, "TOPRIGHT", -10, -12);
		btnBack:SetText(core.L["Retour"]);
		btnBack:SetScript("OnClick", function(self)
			parent:Hide();
			_G["bgMainMenuCampaign"]:Show();
			PlaySound("858", "sfx");
		end);
		
		BtnImportAdventure:SetPoint("RIGHT", btnBack, "LEFT", 0, 0);
	end
	
	--- ADVENTURE LIST ---
	local listAdventure = CreateFrame("ScrollFrame", nil, parent, "ScrollFrameTemplate");
	listAdventure:SetPoint("TOPLEFT", parent, "TOPLEFT", 0, -47);
	listAdventure:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", -2, 0);
	listAdventure:SetClipsChildren(true);
	listAdventure.ScrollBar:ClearAllPoints();
	listAdventure.ScrollBar:SetPoint("TOPRIGHT", listAdventure, "TOPRIGHT", -12, -15);
	listAdventure.ScrollBar:SetPoint("BOTTOMRIGHT", listAdventure, "BOTTOMRIGHT", -12, 12);
	local child = CreateFrame("Frame", addon.."_ListAdventures", listAdventure, BackdropTemplateMixin and "BackdropTemplate");
	if addon == "WC" then
		titleTab:SetPoint("TOPLEFT", parent, "TOPLEFT", 100, -20);
		child:SetSize(800, 395);
	elseif addon == "WCC" then
		child:SetSize(_G["WoWChallengerCreatorFrame"]:GetWidth(), _G["WoWChallengerCreatorFrame"]:GetHeight() - 105);
	end
	child:SetBackdrop({bgFile = "",
							edgeFile = "",
							tile = false, edgeSize = 16, 
							insets = { left = 0, right = 0, top = 0, bottom = 0 }});
	listAdventure:SetScrollChild(child);
	listAdventure:SetScript("OnMouseWheel", WC_Lib.ScrollFrame_OnMouseWheel);
	
	if addon == "WC" then
		local ret = LoadAllAdventures();
		if not ret then
			UIErrorsFrame:AddMessage(core.L["Erreur sur le chargement des aventures."], 1, 0, 0);
		end
	end
	RefreshListAdventures(addon);
	
	if addon == "WC" then
		WC_Lib.DeleteUselessUnitsSpells();
	end
end

function WC_Lib.DeleteUselessUnitsSpells()
	-- Check Units
	for i = #WC_AdventuresData.units, 1, -1 do
		if not WC_Lib.SearchThingUnitInAdventure(WC_AdventuresData.units[i].id, "WC") then
			if not WC_Lib.SearchThingUnitInTriggers(WC_AdventuresData.units[i].id, "WC") then
				table.remove(WC_AdventuresData.units, i);
			end
		end
	end
	-- Check Spells
	for i = #WC_AdventuresData.spells, 1, -1 do
		if not WC_Lib.SearchThingSpellInUnit(WC_AdventuresData.spells[i].id, "WC") then
			if not WC_Lib.SearchThingSpellInTriggers(WC_AdventuresData.spells[i].id, "WC") then
				table.remove(WC_AdventuresData.spells, i);
			end
		end
	end
end