local _, core = ...;
local thanksDB = core.DBthanks;

StaticPopupDialogs["SUPPORT"] = {
	text = core.L["TextUrl"],
	button1 = core.L["Close"],
	OnShow = function (self, data)
		self.editBox:SetText("https://www.patreon.com/WoWChallenger")
		self.editBox:SetSize(250, 50);
	end,
	whileDead = true,
	hasEditBox = true
}

StaticPopupDialogs["DISCORD"] = {
	text = core.L["TextUrl"],
	button1 = core.L["Close"],
	OnShow = function (self, data)
		self.editBox:SetText("https://discord.gg/cc5TGnd")
		self.editBox:SetSize(250, 50);
	end,
	whileDead = true,
	hasEditBox = true
}

local OnEnterHelp = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_TOP", 0, 5);
	GameTooltip:AddLine(core.L["Contributor"]);
	GameTooltip:AddLine(core.L["Contributed"], 1, 1, 1, 1);
	GameTooltip:Show();
end

local OnEnterUser = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_TOPLEFT", 0, 5);
	if (self.rankTooltip == 1) then
		GameTooltip:AddLine("Common Tier", 1, 1, 1, 1);
		if (self.realmTooltip ~= nil) then
			GameTooltip:AddLine(core.L["Realm"]..self.realmTooltip, 1, 1, 1, 1);
		end
	elseif (self.rankTooltip == 2) then
		GameTooltip:AddLine("Uncommon Tier", 0.12, 1, 0, 1);
		if (self.realmTooltip ~= nil) then
			GameTooltip:AddLine(core.L["Realm"]..self.realmTooltip, 1, 1, 1, 1);
		end
	elseif (self.rankTooltip == 3) then
		GameTooltip:AddLine("Rare Tier", 0, 0.44, 1, 1);
		if (self.realmTooltip ~= nil) then
			GameTooltip:AddLine(core.L["Realm"]..self.realmTooltip, 1, 1, 1, 1);
		end
	elseif (self.rankTooltip == 4) then
		GameTooltip:AddLine("Epic Tier", 0.64, 0.21, 0.93 , 1);
		if (self.realmTooltip ~= nil) then
			GameTooltip:AddLine(core.L["Realm"]..self.realmTooltip, 1, 1, 1, 1);
		end
		GameTooltip:AddLine(self.textTooltip, 1, 1, 1);
	elseif (self.rankTooltip == 5) then
		GameTooltip:AddLine("Legendary Tier", 1, 0.5, 0, 1);
		if (self.realmTooltip ~= nil) then
			GameTooltip:AddLine(core.L["Realm"]..self.realmTooltip, 1, 1, 1, 1);
		end
		GameTooltip:AddLine(self.textTooltip, 1, 1, 1);
	elseif (self.rankTooltip == 6) then
		GameTooltip:SetOwner(self, "ANCHOR_TOP", 0, 15);
		GameTooltip:AddLine("God Tier", 0.9, 0.8, 0.5, 1);
		if (self.realmTooltip ~= nil) then
			GameTooltip:AddLine(core.L["Realm"]..self.realmTooltip, 1, 1, 1, 1);
		end
		GameTooltip:AddLine(self.textTooltip, 1, 1, 1);
	end
	GameTooltip:Show();
end

local OnLeave = function(self, motion)
	GameTooltip:Hide()
end

local function CreateThanksTab()
	local bgThanks = CreateFrame("Frame", "bgThanksFrame", core.Tab.t4.content, nil);
	bgThanks:SetSize(800,450);
	bgThanks:SetPoint("CENTER", core.Tab.t4.content, "CENTER");
	local thanksPart = CreateFrame("Frame", nil, core.Tab.t4.content, nil);
	thanksPart:SetSize(798,220);
	thanksPart:SetPoint("BOTTOM", core.Tab.t4.content, "BOTTOM", 0, 27);
	thanksPart: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 = -50 }});
							
	thanksPart.ScrollFrame = CreateFrame("ScrollFrame",  nil, thanksPart, "UIPanelScrollFrameTemplate");
	thanksPart.ScrollFrame:SetPoint("TOPLEFT", thanksPart, "TOPLEFT", 5, -5);
	thanksPart.ScrollFrame:SetPoint("BOTTOMRIGHT", thanksPart, "BOTTOMRIGHT", -5, 5);
	thanksPart.ScrollFrame:SetClipsChildren(true);
	thanksPart.ScrollFrame.ScrollBar:ClearAllPoints();
	thanksPart.ScrollFrame.ScrollBar:SetPoint("TOPRIGHT", thanksPart.ScrollFrame, "TOPRIGHT", 0, -15);
	thanksPart.ScrollFrame.ScrollBar:SetPoint("BOTTOMRIGHT", thanksPart.ScrollFrame, "BOTTOMRIGHT", 0, 15);
	
	thanksPart.ScrollFrame.child = CreateFrame("Frame", "ScrollThanksPart", thanksPart.ScrollFrame);
	thanksPart.ScrollFrame.child:SetSize(798, 220);
	thanksPart.ScrollFrame.child:SetPoint("TOPRIGHT", thanksPart.ScrollFrame, "TOPRIGHT", -5, -20);
	thanksPart.ScrollFrame.child:SetPoint("BOTTOMRIGHT", thanksPart.ScrollFrame, "BOTTOMRIGHT", -5, 20);
	
	thanksPart.ScrollFrame:SetScrollChild(thanksPart.ScrollFrame.child);
	thanksPart.ScrollFrame:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel);
	
	btnSupport = CreateFrame("Button", "btnSupportFrame", bgThanks, "UIPanelButtonTemplate");
	btnSupport:SetSize(150, 25);
	btnSupport:SetText(core.L["Support"]);
	btnSupport:SetPoint("BOTTOM", bgThanks, "BOTTOM", 75, 2);
	btnSupport:SetScript("OnClick", function(self)
		StaticPopup_Show ("SUPPORT");
	end);
	
	btnDiscord = CreateFrame("Button", "btnDiscordFrame", bgThanks, "UIPanelButtonTemplate");
	btnDiscord:SetSize(150, 25);
	btnDiscord:SetText(core.L["JoinDiscord"]);
	btnDiscord:SetPoint("BOTTOM", bgThanks, "BOTTOM", -75, 2);
	btnDiscord:SetScript("OnClick", function(self)
		StaticPopup_Show ("DISCORD");
	end);

	local function PrintNameGod(User, i)
		local GodFrame = CreateFrame("Frame", "GodFrame"..i, bgThanks);
		GodFrame:SetSize(202, 210);
		GodFrame:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/user/border_GodTier"});
		if (i == 1) then
			GodFrame:SetPoint("TOPLEFT", bgThanks, "TOPLEFT", 6, 0);
		else
			GodFrame:SetPoint("RIGHT", _G["GodFrame"..(i - 1)], "RIGHT", 197, 0);
		end
		GodFrame.icon = CreateFrame("Frame", nil, GodFrame);
		GodFrame.icon:SetSize(202, 210);
		GodFrame.icon:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/user/godTier"});
		GodFrame.icon:SetPoint("CENTER", GodFrame, "CENTER", 0, 0);
		GodFrame.icon:SetFrameLevel(4);
		
		GodFrame.portrait = CreateFrame("Frame", nil, GodFrame.icon);
		GodFrame.portrait:SetSize(59, 61);
		GodFrame.portrait:SetBackdrop({bgFile = User.portrait});
		GodFrame.portrait:SetPoint("CENTER", GodFrame.icon, "CENTER", -1, 4);
		GodFrame.portrait.rankTooltip = 6;
		if (User.textTooltip ~= nil) then
			GodFrame.portrait.textTooltip = User.textTooltip;
		end
		GodFrame.portrait:SetScript("OnEnter", OnEnterUser);
		GodFrame.portrait:SetScript("OnLeave", OnLeave);
		
		GodFrame.frameText = CreateFrame("Frame", nil, GodFrame);
		GodFrame.frameText:SetSize(155, 25);
		GodFrame.frameText:SetPoint("TOP", GodFrame, "TOP", 0, -20);
		GodFrame.frameText:SetFrameLevel(7);
		GodFrame.text = GodFrame:CreateFontString(nil, "OVERLAY", GodFrame.frameText);
		if GetLocale() == "ruRU" then
			GodFrame.text:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\FRIZQT__CYR.TTF", 20, "OUTLINE");
		else
			GodFrame.text:SetFont("Fonts\\FRIZQT__.TTF", 20, "OUTLINE");
		end
		GodFrame.text:SetText(User.name);
		GodFrame.text:SetSize(GodFrame.frameText:GetWidth(), 0);
		GodFrame.text:SetPoint("CENTER", GodFrame.frameText, "CENTER", 0, 0);
		GodFrame.text:SetTextColor(0.90, 0.80, 0.50, 1);
	end
	
	local function PrintName(User, frameParent)
		local UserFrame = CreateFrame("Frame", "UserFrame"..User.id, frameParent);
		UserFrame:SetSize(135, 25);		
		UserFrame.text = UserFrame:CreateFontString(nil, "OVERLAY", UserFrame);
		UserFrame.text:SetSize(UserFrame:GetWidth(), 0);
		UserFrame.text:SetFontObject("GameFontHighlightLarge");
		UserFrame.text:SetText(User.name);
		UserFrame.IconSupport = CreateFrame("Frame", nil, UserFrame);
		UserFrame.IconSupport:SetSize(32, 32);
		UserFrame.IconSupport:SetPoint("LEFT", UserFrame, "LEFT", -5, 0);
		if (UserFrame.blank == nil) then
			UserFrame.rankTooltip = User.rank;
			if (User.textTooltip ~= nil) then
				UserFrame.textTooltip = User.textTooltip;
			end
			if (User.realm ~= nil) then
				UserFrame.realmTooltip = User.realm;
			end
			UserFrame:SetScript("OnEnter", OnEnterUser);
			UserFrame:SetScript("OnLeave", OnLeave);
			if (User.rank == 1) then
				UserFrame.text:SetJustifyH("LEFT");
				UserFrame.text:SetPoint("LEFT", UserFrame, "LEFT", 30, 0);
				UserFrame.text:SetTextColor(1, 1, 1, 1);
			elseif (User.rank == 2) then
				UserFrame.text:SetJustifyH("LEFT");
				UserFrame.text:SetPoint("LEFT", UserFrame, "LEFT", 30, 0);
				UserFrame.text:SetTextColor(0.12, 1, 0, 1);
			elseif (User.rank == 3) then
				UserFrame.text:SetJustifyH("LEFT");
				UserFrame.text:SetPoint("LEFT", UserFrame, "LEFT", 30, 0);
				UserFrame.text:SetTextColor(0, 0.44, 1, 1);
				UserFrame.IconSupport:SetBackdrop({bgFile = "Interface/PVPFrame/Icons/prestige-icon-6-1"});
			elseif (User.rank == 4) then
				UserFrame.text:SetJustifyH("LEFT");
				UserFrame.text:SetPoint("LEFT", UserFrame, "LEFT", 30, 0);
				UserFrame.text:SetTextColor(0.64, 0.21, 0.93 , 1);
				UserFrame.IconSupport:SetBackdrop({bgFile = "Interface/PVPFrame/Icons/prestige-icon-6-2"});
			elseif (User.rank == 5) then
				UserFrame.text:SetTextColor(1.00, 0.50, 0, 1);
				UserFrame.IconSupport:SetBackdrop({bgFile = "Interface/PVPFrame/Icons/prestige-icon-6-3"});
				UserFrame.Bandeau = CreateFrame("Frame", nil, UserFrame);
				UserFrame.Bandeau:SetSize(135, 40);
				UserFrame.Bandeau:SetPoint("CENTER", UserFrame, "CENTER", 20, -2);
				UserFrame.Bandeau:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/user/bandUser"});
				UserFrame.Bandeau:SetFrameLevel(5);
				UserFrame.text:SetPoint("CENTER", UserFrame, "CENTER", 20, 0);
			end
			if (User.help ~= nil) then
				UserFrame.IconSupport:SetBackdrop({bgFile = "Interface/PVPFrame/Icons/prestige-icon-3-4"});
				UserFrame.IconSupport:SetScript("OnEnter", OnEnterHelp);
				UserFrame.IconSupport:SetScript("OnLeave", OnLeave);
			end
		end
		if (User.id == 0) then
			UserFrame:SetPoint("TOPLEFT", frameParent, "TOPLEFT", 10, -10);
		elseif (User.id%4 == 0) then
			UserFrame:SetPoint("BOTTOM", _G["UserFrame"..(User.id - 4)], "BOTTOM", 0, -40);	
		else
			UserFrame:SetPoint("RIGHT", _G["UserFrame"..(User.id - 1)], "RIGHT", 200, 0);
		end
	end
	
	for i = 1, 4 do
		PrintNameGod(thanksDB[1].users[i], i);
	end
	
	for i = 1, (core.libs.tablelength(thanksDB[2].users)) do
		PrintName(thanksDB[2].users[i], thanksPart.ScrollFrame.child);
	end
end
core.ThanksTab = CreateThanksTab;