--[[
	// Filename: EarplugsClient.lua
	// Written by: Celebras
	// Date: 5/2/2018
	// Updated: 5/2/2018
--]]


local	Players					=	game:GetService("Players")

local	Tool					=	script.Parent
local	Remote					=	Tool:WaitForChild("Remote")

local	Plr						=	Players.LocalPlayer
local	PlrGui					=	Plr:WaitForChild("PlayerGui")

local	Gui
local	Func

local	Playing					=	false

function Action()
	if not Gui then
		local Response = Remote:InvokeServer("Activate")
		if Response then
			ChooseSong()
		end
	else
		Unequip()
	end
end

function Unequip()
	if Func then
		Func:Disconnect()
		Func = nil
	end
	
	if Gui then
		Gui:Destroy()
		Gui = nil
	end
end

function Equip(Mouse)
	Unequip()
	
	Mouse.Button1Down:Connect(Action)
end

function ChooseSong()
	if PlrGui:FindFirstChild("ChooseSongGui") then
		return
	end
	
	local SG = Instance.new("ScreenGui")
	SG.Name = "ChooseSongGui"
	
	local Frame = Instance.new("Frame")
	Frame.Style = Enum.FrameStyle.RobloxRound
	Frame.Size = UDim2.new(.25, 0, .25, 0)
	Frame.AnchorPoint = Vector2.new(.5, .5)
	Frame.Active = true
	Frame.Draggable = true
	Frame.Position = UDim2.new(.5, 0, .5, 0)
	Frame.Parent = SG
	
	local Text = Instance.new("TextLabel")
	Text.BackgroundTransparency = 1
	Text.TextColor3 = Color3.new(1, 1, 1)
	Text.Font = Enum.Font.SourceSansBold
	Text.TextStrokeTransparency = .75
	Text.TextScaled = true
	Text.Size = UDim2.new(1, 0, .5, 0)
	Text.Text = "Put in the ID number for a song that's been uploaded to ROBLOX.\nLeave it blank to stop playing music."
	Text.Parent = Frame
	
	local Input = Instance.new("TextBox")
	Input.BackgroundColor3 = Color3.new(0, 0, 0)
	Input.BackgroundTransparency = .5
	Input.BorderColor3 = Color3.new(1, 1, 1)
	Input.TextColor3 = Color3.new(1, 1, 1)
	Input.Font = Enum.Font.SourceSansBold
	Input.TextStrokeTransparency = .75
	Input.TextScaled = true
	Input.Size = UDim2.new(1, 0, .2, 0)
	Input.Position = UDim2.new(0, 0, .5, 0)
	Input.Text = "ID"
	Input.Parent = Frame
	
	local Button = Instance.new("TextButton")
	Button.Style = Enum.ButtonStyle.RobloxButton
	Button.AnchorPoint = Vector2.new(.5, .5)
	Button.TextColor3 = Color3.new(1, 1, 1)
	Button.Font = Enum.Font.SourceSansBold
	Button.TextStrokeTransparency = .75
	Button.TextScaled = true
	Button.Size = UDim2.new(.75, 0, .3, 0)
	Button.Position = UDim2.new(.5, 0, .85, 0)
	Button.Text = Playing and "Stop" or not Playing and "Play!"
	Button.Parent = Frame
	
	Func = Button.MouseButton1Click:Connect(function()
		if Playing then
			Playing = false
			Button.Text = Playing and "Stop" or not Playing and "Play!"
			Remote:InvokeServer("PlaySong", Input.Text, true)
			return
		end
		
		local Response = Remote:InvokeServer("PlaySong", Input.Text, Playing)
		if Response == "Success" then
			Playing = true
			SG:Destroy()
		else
			Text.Text = Response
			delay(1.5, function()
				if Text.Text == Response then
					Text.Text = "Put in the ID number for a song that's been uploaded to ROBLOX.\nLeave it blank to stop playing music."
				end
			end)
		end
	end)
	
	SG.Parent = PlrGui
	
	Gui = SG
end

Tool.Equipped:Connect(Equip)
Tool.Unequipped:Connect(Unequip)
