private ActionResult handleLogin(YoutubeResults results)
        {
            if (results.username == null || results.password == null)
            {
                return Content("Empty username/password", "text/html");
            }

            string username = results.username;
            string password = results.password;

            if (loginSuccess(results.username, results.password))
            {
                results.loginMessage = "Success. Logged in as " + results.username;
                results.correct = true;
                results.username = null;
                results.password = null;

                return Content("Success");
            }

            results.loginMessage = "Fail. Invalid username/password";
            results.correct = false;
            results.username = null;
            results.password = null;

            return Content("Invalid username/password");
        }
        //default argument
        public static YoutubeResults NormalSearch(string searchString)
        {
            YoutubeService youtube = new YoutubeService();
            youtube.Key = ConfigurationManager.AppSettings["GoogleAPIKey"]; //Gets the API key from app settings at azure

            SearchResource.ListRequest listRequest = youtube.Search.List("snippet");
            listRequest.Q = searchString;
            listRequest.Order = SearchResource.Order.Relevance;

            SearchListResponse searchResponse = listRequest.Fetch();

            YoutubeResults searchResults = new YoutubeResults();
            searchResults.titles = new List<string>();
            searchResults.videoIDs = new List<string>();
            searchResults.thumbnailURLs = new List<string>();
            searchResults.descriptions = new List<string>();

            foreach (SearchResult searchResult in searchResponse.Items)
            {
                if (searchResult.Id.Kind == "youtube#video")
                {
                    searchResults.titles.Add(searchResult.Snippet.Title);

                    searchResults.thumbnailURLs.Add(searchResult.Snippet.Thumbnails[searchResult.Snippet.Thumbnails.Keys.ToArray()[0]].Url);
                    searchResults.videoIDs.Add(searchResult.Id.VideoId);

                    searchResults.descriptions.Add(searchResult.Snippet.Description);
                }
            }

            return searchResults;
        }
        public static YoutubeResults getResultsForList(List<SongStruct> songs)
        {
            YoutubeService youtube = new YoutubeService();
            youtube.Key = ConfigurationManager.AppSettings["GoogleAPIKey"]; //Gets the API key from app settings at azure

            YoutubeResults searchResults = new YoutubeResults();
            searchResults.titles = new List<string>();
            searchResults.videoIDs = new List<string>();
            searchResults.thumbnailURLs = new List<string>();
            searchResults.descriptions = new List<string>();

            for (int i = 0; i < songs.Count; i++)
            {
                string artist = songs[i].artist;
                string song = songs[i].title;
                //int duration = songs[i].duration;

                string searchString = artist + " " + song;

                SearchResource.ListRequest listRequest = youtube.Search.List("snippet");
                listRequest.Q = searchString;
                listRequest.Order = SearchResource.Order.Relevance;
                listRequest.MaxResults = 1;
                listRequest.Type = "video";

                SearchListResponse searchResponse = listRequest.Fetch();

                SearchResult searchResult = searchResponse.Items[0];

                searchResults.titles.Add(searchResult.Snippet.Title);

                searchResults.thumbnailURLs.Add(searchResult.Snippet.Thumbnails[searchResult.Snippet.Thumbnails.Keys.ToArray()[0]].Url);
                searchResults.videoIDs.Add(searchResult.Id.VideoId);
                searchResults.descriptions.Add(searchResult.Snippet.Description);
            }

            return searchResults;
        }
        public ActionResult Index(YoutubeResults results)
        {
            if (Request.IsAjaxRequest())
            {
                return handleLogin(results);
            }

            if (results.artistSearchString != null)
            {
                results = YouTubeOptimalResultFinder.getResultsForList(LastFmSongList.getTopTracksForArtist(results.artistSearchString));
            }
            else if (results.normalSearchString != null)
            {
                results = YouTubeOptimalResultFinder.NormalSearch(results.normalSearchString);
            }
            else if (results.artistSearchString == null && results.normalSearchString == null)
            {
                results = YouTubeOptimalResultFinder.NormalSearch("");
            }

            //results = Search(results.searchString);

            return View("Index", results);
        }