-- Container for temporary connections (disconnected automatically)

local Connections = {};

function TextureTool.Equip()
	-- Enables the tool's equipped functionality

	-- Start up our interface
	ShowUI();
	EnableSurfaceClickSelection();

	-- Set our current texture type and face
	SetTextureType(TextureTool.Type);
	SetFace(TextureTool.Face);

end;

function TextureTool.Unequip()
	-- Disables the tool's equipped functionality

	-- Clear unnecessary resources
	HideUI();
	ClearConnections();

end;

function ClearConnections()
	-- Clears out temporary connections

	for ConnectionKey, Connection in pairs(Connections) do
		Connection:Disconnect();
		Connections[ConnectionKey] = nil;
	end;

end;

function ShowUI()
	-- Creates and reveals the UI

	-- Reveal UI if already created
	if UI then

		-- Reveal the UI
		UI.Visible = true;

		-- Update the UI every 0.1 seconds
		UIUpdater = Support.ScheduleRecurringTask(UpdateUI, 0.1);

		-- Skip UI creation
		return;

	end;

	-- Create the UI
	UI = Core.Tool.Interfaces.BTTextureToolGUI:Clone();
	UI.Parent = Core.UI;
	UI.Visible = true;

	-- References to UI elements
	local AddButton = UI.AddButton;
	local RemoveButton = UI.RemoveButton;
	local DecalModeButton = UI.ModeOption.Decal.Button;
	local TextureModeButton = UI.ModeOption.Texture.Button;
	local ImageIdInput = UI.ImageIDOption.TextBox;
	local TransparencyInput = UI.TransparencyOption.Input.TextBox;
	local RepeatXInput = UI.RepeatOption.XInput.TextBox;
	local RepeatYInput = UI.RepeatOption.YInput.TextBox;

	-- Enable the texture type switch
	DecalModeButton.MouseButton1Click:Connect(function ()
		SetTextureType 'Decal';
	end);
	TextureModeButton.MouseButton1Click:Connect(function ()
		SetTextureType 'Texture';
	end);

	-- Create the face selection dropdown
	local Faces = { 'Top', 'Bottom', 'Front', 'Back', 'Left', 'Right' };
	FaceDropdown = Core.Cheer(UI.SideOption.Dropdown).Start(Faces, '', function (Face)
		SetFace(Enum.NormalId[Face]);
	end);

	-- Enable the image ID input
	ImageIdInput.FocusLost:Connect(function (EnterPressed)
		SetTextureId(TextureTool.Type, TextureTool.Face, ParseAssetId(ImageIdInput.Text));
	end);

	-- Enable other inputs
	SyncInputToProperty('Transparency', TransparencyInput);
	SyncInputToProperty('StudsPerTileU', RepeatXInput);
	SyncInputToProperty('StudsPerTileV', RepeatYInput);

	-- Enable the texture adding button
	AddButton.Button.MouseButton1Click:Connect(function ()
		AddTextures(TextureTool.Type, TextureTool.Face);
	end);
	RemoveButton.Button.MouseButton1Click:Connect(function ()
		RemoveTextures(TextureTool.Type, TextureTool.Face);
	end);

	-- Update the UI every 0.1 seconds
	UIUpdater = Support.ScheduleRecurringTask(UpdateUI, 0.1);

end;

function SyncInputToProperty(Property, Input)
	-- Enables `Input` to change the given property

	-- Enable inputs
	Input.FocusLost:Connect(function ()
		SetProperty(TextureTool.Type, TextureTool.Face, Property, tonumber(Input.Text));
	end);

end;

function EnableSurfaceClickSelection()
	-- Allows for the setting of the current face by clicking

	-- Clear out any existing connection
	if Connections.SurfaceClickSelection then
		Connections.SurfaceClickSelection:Disconnect();
		Connections.SurfaceClickSelection = nil;
	end;

	-- Add the new click connection
	Connections.SurfaceClickSelection = Core.Mouse.Button1Down:Connect(function ()
		local _, ScopeTarget = Core.Targeting:UpdateTarget()
		if Selection.IsSelected(ScopeTarget) then
			SetFace(Core.Mouse.TargetSurface)
		end
	end)

end;

function HideUI()
	-- Hides the tool UI

	-- Make sure there's a UI
	if not UI then
		return;
	end;

	-- Hide the UI
	UI.Visible = false;

	-- Stop updating the UI
	UIUpdater:Stop();

end;

function GetTextures(TextureType, Face)
	-- Returns all the textures in the selection

	local Textures = {};

	-- Get any textures from any selected parts
	for _, Part in pairs(Selection.Parts) do
		for _, Child in pairs(Part:GetChildren()) do

			-- If this child is texture we're looking for, collect it
			if Child.ClassName == TextureType and Child.Face == Face then
				table.insert(Textures, Child);
			end;

		end;
	end;

	-- Return the found textures
	return Textures;

end;
