-- Create component
local Component = Cheer.CreateComponent('BTToolInformationManager', View);

function Component.Start(Core)

	-- Save reference to core API
	getfenv(1).Core = Core;

	-- Return component for chaining
	return Component;

end;

function Component.RegisterSection(SectionName)
	-- Registers triggers for the given section

	-- Get section
	local Section = Component.GetSection(SectionName);

	-- Reset fade timer on hover
	Cheer.Bind(Section.MouseEnter, function ()
		if Component.CurrentSection == Section then
			Component.CurrentFadeTimer = false;
		end;
	end);

	-- Start fade time on unhover
	Cheer.Bind(Section.MouseLeave, function ()
		Component.StartFadeTimer(true);
	end);

end;

function Component.StartFadeTimer(Override)
	-- Creates timer to disappear current section after 2 seconds unless overridden

	if Component.CurrentFadeTimer == false and not Override then
		return;
	end;

	-- Generate unique trigger ID
	local TriggerId = HttpService:GenerateGUID();

	-- Register timer
	Component.CurrentFadeTimer = TriggerId;

	-- Start timer
	Delay(2, function ()
		if Component.CurrentFadeTimer == TriggerId then
			Component.HideCurrentSection();
		end;
	end);

end;

function Component.StartShowTimer(SectionName)
	-- Creates timer to show content after hovering for over half a second

	-- Only override current section if also triggered by hover
	if Component.LastTrigger == 'Click' then
		return;
	end;

	-- Generate unique trigger ID
	local TriggerId = HttpService:GenerateGUID();

	-- Register timer
	Component.CurrentShowTimer = TriggerId;

	-- Start timer
	Delay(0.25, function ()
		if Component.CurrentShowTimer == TriggerId then
			Component.ShowSection(SectionName, 'Hover');
			Component.StartFadeTimer();
		end;
	end);

end;

function Component.ProcessHover(Tool, SectionName)

	-- Only override current section if also triggered by hover
	if Component.LastTrigger == 'Click' then
		return;
	end;

	wait();

	-- Start a show timer
	Component.StartShowTimer(SectionName);

end;

function Component.ShowSection(SectionName, TriggerType)

	-- Hide any current section
	Component.HideCurrentSection();

	-- Get section
	local Section = Component.GetSection(SectionName);

	-- Set new current section
	Component.CurrentSection = Section;
	Component.LastTrigger = TriggerType;

	-- Show the new section
	Section.Visible = true;

end;

function Component.ProcessUnhover(Tool, SectionName)

	-- Clear any other show timer
	Component.CurrentShowTimer = nil;

	-- Only override current section if triggered by a hover
	if Component.LastTrigger == 'Click' then
		return;
	end;

	-- Get section
	local Section = Component.GetSection(SectionName);

	-- Disappear after 2 seconds unless overridden
	if Component.CurrentSection == Section then
		Component.StartFadeTimer();
	end;

end;

function Component.ProcessClick(Tool, SectionName)

	-- Show the new section
	Component.ShowSection(SectionName, 'Click');

	-- Disappear after 2 seconds unless overridden
	Component.StartFadeTimer();

end;

function Component.HideCurrentSection()

	-- Ensure there is a current section
	if not Component.CurrentSection then
		return;
	end;

	-- Hide section
	Component.CurrentSection.Visible = false;

	-- Disable section fade timer if any
	Component.CurrentFadeTimer = nil;

	-- Unregister current section
	Component.CurrentSection = nil;
	Component.LastTrigger = nil;

end;

function Component.GetSection(SectionName)

	-- Return the information section with the given name
	return View:FindFirstChild(SectionName);

end;

return Component;