--Functions (Needed to organize)


local function AddRecentApp(App)
	if RecentAppEnabled == true then
	local sameapp = false
	if home:FindFirstChild(App) then
		if side.RecentApps:FindFirstChild(App) then
			sameapp = true
			end
		side.Status:TweenPosition(UDim2.new(0.094,0,0.638,0), "InOut", "Quint", 0.5, true)
		side.B1:TweenPosition(UDim2.new(0.024,0,0.593,0), "InOut", "Quint", 0.5, true)
		for i, v in pairs(side.RecentApps:GetChildren()) do
			if v.Position == UDim2.new(0.016,0,0.345,0) then
				if sameapp and side.RecentApps:FindFirstChild(App) then
					if side.RecentApps[App].Position ~= UDim2.new(0.015,0,0.027,0) then
							v:TweenPosition(UDim2.new(0.032,0,0.668,0), "InOut", "Quint", 0.25, true)
					end
				else
						v:TweenPosition(UDim2.new(0.032,0,0.668,0), "InOut", "Quint", 0.25, true)
					end
				end
			if v.Position == UDim2.new(0.015,0,0.027,0) then
				v:TweenPosition(UDim2.new(0.016,0,0.345,0), "InOut", "Quint", 0.25, true)
			end
			if v.Name == App then --prevents any same apps to be duplicated ok
				v:TweenPosition(UDim2.new(0.015,0,0.027,0), "InOut", "Quint", 0.25, true)
				end
			
			if v.Position == UDim2.new(0.032,0,0.668,0) then
				if not sameapp then
				for i = 0, 1, 0.2 do
					wait()
					v.ImageTransparency = i
				end
				v:Destroy()
				end
			end
			end
		if not sameapp then
					local app = home:FindFirstChild(App):Clone()
				app.Parent = side.RecentApps
				app.IconName.Visible = false
				app.ZIndex = 102
				app.Size = UDim2.new(0.968, 0, 0.358, 0)
				app.ImageTransparency = 1
				app.Position = UDim2.new(0.015,0,0.027,0)
				for i = 1, 0, -0.2 do
					wait()
					app.ImageTransparency = i
		end
		end
		end
	end
	end

local function PlayMusic(Title, ID, pitch)
	 local musictitle = script.Parent.CarPlay.NowPlaying.MusicTitle
	 local id = script.Parent.CarPlay.NowPlaying.MusicID
		musictitle.Text = Title
		id.Text = ID
		filter:FireServer("updateSong", ID, pitch)
	handler:FireServer("PlayMusic", Title, ID)
		nowplaying.Play.ImageRectOffset = Vector2.new(150, 0)
end

local function PlayMusicUsingSongValue(Val, Playlists, pitch)
	if script.Parent.CarPlay_Data.Playlists:FindFirstChild(Playlists) then
		 local musictitle = script.Parent.CarPlay.NowPlaying.MusicTitle
	 local id = script.Parent.CarPlay.NowPlaying.MusicID
		local playlists = script.Parent.CarPlay_Data.Playlists[Playlists]:GetChildren()
		musictitle.Text = playlists[Val].Name
		handler:FireServer("PlayMusic", playlists[Val].Name, playlists[Val].Value)
		id.Text = playlists[Val].Value
		filter:FireServer("updateSong", playlists[Val].Value, playlists[Val].Pitch.Value)
	end
end


local function LaunchApp(AppToLaunch)
	if home:FindFirstChild(AppToLaunch) then
		handler:FireServer("LaunchApp", AppToLaunch)
		for i, app in ipairs(home:GetChildren()) do
			app.AppEnabled.Value = false -- no jerks that can mess carplay
		end
		AddRecentApp(AppToLaunch)
		home[AppToLaunch].IconName.Visible = false
		home[AppToLaunch].ZIndex = 102
		X = home[AppToLaunch].Position.X.Scale
		Y = home[AppToLaunch].Position.Y.Scale
		home[AppToLaunch]:TweenSizeAndPosition(UDim2.new(1.599, 0,2.591, 0), UDim2.new(-0.325, 0,-0.965, 0), "Out", "Quint", 1.2, true)
		home[AppToLaunch].Parent = script.Parent.CarPlay.Homescreen
		for i, app in ipairs(script.Parent.CarPlay:GetChildren()) do
			if app.Name == AppToLaunch then
				app.Visible = true
			end
		end
		home.Visible = false
		for i = 0, 1, 0.2 do
			wait()
			home.Parent[AppToLaunch].ImageTransparency = i
		end
		home.Parent.Visible = false
end
end

local function LaunchHome()
	for i, v in pairs(home.Parent:GetChildren()) do
		if v.Name == "Music" or v.Name == "Maps" or v.Name == "Settings" or v.Name == "BMW" or v.Name == "NowPlaying" or v.Name == "Podcasts" or v.Name == "Phone" then
			handler:FireServer("LaunchHome")
			script.Parent.CarPlay.Music.Library.Visible = true
			script.Parent.CarPlay.Music.Playlists.Visible = false
			for i, app in pairs(script.Parent.CarPlay:GetChildren()) do
				if app:IsA("Frame") and app.Visible == true then
					app.Visible = false
				end
			end
			script.Parent.CarPlay:FindFirstChild(v.Name).Visible = false
			home.Parent.Visible = true
			v:TweenSizeAndPosition(UDim2.new(0.23,0,1,0), UDim2.new(X,0,Y,0), "Out", "Quint", 0.5, true)
			home.Visible = true
			v.ImageTransparency = 0
			v.ZIndex = 2
			v.IconName.Visible = true
			v.Parent = home
			wait(.4)
				for i, app in pairs(home:GetChildren()) do
					if app:FindFirstChild("AppEnabled") then
			app.AppEnabled.Value = true -- no jerks that can mess carplay
			end
		end
		end
	end
end

local function LoadPlaylist(PlaylistName)
	local Y = 0
	local NextY = 0.039
	music.Playlists.MusicPlaylist:ClearAllChildren()
	local Data = script.Parent.CarPlay_Data.Playlists[PlaylistName]
	music.Playlists.PlaylistName.Text = PlaylistName
	for i, musics in ipairs(Data:GetChildren()) do
		if musics:IsA("IntValue") then
			handler:FireServer("LoadPlaylist",PlaylistName, musics.Name, musics.Value, Y)
			local Template = music.Playlists.Template:Clone()
			Template.Name = musics.Value
			Template.MusicTitle.Text = musics.Name
			Template.MusicID.Text = musics.Value
			Template.Position = UDim2.new(0.037,0,Y,0)
			Template.Parent = music.Playlists.MusicPlaylist
			Template.Pitch.Value = musics.Pitch.Value
			Template.Visible = true
			Y = Y + NextY
		end
	end
		for i, playlists in ipairs(script.Parent.CarPlay.Music.Playlists.MusicPlaylist:GetChildren()) do
if playlists:IsA("TextButton") then
	playlists.MouseButton1Down:connect(function()
		TransitionTo("Playlists", "NowPlaying")
		AddRecentApp("NowPlaying")
		script.Parent.CarPlay_Data.NowPlayingData.CurrentPlaylist.Value = PlaylistName
		script.Parent.CarPlay_Data.NowPlayingData.SongNumber.Value = i
		PlayMusic(playlists.MusicTitle.Text, playlists.MusicID.Text, playlists.Pitch.Value)
	end)
end
end
end

local function CreatePlaylists()
	local Y = 0.002
	local NextY = 0.092
	music.Library.LibraryButtons:ClearAllChildren()
	local Data = script.Parent.CarPlay_Data.Playlists
	for i, playlists in ipairs(Data:GetChildren()) do
		if playlists:IsA("Folder") then
			local Template = music.Library:WaitForChild('Template'):Clone()
			Template.Parent = music.Library.LibraryButtons
			Template.Name = playlists.Name
			Template.Visible = true
			handler:FireServer("CreatePlaylist", playlists.Name, Y)
			Template.PlaylistName.Text = playlists.Name
			Template.Position = UDim2.new(0.025, 0, Y, 0)
			Y = Y + NextY
		end
	end
	for i, playlists in ipairs(script.Parent.CarPlay.Music.Library.LibraryButtons:GetChildren()) do
if playlists:IsA("TextButton") then
	playlists.MouseButton1Down:connect(function()
		TransitionTo("Library", "Playlists")
		LoadPlaylist(playlists.Name)
	end)
end
end
end

function TransitionTo(Prev, Next)
	if debounce == false then
		debounce = true
	for i, frame in ipairs(script.Parent.CarPlay:GetDescendants()) do
		if frame:IsA("Frame") and frame.Name == Prev then
			frame:TweenPosition(UDim2.new(-1,0,0,0), "Out", "Quint", 0.5, true)
			local framediss = coroutine.wrap(function()
				wait(.5)
 		   frame.Position = UDim2.new(0,0,0,0)
			frame.Visible = false
			end)
 			framediss()
		end
		if frame:IsA("Frame") and frame.Name == Next then
			frame.Visible = true
			frame.Position = UDim2.new(1,0,0,0)
			frame:TweenPosition(UDim2.new(0,0,0,0), "Out", "Quint", 0.5, true)
		end
		handler:FireServer("Transition", Prev, Next)
		debounce = false
	end
	end
end