public void rptSongs_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            var setSongService = new SetSongService(Ioc.GetInstance<ISetSongRepository>());

            var setSong = setSongService.GetSetSong(new Guid(e.CommandArgument.ToString()));

            if (e.CommandName.ToLower() == "fix")
            {
                txtSongName.Text = setSong.SongName;
                hdnSetSongIdToFix.Value = setSong.SetSongId.ToString();
            }
            else if (e.CommandName.ToLower() == "delete")
            {
                ///TEST THIS SECTION
                var songService = new SongService(Ioc.GetInstance<ISongRepository>());
                var song = songService.GetSong(setSong.SongId.Value);

                using (IUnitOfWork uow = UnitOfWork.Begin())
                {
                    setSongService.Delete(setSong);

                    if (song != null)
                        songService.Delete(song);

                    uow.Commit();
                }

                var setsongs = setSongService.GetAllSetSongs().Where(x => x.SongName.Contains(txtSearchSongName.Text));

                rptSongs.DataSource = setsongs;
                rptSongs.DataBind();
            }
        }
        public void btnChooseAlbum_Click(object sender, EventArgs e)
        {
            var songService = new SongService(Ioc.GetInstance<ISongRepository>());

            var songs = songService.GetSongsByAlbum(ddlAlbum.SelectedValue);

            rptSongs.DataSource = songs;
            rptSongs.DataBind();
        }
        private void Bind()
        {
            SongService songService = new SongService(Ioc.GetInstance<ISongRepository>());

            var albums = songService.GetAlbums();

            if (albums != null)
            {
                ddlAlbum.Items.AddRange(albums);
            }
        }
        private void BindWantedList()
        {
            var wantedListService = new WantedListService(Ioc.GetInstance<IWantedListRepository>());

            var wantedList = wantedListService.GetByUserId(userId);

            var songService = new SongService(Ioc.GetInstance<ISongRepository>());

            var songsWantedList = (from w in wantedList
                                   from s in songService.GetAllSongs()
                                   where s.SongId.Equals(w.SongId)
                                   select new { Song = s, Wanted = w });

            rptWantedList.DataSource = songsWantedList;
            rptWantedList.DataBind();
        }
        public void btnSubmit_Click(object sender, EventArgs e)
        {
            SongService service = new SongService(Ioc.GetInstance<ISongRepository>());

            bool success = false;
            double length = 0;
            short? order = null;
            short? jamType = null;

            if (Validated(out length, out order, out jamType))
            {

                Song song = new Song()
                {
                    SongId = Guid.NewGuid(),
                    SongName = txtSongName.Text.Trim(),
                    SpecialAppearances = txtSpecialAppearances.Text.Trim(),
                    Album = txtAlbum.Text.Trim(),
                    Order = order,
                    Length = length,
                    JamStyle = jamType,
                    Cover = chkCover.Checked,
                    Notes = txtNotes.Text.Trim(),
                    Abbreviation = txtAbbreviation.Text.Trim()
                };

                service.SaveCommit(song, out success);
            }

            if (success)
            {
                phSuccess.Visible = true;
                phError.Visible = false;
            }
            else
            {
                phError.Visible = true;
                phSuccess.Visible = false;
            }
        }
        public void btnFixSetSong_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSongName.Text))
                return;

            if (string.IsNullOrEmpty(hdnSetSongIdToFix.Value))
                return;

            var changeSongNameToo = false;

            if (chkSongToo.Checked)
                changeSongNameToo = true;

            var newSongName = txtSongName.Text.Trim();
            var setSongId = new Guid(hdnSetSongIdToFix.Value);
                        
            using (IUnitOfWork uow = UnitOfWork.Begin())
            {
                var setSongService = new SetSongService(Ioc.GetInstance<ISetSongRepository>());

                var setSong = setSongService.GetSetSong(setSongId);

                setSong.SongName = newSongName;
                var setsongs = setSongService.GetAllSetSongs().Where(x => x.SongName.Contains(txtSearchSongName.Text));

                if (changeSongNameToo)
                {
                    var songService = new SongService(Ioc.GetInstance<ISongRepository>());
                    var song = songService.GetSong(setSong.SongId.Value);
                    song.SongName = newSongName;
                }

                uow.Commit();

                rptSongs.DataSource = setsongs;
                rptSongs.DataBind();
            }
        }
        private FavoriteLiveSongList GetAnalysisPart(Song song)
        {
            var setSongService = new SetSongService(Ioc.GetInstance<ISetSongRepository>());
            var analysisService = new AnalysisService(Ioc.GetInstance<IAnalysisRepository>());
            var songService = new SongService(Ioc.GetInstance<ISongRepository>());

            //Get all Analysis for that Song but in groups of SetSong
            var analysis = analysisService.GetAnalysisBySong(song.SongId).GroupBy(x => x.SetSongId);

            double highestRating = 0;
            double? rating = 0;
            List<Guid> setSongIds = new List<Guid>();

            //If there are no analysis then there is nothing to see here
            if (analysis.Count() == 0)
                return null;

            var fave = new FavoriteLiveSongList(SetSong.FromSong(song));

            //If there are 1 or more analysis then we need to find out which is the highest ranked
            foreach (var a in analysis)
            {
                rating = a.Average(x => x.Rating);
                var setSongId = a.First().SetSongId;

                if (rating.HasValue && rating.Value > highestRating)
                {
                    highestRating = rating.Value;
                    fave.HighestRatedShows = new List<Show>();

                    var setSong = (SetSong)setSongService.GetSetSong(setSongId);
                    var show = GetShowFromSetSong(setSongId);
                    fave.HighestRatedShows.Add(show);
                }
                else if (rating.HasValue && rating.Value == highestRating)
                {
                    var setSong = (SetSong)setSongService.GetSetSong(setSongId);
                    var show = GetShowFromSetSong(setSongId);

                    fave.HighestRatedShows.Add(show);
                }
            }

            fave.HighestRating = highestRating;

            return fave;
        }
        private FavoriteLiveSongList GetAnalysisPart(SetSong setSong)
        {
            var songService = new SongService(Ioc.GetInstance<ISongRepository>());

            return GetAnalysisPart((Song)songService.GetSong(setSong.SongId.Value));
        }
        public List<FavoriteLiveSongList> GenerateFavoriteLiveSongList(IQueryable<ISong> songs)
        {
            var songService = new SongService(Ioc.GetInstance<ISongRepository>());
            var favoriteVersionService = new FavoriteVersionService(Ioc.GetInstance<IFavoriteVersionRepository>());

            List<FavoriteLiveSongList> songLists = new List<FavoriteLiveSongList>();

            foreach (var song in songs)
            {
                var versions = favoriteVersionService.GetAllFavoriteVersions().Where(s => s.SongId == song.SongId).GroupBy(g => g.SetSongId).ToList();

                //If there aren't any favorites chosen for that song then just add the song to be displayed and continue
                if (versions == null || versions.Count() <= 0)
                {
                    var fave = GetAnalysisPart((Song)song);
                    if (fave != null)
                    {
                        songLists.Add(fave);
                    }
                    else
                    {
                        songLists.Add(new FavoriteLiveSongList(SetSong.FromSong((Song)song)));
                    }

                    continue;
                }

                //If there is only 1 favorite version then just use the first one in the collection
                if (versions.Count() == 1)
                {
                    var version = versions[0].First();
                    var setSongId = version.SetSongId.Value;
                    var setSong = (SetSong)setSongService.GetSetSong(setSongId);
                    var show = GetShowFromSetSong(setSongId);

                    var fave = GetAnalysisPart(setSong);
                    if (fave != null)
                    {
                        fave.FavoriteLiveShows.Add(show);
                    }
                    else
                    {
                        fave = new FavoriteLiveSongList(setSong, show);
                    }

                    songLists.Add(fave);
                }
                //There is a lot to check
                else
                {
                    int count = 0;

                    Guid? setSongId = null;

                    FavoriteLiveSongList songList = new FavoriteLiveSongList();

                    foreach (var version in versions)
                    {
                        //If this version has more votes then it needs to be added
                        if (version.Count() >= count)
                        {
                            if (version.Count() > count && count > 0)
                            {
                                //If its not the first time in the loop and this version is the most voted on then clear whatever is in there
                                songList.ClearShows();
                            }

                            //Change the count so that next time it will be right
                            count = version.Count();

                            setSongId = version.First().SetSongId;
                            var setSong = (SetSong)setSongService.GetSetSong(setSongId.Value);
                            var show = GetShowFromSetSong(setSongId.Value);
                            songList.AddFavorite(setSong, show);
                        }
                    }

                    var fave = GetAnalysisPart(setSongId);

                    if (fave != null)
                    {
                        songList.HighestRating = fave.HighestRating;
                        songList.HighestRatedShows = fave.HighestRatedShows;
                    }

                    songLists.Add(songList);
                }
            }

            return songLists;
        }
        private void BindSongs()
        {
            SongService service = new SongService(Ioc.GetInstance<ISongRepository>());

            var songs = service.GetAllSongs().OrderBy(x => x.SongName).ToList();

            int finalPosition = 0;
            ListItem[] collection;

            if (songs != null)
            {
                //Need extra one for the "Please select a song" item
                collection = new ListItem[songs.Count + 1];

                //use song.Count because it has the exact correct amount. Collections length has an extra one for the first item
                for (int i = 0; i < songs.Count; i++)
                {
                    collection[i] = new ListItem(songs[i].SongName, songs[i].SongId.ToString());
                    finalPosition = i + 1;
                }

                ListItem item = new ListItem("Please select a song", "-1");

                collection[finalPosition] = item;

                item.Selected = true;

                lstSongs.Items.AddRange(collection);

            }
        }
        public void btnSubmit_Click(object sender, EventArgs e)
        {
            ResetPanels();

            TopicService tourService = new TopicService(Ioc.GetInstance<ITopicRepository>());
            SongService songService = new SongService(Ioc.GetInstance<ISongRepository>());
            SetService setService = new SetService(Ioc.GetInstance<ISetRepository>());
            SetSongService setSongService = new SetSongService(Ioc.GetInstance<ISetSongRepository>());

            bool success = false;
            bool compiledSuccess = true;

            if (string.IsNullOrEmpty(hdnId.Value))
            {
                Bind();
                return;
            }

            var set = (Set)setService.GetSet(new Guid(hdnId.Value));

            if (set == null)
            {
                Bind();
                return;
            }

            if (lstSongs.Items.Count <= 0)
            {
                Bind();
                return;
            }

            using (IUnitOfWork uow = TheCore.Infrastructure.UnitOfWork.Begin())
            {

                foreach (ListItem item in lstSongs.Items)
                {
                    if (!item.Selected) { continue; }

                    var song = songService.GetSong(new Guid(item.Value));

                    if (song == null) { continue; }

                    short? order = 1;

                    if (set.SetSongs.Count > 0)
                    {
                        order = set.SetSongs.OrderBy(x => x.Order).Last().Order;
                        order++;
                    }

                    SetSong setSong = new SetSong()
                        {
                            Album = song.Album,
                            CreatedDate = DateTime.UtcNow,
                            SetSongId = Guid.NewGuid(),
                            SongId = song.SongId,
                            SongName = song.SongName,
                            Order = order,
                            Set = set,
                            SetId = set.SetId,
                            Segue = chkSegue.Checked
                        };

                    setSongService.Save(setSong, out success);

                    compiledSuccess = compiledSuccess && success;
                }

                if (compiledSuccess)
                {
                    uow.Commit();
                    phSuccess.Visible = true;
                    phError.Visible = false;
                }
                else
                {
                    phError.Visible = true;
                    phSuccess.Visible = false;
                }
            }
            
            Bind();
        }
        public static List<FavoriteSetSong> GenerateFavoriteVersionListByAlbum(string album)
        {
            var songService = new SongService(Ioc.GetInstance<ISongRepository>());
            var setSongService = new SetSongService(Ioc.GetInstance<ISetSongRepository>());
            var favoriteVersionService = new FavoriteVersionService(Ioc.GetInstance<IFavoriteVersionRepository>());
            var showService = new ShowService(Ioc.GetInstance<IShowRepository>());
            var setService = new SetService(Ioc.GetInstance<ISetRepository>());

            FavoriteVersionSongList songList = new FavoriteVersionSongList();

            foreach (var song in songService.GetSongsByAlbum(album))
            {
                
                var versions = favoriteVersionService.GetAllFavoriteVersions().Where(s => s.SongId == song.SongId).GroupBy(g => g.SetSongId).ToList();

                if (versions == null || versions.Count() <= 0)
                {
                    songList.AddFavoriteSongPair(null, SetSong.FromSong((Song)song), null);
                    continue;
                }

                if (versions.Count() == 1)
                {
                    var version = versions[0].First();

                    var setSong = setSongService.GetSetSong(version.SetSongId.Value);
                    var set = setService.GetSet(setSong.SetId.Value);
                    var show = showService.GetShow(set.ShowId.Value);

                    songList.AddFavoriteSongPair((FavoriteVersion)version, (SetSong)setSong, (Show)show);
                }
                else
                {
                    int count = 0;

                    Guid? setSongId = null;
                    FavoriteVersion fave = null;
                    SetSong setSong = null;
                    IShow show = null;

                    foreach (var version in versions)
                    {
                        if (version.Count() > count)
                        {
                            fave = (FavoriteVersion)version.First();
                            setSongId = version.First().SetSongId;
                        }
                    }

                    if(setSongId != null)
                    {
                        setSong = (SetSong)setSongService.GetSetSong(setSongId.Value);
                        var set = setService.GetSet(setSong.SetId.Value);
                        show = showService.GetShow(set.ShowId.Value);
                        
                    }

                    songList.AddFavoriteSongPair(fave, setSong ?? SetSong.FromSong((Song)song) , (Show)show);
                }
            }

            return songList.SongList;
        }