public void RefreshEpisodes()
        {
            allEpisodes = new List <VM_AnimeEpisode_User>();

            try
            {
                DateTime start = DateTime.Now;
                List <VM_AnimeEpisode_User> eps = VM_ShokoServer.Instance.ShokoServices.GetEpisodesForSeries(AnimeSeriesID,
                                                                                                             VM_ShokoServer.Instance.CurrentUser.JMMUserID).CastList <VM_AnimeEpisode_User>();
                TimeSpan ts = DateTime.Now - start;
                logger.Info("Got episode data from service: {0} in {1} ms", AniDBAnime.AniDBAnime.FormattedTitle, ts.TotalMilliseconds);
                start = DateTime.Now;

                VM_TvDBSummary tvSummary = AniDBAnime.AniDBAnime.TvSummary;
                // Normal episodes
                foreach (VM_AnimeEpisode_User epvm in eps)
                {
                    epvm.SetTvDBInfo(tvSummary);
                }

                ts = DateTime.Now - start;
                logger.Info("Got episode contracts: {0} in {1} ms", AniDBAnime.AniDBAnime.FormattedTitle, ts.TotalMilliseconds);

                start       = DateTime.Now;
                allEpisodes = eps.OrderBy(a => a.EpisodeNumber).ToList();
                ts          = DateTime.Now - start;
                logger.Info("Sorted episode contracts: {0} in {1} ms", AniDBAnime.AniDBAnime.FormattedTitle, ts.TotalMilliseconds);
            }
            catch (Exception ex)
            {
                Utils.ShowErrorMessage(ex);
            }
        }
        public void SetTvDBInfo()
        {
            //logger.Trace("SetTvDBInfo: RefreshAnime start: {0} - {1}", this.AnimeSeries.SeriesName, this.EpisodeNumberAndName);
            RefreshAnime();

            VM_TvDBSummary tvSummary = AniDB_Anime.TvSummary;

            SetTvDBInfo(tvSummary);
        }
        public void SetTvDBInfo(VM_TvDBSummary tvSummary)
        {
            TvDBLinkExists  = false;
            TvDBLinkMissing = true;

            #region episode override
            // check if this episode has a direct tvdb over-ride
            if (tvSummary.DictTvDBCrossRefEpisodes.ContainsKey(AniDB_EpisodeID))
            {
                foreach (VM_TvDB_Episode tvep in tvSummary.DictTvDBEpisodes.Values)
                {
                    if (tvSummary.DictTvDBCrossRefEpisodes[AniDB_EpisodeID] == tvep.Id)
                    {
                        EpisodeOverviewLoading = string.IsNullOrEmpty(tvep.Overview) ? "Episode Overview Not Available" : tvep.Overview;

                        if (string.IsNullOrEmpty(tvep.FullImagePathPlain) || !File.Exists(tvep.FullImagePath))
                        {
                            EpisodeImageLoading = @"/Images/EpisodeThumb_NotFound.png";
                            // if there is no proper image to show, we will hide it on the dashboard
                            ShowEpisodeImageInDashboard = false;
                        }
                        else
                        {
                            EpisodeImageLoading = tvep.FullImagePath;
                        }

                        if (VM_ShokoServer.Instance.EpisodeTitleSource == DataSourceType.TheTvDB && !string.IsNullOrEmpty(tvep.EpisodeName))
                        {
                            EpisodeName = tvep.EpisodeName;
                        }

                        TvDBLinkExists  = true;
                        TvDBLinkMissing = false;

                        return;
                    }
                }
            }
            #endregion

            //logger.Trace("SetTvDBInfo: normal episodes start");

            #region normal episodes
            // now do stuff to improve performance
            if (EpisodeTypeEnum == enEpisodeType.Episode)
            {
                if (tvSummary.CrossRefTvDBV2 != null && tvSummary.CrossRefTvDBV2.Count > 0)
                {
                    //logger.Trace("SetTvDBInfo: sorting TvDB cross refs: {0} records", tvSummary.CrossRefTvDBV2.Count);

                    // find the xref that is right
                    // relies on the xref's being sorted by season number and then episode number (desc)
                    List <VM_CrossRef_AniDB_TvDBV2> tvDBCrossRef = tvSummary.CrossRefTvDBV2.OrderByDescending(a => a.AniDBStartEpisodeNumber).ToList();

                    //logger.Trace("SetTvDBInfo: looking for starting points");

                    bool foundStartingPoint           = false;
                    VM_CrossRef_AniDB_TvDBV2 xrefBase = null;
                    foreach (VM_CrossRef_AniDB_TvDBV2 xrefTV in tvDBCrossRef)
                    {
                        if (xrefTV.AniDBStartEpisodeType != (int)enEpisodeType.Episode)
                        {
                            continue;
                        }
                        if (EpisodeNumber >= xrefTV.AniDBStartEpisodeNumber)
                        {
                            foundStartingPoint = true;
                            xrefBase           = xrefTV;
                            break;
                        }
                    }

                    //logger.Trace("SetTvDBInfo: looking for starting points - done");

                    // we have found the starting epiosde numbder from AniDB
                    // now let's check that the TvDB Season and Episode Number exist
                    if (foundStartingPoint)
                    {
                        //logger.Trace("SetTvDBInfo: creating dictionary");

                        Dictionary <int, int>             dictTvDBSeasons  = null;
                        Dictionary <int, VM_TvDB_Episode> dictTvDBEpisodes = null;
                        foreach (VM_TvDBDetails det in tvSummary.TvDetails.Values)
                        {
                            if (det.TvDBID == xrefBase.TvDBID)
                            {
                                dictTvDBSeasons  = det.DictTvDBSeasons;
                                dictTvDBEpisodes = det.DictTvDBEpisodes;
                                break;
                            }
                        }

                        //logger.Trace("SetTvDBInfo: creating dictionary - done");

                        if (dictTvDBSeasons != null && dictTvDBSeasons.ContainsKey(xrefBase.TvDBSeasonNumber))
                        {
                            int episodeNumber = dictTvDBSeasons[xrefBase.TvDBSeasonNumber] + (EpisodeNumber + xrefBase.TvDBStartEpisodeNumber - 2) - (xrefBase.AniDBStartEpisodeNumber - 1);
                            if (dictTvDBEpisodes.ContainsKey(episodeNumber))
                            {
                                //logger.Trace("SetTvDBInfo: loading episode overview");
                                VM_TvDB_Episode tvep = dictTvDBEpisodes[episodeNumber];
                                EpisodeOverviewLoading = string.IsNullOrEmpty(tvep.Overview) ? "Episode Overview Not Available" : tvep.Overview;

                                //logger.Trace("SetTvDBInfo: loading episode overview - done");

                                if (string.IsNullOrEmpty(tvep.FullImagePathPlain) || !File.Exists(tvep.FullImagePath))
                                {
                                    EpisodeImageLoading = @"/Images/EpisodeThumb_NotFound.png";
                                    // if there is no proper image to show, we will hide it on the dashboard
                                    ShowEpisodeImageInDashboard = false;
                                }
                                else
                                {
                                    EpisodeImageLoading = tvep.FullImagePath;
                                }

                                //logger.Trace("SetTvDBInfo: episode image - done");

                                if (VM_ShokoServer.Instance.EpisodeTitleSource == DataSourceType.TheTvDB && !string.IsNullOrEmpty(tvep.EpisodeName))
                                {
                                    EpisodeName = tvep.EpisodeName;
                                }
                            }
                        }
                    }
                }
            }
            #endregion

            //logger.Trace("SetTvDBInfo: normal episodes finish");

            #region special episodes
            if (EpisodeTypeEnum == enEpisodeType.Special)
            {
                // find the xref that is right
                // relies on the xref's being sorted by season number and then episode number (desc)
                List <VM_CrossRef_AniDB_TvDBV2> tvDBCrossRef = tvSummary.CrossRefTvDBV2?.OrderByDescending(a => a.AniDBStartEpisodeNumber).ToList() ?? new List <VM_CrossRef_AniDB_TvDBV2>();

                bool foundStartingPoint           = false;
                VM_CrossRef_AniDB_TvDBV2 xrefBase = null;
                foreach (VM_CrossRef_AniDB_TvDBV2 xrefTV in tvDBCrossRef)
                {
                    if (xrefTV.AniDBStartEpisodeType != (int)enEpisodeType.Special)
                    {
                        continue;
                    }
                    if (EpisodeNumber >= xrefTV.AniDBStartEpisodeNumber)
                    {
                        foundStartingPoint = true;
                        xrefBase           = xrefTV;
                        break;
                    }
                }

                if (tvSummary.CrossRefTvDBV2 != null && tvSummary.CrossRefTvDBV2.Count > 0)
                {
                    // we have found the starting epiosde numbder from AniDB
                    // now let's check that the TvDB Season and Episode Number exist
                    if (foundStartingPoint)
                    {
                        Dictionary <int, int>             dictTvDBSeasons  = null;
                        Dictionary <int, VM_TvDB_Episode> dictTvDBEpisodes = null;
                        foreach (VM_TvDBDetails det in tvSummary.TvDetails.Values)
                        {
                            if (det.TvDBID == xrefBase.TvDBID)
                            {
                                dictTvDBSeasons  = det.DictTvDBSeasons;
                                dictTvDBEpisodes = det.DictTvDBEpisodes;
                                break;
                            }
                        }

                        if (dictTvDBSeasons != null && dictTvDBSeasons.ContainsKey(xrefBase.TvDBSeasonNumber))
                        {
                            int episodeNumber = dictTvDBSeasons[xrefBase.TvDBSeasonNumber] + (EpisodeNumber + xrefBase.TvDBStartEpisodeNumber - 2) - (xrefBase.AniDBStartEpisodeNumber - 1);
                            if (dictTvDBEpisodes.ContainsKey(episodeNumber))
                            {
                                VM_TvDB_Episode tvep = dictTvDBEpisodes[episodeNumber];
                                EpisodeOverviewLoading = tvep.Overview;

                                if (string.IsNullOrEmpty(tvep.FullImagePathPlain) || !File.Exists(tvep.FullImagePath))
                                {
                                    EpisodeImageLoading = @"/Images/EpisodeThumb_NotFound.png";
                                    // if there is no proper image to show, we will hide it on the dashboard
                                    ShowEpisodeImageInDashboard = false;
                                }
                                else
                                {
                                    EpisodeImageLoading = tvep.FullImagePath;
                                }

                                if (VM_ShokoServer.Instance.EpisodeTitleSource == DataSourceType.TheTvDB && !string.IsNullOrEmpty(tvep.EpisodeName))
                                {
                                    EpisodeName = tvep.EpisodeName;
                                }
                            }
                        }
                    }
                }
            }
            #endregion
        }