local _, core = ...;
core.Tab = {};
core.Config = {};

local Config = core.Config;
local UIConfig;
local UIListMissions;
WC_Lib.isOnRankingTab = false;
FirstShow = true;
firstRefreshRanking = false;

----- WC CREATOR -----
local WCC_Loaded = false;


print(core.L["StartText1"]);
print(core.L["StartText3"]);
print(core.L["StartText4"]);
print(core.L["StartText5"]);

local function Tab1_OnClick(self)
	PanelTemplates_SetTab(self:GetParent(), self:GetID());
	Tab2Content:Hide();
	Tab3Content:Hide();
	Tab4Content:Hide();
	core.onTabCampaign    = true;
	WC_Lib.isOnRankingTab = false;
	WC_Lib.SetShowHideToModel("show");
	self:GetParent():SetSize(800,475);
	_G["WC_Draggable"]:SetSize(800,475);
	self.content:Show();
end

local function Tab2_OnClick(self)
	PanelTemplates_SetTab(self:GetParent(), self:GetID());
	Tab1Content:Hide();
	Tab3Content:Hide();
	Tab4Content:Hide();
	core.onTabCampaign    = false;
	WC_Lib.isOnRankingTab = false;
	WC_Lib.SetShowHideToModel("hide");
	self:GetParent():SetSize(800,475);
	_G["WC_Draggable"]:SetSize(800,475);
	self.content:Show();
end

local function Tab3_OnClick(self)
	PanelTemplates_SetTab(self:GetParent(), self:GetID());
	Tab1Content:Hide();
	Tab2Content:Hide();
	Tab4Content:Hide();
	core.onTabCampaign    = false;
	WC_Lib.isOnRankingTab = false;
	WC_Lib.SetShowHideToModel("hide");
	self:GetParent():SetSize(800,475);
	_G["WC_Draggable"]:SetSize(800,475);
	self.content:Show();
end

local function Tab4_OnClick(self)
	PanelTemplates_SetTab(self:GetParent(), self:GetID());
	Tab1Content:Hide();
	Tab2Content:Hide();
	Tab3Content:Hide();
	core.onTabCampaign    = false;
	WC_Lib.isOnRankingTab = true;
	WC_Lib.SetShowHideToModel("hide");
	self:GetParent():SetSize(500,475);
	_G["WC_Draggable"]:SetSize(500,475);
	-- UPDATE CONTENT --
	WC_Lib.SetPlayerScore();
	if not firstRefreshRanking then
		firstRefreshRanking = true;
		--WC_Lib.SendInfoPlayer();
		WC_Lib.UpdateRankingList("GUILD")
		WC_Lib.UpdateRankingList("INSTANCE_CHAT")
	end
	self.content:Show();
end

function Config:Toggle()
	local menu = frameDraggable or Config:CreateMenu();
	if (menu:IsShown()) then
		WC_Lib.SetShowHideToModel("hide");
		PlaySound("680", "sfx");
		menu:SetShown(false);
	elseif (not menu:IsShown()) then
		WC_Lib.SetShowHideToModel("show");
		PlaySound("679", "sfx");
		menu:SetShown(true);
		 if (FirstShow == true) then
			WCC_Loaded = LoadAddOn("WoWChallengerCreator");
		 end
		FirstShow = false;
	end
end

function Config:CreateMenu()
	frameDraggable = CreateFrame("Frame", "WC_Draggable", UIParent, nil);
	frameDraggable:SetMovable(true);
	frameDraggable:EnableMouse(true);
	frameDraggable:RegisterForDrag("LeftButton");
	frameDraggable:SetScript("OnDragStart", frameDraggable.StartMoving);
	frameDraggable:SetScript("OnDragStop", frameDraggable.StopMovingOrSizing);
	frameDraggable:SetSize(800,475);
	frameDraggable:SetFrameLevel(0);
	frameDraggable:SetPoint("CENTER", UIParent, "CENTER");
	
	UIConfig = CreateFrame("Frame", "WoWChallengerFrame", frameDraggable, "PortraitFrameTemplateNoCloseButton");
	UIConfig:SetSize(800,475);
	UIConfig:SetPoint("CENTER", frameDraggable, "CENTER");
	UIConfig:SetScript("OnKeyDown", function(self, key)
		if (key == "ESCAPE") then
			PlaySound("680", "sfx");
			WC_Lib.SetShowHideToModel("hide");
			frameDraggable:Hide();
		end
	end);
	UIConfig:SetScript("OnHide", function()
		if (FirstShow == false) then
			WC_Lib.SetShowHideToModel("hide");
		end
	end);
	UIConfig:SetPropagateKeyboardInput(true)

	UIConfig.portrait = CreateFrame("Frame", "WC_PortraitFrame", UIConfig, BackdropTemplateMixin and "BackdropTemplate");
	UIConfig.portrait:SetSize(60,60);
	UIConfig.portrait:SetFrameLevel(15);
	UIConfig.portrait:SetPoint("TOPLEFT", UIConfig, "TOPLEFT", -5, 7);
	UIConfig.portrait:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/difficulties/icon_wc",
												tile = false,
												insets = { left = 0, right = 0, top = 0, bottom = 0 }
												});

	UIConfig.title = UIConfig.TitleContainer:CreateFontString(nil, "OVERLAY", "GameFontNormal", UIConfig);
	UIConfig.title:SetText("WoW Challenger");
	UIConfig.title:SetPoint("CENTER", UIConfig.TitleContainer, "CENTER", 25, 0);
	
	UIConfig.close = CreateFrame("Button", "BtnCloseWC", UIConfig, "UIPanelCloseButton");
	UIConfig.close:SetSize(32, 30);
	UIConfig.close:SetPoint("TOPRIGHT", UIConfig, "TOPRIGHT", 6, 4);
	UIConfig.close:SetScript("OnClick", function(self)
		frameDraggable:Hide();
		PlaySound("680", "sfx");
	end);
	
	------- WC CREATOR -------
	local tooltipWCCreatorMissing = function(self, motion)
		GameTooltip:SetOwner(self, "ANCHOR_TOP", 0, 0);
		GameTooltip:AddLine(core.L["Téléchargez WoW Challenger Creator"], 1, 1, 1);
		GameTooltip:AddLine(" ");
		GameTooltip:AddLine(core.L["WCC Presentation"], 1, 1, 1, 1);
		GameTooltip:Show();
	end
	
	StaticPopupDialogs["DL_CREATOR"] = {
		text = core.L["TextUrl"],
		button1 = core.L["Close"],
		OnShow = function (self, data)
			self.editBox:SetText("https://www.curseforge.com/wow/addons/wow-challenger-creator");
			self.editBox:SetSize(250, 50);
			self.editBox:HighlightText();
		end,
		whileDead = true,
		hasEditBox = true
	}
	local BtnActiveWCCreator = CreateFrame("Button", "BtnActiveWCCreator", UIConfig.TitleContainer, "UIPanelButtonTemplate");
	BtnActiveWCCreator:SetSize(110, 20);
	BtnActiveWCCreator:SetText("WC Creator");
	BtnActiveWCCreator:SetPoint("LEFT", UIConfig.TitleContainer, "LEFT", 57, 0);
	BtnActiveWCCreator:SetScript("OnLeave", WC_Lib.OnLeave);
	local loaded = IsAddOnLoaded("WoWChallengerCreator");
	if (loaded == true) then
		BtnActiveWCCreator:SetScript("OnClick", WCCreator_Lib.OnClick_ShowWCC);
		BtnActiveWCCreator:SetScript("OnEnter", WCCreator_Lib.tooltip.tooltip_WCCreatorOn);
	else
		BtnActiveWCCreator:SetScript("OnEnter", tooltipWCCreatorMissing);
		BtnActiveWCCreator:SetScript("OnClick", function(self) StaticPopup_Show("DL_CREATOR"); end);
	end
	----- END WC CREATOR -----
	
	------- BOUTON REJOINDRE -------
	local BtnJoin = CreateFrame("Button", "WC_BtnJoin", UIConfig.TitleContainer, "UIPanelButtonTemplate");
	BtnJoin:SetSize(110, 20);
	BtnJoin:SetText(core.L["Join"]);
	BtnJoin:SetPoint("RIGHT", UIConfig.TitleContainer, "RIGHT", -27, 0);
	BtnJoin.contextMenu = CreateFrame("Frame", nil, btn, "UIDropDownMenuTemplate");
	BtnJoin:SetScript("OnClick", function(self)
		EasyMenu(WC_Lib.structures.WCJoinMenu.info, BtnJoin.contextMenu, BtnJoin, 0 , 0, "MENU")
	end);
	------- END BOUTON REJOINDRE -------
	local frameName = UIConfig:GetName();
	UIConfig.numTabs = 4;

	local tab1 = CreateFrame("Button", frameName.."Tab1", UIConfig, "PanelTabButtonTemplate");
	tab1:SetID(1);
	tab1:SetText(core.L["Campaigns"]);
	tab1:SetScript("OnClick", Tab1_OnClick);
	tab1:SetPoint("TOPLEFT", UIConfig, "BOTTOMLEFT", 0, 1);
	tab1.content = CreateFrame("Frame", "Tab1Content", UIConfig);
	tab1.content:SetPoint("TOPLEFT", UIConfig, "TOPLEFT", 0, -20);
	tab1.content:SetPoint("BOTTOMRIGHT", UIConfig, "BOTTOMRIGHT");
	tab1.content:Hide();
	core.Tab.t1 = tab1;

	local tab2 = CreateFrame("Button", frameName.."Tab2", UIConfig, "PanelTabButtonTemplate");
	tab2:SetID(2);
	tab2:SetText(core.L["Challenges"]);
	tab2:SetScript("OnClick", Tab2_OnClick);
	tab2:SetPoint("TOPLEFT", _G[frameName.."Tab1"], "TOPRIGHT", 4, 0);
	tab2.content = CreateFrame("Frame", "Tab2Content", UIConfig);
	tab2.content:SetPoint("TOPLEFT", UIConfig, "TOPLEFT", 8, -28);
	tab2.content:SetPoint("BOTTOMRIGHT", UIConfig, "BOTTOMRIGHT", -9, 7);
	tab2.content:Hide();
	core.Tab.t2 = tab2;
	
	local tab3 = CreateFrame("Button", frameName.."Tab3", UIConfig, "PanelTabButtonTemplate");
	tab3:SetID(3);
	tab3:SetText(core.L["Collections"]);
	tab3:SetScript("OnClick", Tab3_OnClick);
	tab3:SetPoint("TOPLEFT", _G[frameName.."Tab2"], "TOPRIGHT", 4, 0);
	tab3.content = CreateFrame("Frame", "Tab3Content", UIConfig);
	tab3.content:SetPoint("TOPLEFT", UIConfig, "TOPLEFT", 0, -20);
	tab3.content:SetPoint("BOTTOMRIGHT", UIConfig, "BOTTOMRIGHT");
	tab3.content:Hide();
	core.Tab.t3 = tab3;
	
	local tab4 = CreateFrame("Button", frameName.."Tab4", UIConfig, "PanelTabButtonTemplate");
	tab4:SetID(4);
	tab4:SetText(core.L["Ranking"]);
	tab4:SetScript("OnClick", Tab4_OnClick);
	tab4:SetPoint("TOPLEFT", _G[frameName.."Tab3"], "TOPRIGHT", 4, 0);
	tab4.content = CreateFrame("Frame", "Tab4Content", UIConfig);
	tab4.content:SetPoint("TOPLEFT", UIConfig, "TOPLEFT", 0, -20);
	tab4.content:SetPoint("BOTTOMRIGHT", UIConfig, "BOTTOMRIGHT");
	tab4.content:Hide();
	core.Tab.t4 = tab4;
	
	Tab1_OnClick(_G[frameName.."Tab1"]);

	core.CampaignTab();
	core.ChallengesTab(core.Tab.t2.content);
	WC_Lib.CreateCollectionsTab(core.Tab.t3.content);
	WC_Lib.CreateRankingTab(core.Tab.t4.content);
	UIConfig:Hide();
	return UIConfig;
end

local icon_minimap;
function Config:CreateMinimap()
	WC_LDB = LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject("WoW Challenger", {
		type = "launcher",
		icon = "Interface/AddOns/WoWChallenger/images/difficulties/icon_wc",
		tocname = "wowchallenger",
		OnClick = function()
			core.Config.Toggle()
		end,
		OnTooltipShow = function(tooltip)
			tooltip:AddLine("WoW Challenger", 1, 1, 1);
			tooltip:AddLine(core.setStringProgressionCampaign());
			tooltip:AddLine(core.setStringProgressionHF());
			tooltip:AddLine(WC_Lib.setStringProgressionGuides());
		end,
	})
	icon_minimap = LibStub("LibDBIcon-1.0");
	icon_minimap:Register("WoW Challenger", WC_LDB, Prefer);
end


local loadmap = CreateFrame("FRAME");
loadmap:RegisterEvent("ADDON_LOADED");
function loadmap:OnEvent(event, arg1)
	if event == "ADDON_LOADED" and arg1 == "WoWChallenger" then
		Config:CreateMinimap();
	end
end
loadmap:SetScript("OnEvent", loadmap.OnEvent);

SLASH_WC1 = "/wc";
SLASH_MAPON1 = "/wcmapon";
SLASH_MAPOFF1 = "/wcmapoff";
SlashCmdList["WC"] = function(msg)
   Config:Toggle();
end

SlashCmdList["MAPON"] = function(msg)
	Prefer.hide = false;
	icon_minimap:Show("WoW Challenger");
end

SlashCmdList["MAPOFF"] = function(msg)
	Prefer.hide = true;
	icon_minimap:Hide("WoW Challenger");
end