private MediaContainer FakeParentForIOSThumbnail(IProvider prov, string base64)
 {
     BaseObject ret = new BaseObject(prov.NewMediaContainer(MediaContainerTypes.None, null, false, true, null));
     if (!ret.Init())
         return new MediaContainer();
     string[] urls = Helper.Base64DecodeUrl(base64).Split('|');
     string thumb = Helper.ReplaceSchemeHost(urls[0]);
     string art = Helper.ReplaceSchemeHost(urls[1]);
     Directory v = new Directory()
     {
         Thumb = thumb,
         ParentThumb = thumb,
         GrandparentThumb = thumb,
         Art = art,
         ParentArt = art,
         GrandparentArt = art
     };
     ret.MediaContainer.Thumb = ret.MediaContainer.ParentThumb = ret.MediaContainer.GrandparentThumb = thumb;
     ret.MediaContainer.Art = ret.MediaContainer.ParentArt = ret.MediaContainer.GrandparentArt = art;
     List<Video> vids = new List<Video>();
     vids.Add(v);
     ret.Childrens = vids;
     return ret.GetStream(prov);
 }
        private MediaContainer GetGroupsOrSubFiltersFromFilter(IProvider prov, int userid, string GroupFilterId,
            BreadCrumbs info, bool nocast)
        {
            //List<Joint> retGroups = new List<Joint>();
            try
            {
                int groupFilterID;
                int.TryParse(GroupFilterId, out groupFilterID);
                using (var session = DatabaseFactory.SessionFactory.OpenSession())
                {
                    List<Video> retGroups = new List<Video>();
                    if (groupFilterID == -1)
                        return new MediaContainer() {ErrorString = "Invalid Group Filter"};
                    DateTime start = DateTime.Now;

                    GroupFilter gf;
                    gf = RepoFactory.GroupFilter.GetByID(groupFilterID);
                    if (gf == null) return new MediaContainer() { ErrorString = "Invalid Group Filter" };

                    BaseObject ret =
                        new BaseObject(prov.NewMediaContainer(MediaContainerTypes.Show, gf.GroupFilterName, false, true,
                            info));
                    if (!ret.Init())
                        return new MediaContainer();
                    List<GroupFilter> allGfs =
                    RepoFactory.GroupFilter.GetByParentID(groupFilterID).Where(a => a.InvisibleInClients == 0 &&
                    (
                        (a.GroupsIds.ContainsKey(userid) && a.GroupsIds[userid].Count > 0)
                        || (a.FilterType & (int)GroupFilterType.Directory) == (int)GroupFilterType.Directory)
                    ).ToList();
                    List<Directory> dirs = new List<Directory>();
                    foreach (GroupFilter gg in allGfs)
                    {
                        Directory pp = Helper.DirectoryFromFilter(prov, gg, userid);
                        if (pp != null)
                            dirs.Add(prov, pp, info);
                    }
                    if (dirs.Count > 0)
                    {
                        ret.Childrens = dirs.OrderBy(a => a.Title).Cast<Video>().ToList();
                        return ret.GetStream(prov);
                    }
                    Dictionary<Contract_AnimeGroup, Video> order = new Dictionary<Contract_AnimeGroup, Video>();
                    if (gf.GroupsIds.ContainsKey(userid))
                    {
                        // NOTE: The ToList() in the below foreach is required to prevent enumerable was modified exception
                        foreach (AnimeGroup grp in gf.GroupsIds[userid].ToList().Select(a => RepoFactory.AnimeGroup.GetByID(a)).Where(a => a != null))
                        {
                            Video v = grp.GetPlexContract(userid)?.Clone<Directory>();
                            if (v != null)
                            {
                                if (v.Group == null)
                                    v.Group = grp.GetUserContract(userid);
                                v.GenerateKey(prov, userid);
                                v.Type = "show";
                                v.Art = Helper.GetRandomFanartFromVideo(v) ?? v.Art;
                                v.Banner = Helper.GetRandomBannerFromVideo(v) ?? v.Banner;
                                if (nocast) v.Roles = null;
                                order.Add(v.Group, v);
                                retGroups.Add(prov, v, info);
                                v.ParentThumb = v.GrandparentThumb = null;
                            }
                        }
                    }
                    ret.MediaContainer.RandomizeArt(retGroups);
                    IEnumerable<Contract_AnimeGroup> grps = retGroups.Select(a => a.Group);
                    grps = gf.SortCriteriaList.Count != 0 ? GroupFilterHelper.Sort(grps, gf) : grps.OrderBy(a => a.GroupName);
                    ret.Childrens = grps.Select(a => order[a]).ToList();
                    //FilterExtras(prov,ret.Childrens);
                    return ret.GetStream(prov);
                }
            }
            catch (Exception ex)
            {
                logger.Error( ex,ex.ToString());
                return new MediaContainer() { ErrorString = "System Error, see JMMServer logs for more information" };

            }
        }
        public MediaContainer GetFilters(IProvider prov, string uid)
        {
            int t = 0;
            int.TryParse(uid, out t);
            JMMUser user = t > 0 ? Helper.GetJMMUser(uid) : Helper.GetUser(uid);
            if (user == null)
                return new MediaContainer() { ErrorString = "User not found" };
            int userid = user.JMMUserID;

            BreadCrumbs info = prov.UseBreadCrumbs
                ? new BreadCrumbs { Key = prov.ConstructFiltersUrl(userid), Title = "Anime" }
                : null;
            BaseObject ret =
                new BaseObject(prov.NewMediaContainer(MediaContainerTypes.Show, "Anime", false, false, info));
            if (!ret.Init())
                return new MediaContainer(); //Normal OPTION VERB
            List<Video> dirs = new List<Video>();
            try
            {
                using (var session = DatabaseFactory.SessionFactory.OpenSession())
                {
                    List<GroupFilter> allGfs = RepoFactory.GroupFilter.GetTopLevel().Where(a => a.InvisibleInClients == 0 &&
                    (
                        (a.GroupsIds.ContainsKey(userid) && a.GroupsIds[userid].Count > 0)
                        || (a.FilterType & (int)GroupFilterType.Directory) == (int)GroupFilterType.Directory)
                    ).ToList();

                    foreach (GroupFilter gg in allGfs)
                    {
                        Directory pp = Helper.DirectoryFromFilter(prov, gg, userid);
                        if (pp != null)
                            dirs.Add(prov, pp, info);
                    }
                    List<VideoLocal> vids = RepoFactory.VideoLocal.GetVideosWithoutEpisode();
                    if (vids.Count > 0)
                    {
                        Directory pp = new Directory() { Type = "show" };
                        pp.Key = prov.ShortUrl(prov.ConstructUnsortUrl(userid));
                        pp.Title = "Unsort";
                        pp.AnimeType = JMMContracts.PlexAndKodi.AnimeTypes.AnimeUnsort.ToString();
                        pp.Thumb = Helper.ConstructSupportImageLink("plex_unsort.png");
                        pp.LeafCount = vids.Count.ToString();
                        pp.ViewedLeafCount = "0";
                        dirs.Add(prov, pp, info);
                    }
                    var playlists = RepoFactory.Playlist.GetAll();
                    if (playlists.Count > 0)
                    {
                        Directory pp = new Directory() { Type = "show" };
                        pp.Key = prov.ShortUrl(prov.ConstructPlaylistUrl(userid));
                        pp.Title = "Playlists";
                        pp.AnimeType = JMMContracts.PlexAndKodi.AnimeTypes.AnimePlaylist.ToString();
                        pp.Thumb = Helper.ConstructSupportImageLink("plex_playlists.png");
                        pp.LeafCount = playlists.Count.ToString();
                        pp.ViewedLeafCount = "0";
                        dirs.Add(prov, pp, info);
                    }
                    dirs = dirs.OrderBy(a => a.Title).ToList();
                }
                ret.MediaContainer.RandomizeArt(dirs);
                ret.Childrens = dirs;
                ret.MediaContainer.Size = (int.Parse(ret.MediaContainer.Size) + prov.AddExtraItemForSearchButtonInGroupFilters).ToString();
                return ret.GetStream(prov);
            }
            catch (Exception ex)
            {
                logger.Error( ex,ex.ToString());
                return new MediaContainer() { ErrorString = "System Error, see JMMServer logs for more information" };
            }
        }
        //private void FilterExtras(IProvider provider, List<Video> videos)
        //{
        //    //foreach (Video v in videos)
        //    //{
        //    //    if (!provider.EnableAnimeTitlesInLists)
        //    //        v.Titles = null;
        //    //    if (!provider.EnableGenresInLists)
        //    //        v.Genres = null;
        //    //    if (!provider.EnableRolesInLists)
        //    //        v.Roles = null;
        //    //}
        //}
        public MediaContainer GetItemsFromSerie(IProvider prov, int userid, string SerieId, BreadCrumbs info, bool nocast)
        {
            BaseObject ret = null;
            enEpisodeType? eptype = null;
            int serieID;
            if (SerieId.Contains("_"))
            {
                int ept;
                string[] ndata = SerieId.Split('_');
                if (!int.TryParse(ndata[0], out ept))
                    return new MediaContainer() {ErrorString = "Invalid Serie Id"};
                eptype = (enEpisodeType) ept;
                if (!int.TryParse(ndata[1], out serieID))
                    return new MediaContainer() { ErrorString = "Invalid Serie Id" };
            }
            else
            {
                if (!int.TryParse(SerieId, out serieID))
                    return new MediaContainer() { ErrorString = "Invalid Serie Id" };
            }

            using (var session = DatabaseFactory.SessionFactory.OpenSession())
            {
                if (serieID == -1)
                    return new MediaContainer() { ErrorString = "Invalid Serie Id" };
                ISessionWrapper sessionWrapper = session.Wrap();
                AnimeSeries ser = RepoFactory.AnimeSeries.GetByID(serieID);
                if (ser == null)
                    return new MediaContainer() {ErrorString = "Invalid Series"};
                Contract_AnimeSeries cseries = ser.GetUserContract(userid);
                if (cseries == null)
                    return new MediaContainer() {ErrorString = "Invalid Series, Contract Not Found"};
                Video nv = ser.GetPlexContract(userid);

                Dictionary<AnimeEpisode, Contract_AnimeEpisode> episodes = ser.GetAnimeEpisodes()
                    .ToDictionary(a => a, a => a.GetUserContract(userid));
                episodes = episodes.Where(a => a.Value == null || a.Value.LocalFileCount > 0)
                    .ToDictionary(a => a.Key, a => a.Value);
                if (eptype.HasValue)
                {
                    ret =
                        new BaseObject(prov.NewMediaContainer(MediaContainerTypes.Episode, ser.GetSeriesName(), true,
                            true, info));
                    if (!ret.Init())
                        return new MediaContainer();
                    ret.MediaContainer.Art = cseries.AniDBAnime?.AniDBAnime?.DefaultImageFanart.GenArt();
                    ret.MediaContainer.LeafCount =
                        (cseries.WatchedEpisodeCount + cseries.UnwatchedEpisodeCount).ToString();
                    ret.MediaContainer.ViewedLeafCount = cseries.WatchedEpisodeCount.ToString();
                    episodes = episodes.Where(a => a.Key.EpisodeTypeEnum == eptype.Value)
                        .ToDictionary(a => a.Key, a => a.Value);
                }
                else
                {
                    ret = new BaseObject(prov.NewMediaContainer(MediaContainerTypes.Show, "Types", false, true, info));
                    if (!ret.Init())
                        return new MediaContainer();
                    ret.MediaContainer.Art = cseries.AniDBAnime?.AniDBAnime?.DefaultImageFanart.GenArt();
                    ret.MediaContainer.LeafCount =
                        (cseries.WatchedEpisodeCount + cseries.UnwatchedEpisodeCount).ToString();
                    ret.MediaContainer.ViewedLeafCount = cseries.WatchedEpisodeCount.ToString();
                    List<enEpisodeType> types = episodes.Keys.Select(a => a.EpisodeTypeEnum).Distinct().ToList();
                    if (types.Count > 1)
                    {
                        List<PlexEpisodeType> eps = new List<PlexEpisodeType>();
                        foreach (enEpisodeType ee in types)
                        {
                            PlexEpisodeType k2 = new PlexEpisodeType();
                            PlexEpisodeType.EpisodeTypeTranslated(k2, ee,
                                (AnimeTypes) cseries.AniDBAnime.AniDBAnime.AnimeType,
                                episodes.Count(a => a.Key.EpisodeTypeEnum == ee));
                            eps.Add(k2);
                        }
                        List<Video> dirs = new List<Video>();
                        //bool converttoseason = true;

                        foreach (PlexEpisodeType ee in  eps.OrderBy(a=>a.Name))
                        {
                            Video v = new Directory();
                            v.Art = nv.Art;
                            v.Title = ee.Name;
                            v.AnimeType = "AnimeType";
                            v.LeafCount = ee.Count.ToString();
                            v.ChildCount = v.LeafCount;
                            v.ViewedLeafCount = "0";
                            v.Key = prov.ShortUrl(prov.ConstructSerieIdUrl(userid, ee.Type + "_" + ser.AnimeSeriesID));
                            v.Thumb = Helper.ConstructSupportImageLink(ee.Image);
                            if ((ee.AnimeType == AnimeTypes.Movie) || (ee.AnimeType == AnimeTypes.OVA))
                            {
                                v = Helper.MayReplaceVideo(v, ser, cseries, userid, false, nv);
                            }
                            dirs.Add(prov, v, info, false, true);
                        }
                        ret.Childrens = dirs;
                        return ret.GetStream(prov);
                    }
                }
                List<Video> vids = new List<Video>();

                if ((eptype.HasValue) && (info!=null))
                    info.ParentKey = info.GrandParentKey;
                bool hasRoles = false;
                foreach (KeyValuePair<AnimeEpisode, Contract_AnimeEpisode> ep in episodes)
                {
                    try
                    {
                        Video v = Helper.VideoFromAnimeEpisode(prov, cseries.CrossRefAniDBTvDBV2, ep, userid);
                        if (v!=null && v.Medias != null && v.Medias.Count > 0)
                        {
                            if (nocast && !hasRoles) hasRoles = true;
                            Helper.AddInformationFromMasterSeries(v, cseries, nv, hasRoles);
                            v.Type = "episode";
                            vids.Add(prov, v, info);
                            if (prov.ConstructFakeIosParent)
                                v.GrandparentKey =
                                    prov.Proxyfy(prov.ConstructFakeIosThumb(userid, v.ParentThumb,
                                        v.Art ?? v.ParentArt ?? v.GrandparentArt));
                            v.ParentKey = null;
                            if (!hasRoles) hasRoles = true;
                        }
                    }
                    catch (Exception e)
                    {
                        //Fast fix if file do not exist, and still is in db. (Xml Serialization of video info will fail on null)
                    }
                }
                ret.Childrens = vids.OrderBy(a => int.Parse(a.EpisodeNumber)).ToList();
                //FilterExtras(prov,ret.Childrens);
                return ret.GetStream(prov);
            }
        }
 private MediaContainer GetUnsort(IProvider prov, int userid, BreadCrumbs info)
 {
     BaseObject ret =
         new BaseObject(prov.NewMediaContainer(MediaContainerTypes.Video, "Unsort", true, true, info));
     if (!ret.Init())
         return new MediaContainer();
     List<Video> dirs = new List<Video>();
     List<VideoLocal> vids = RepoFactory.VideoLocal.GetVideosWithoutEpisode();
     foreach (VideoLocal v in vids.OrderByDescending(a => a.DateTimeCreated))
     {
         try
         {
             Video m = Helper.VideoFromVideoLocal(prov, v, userid);
             dirs.Add(prov, m, info);
             m.Thumb = Helper.ConstructSupportImageLink("plex_404.png");
             m.ParentThumb = Helper.ConstructSupportImageLink("plex_unsort.png");
             m.ParentKey = null;
             if (prov.ConstructFakeIosParent)
                 m.GrandparentKey =
                     prov.Proxyfy(prov.ConstructFakeIosThumb(userid, m.ParentThumb,
                         m.Art ?? m.ParentArt ?? m.GrandparentArt));
         }
         catch (Exception e)
         {
             //Fast fix if file do not exist, and still is in db. (Xml Serialization of video info will fail on null)
         }
     }
     ret.Childrens = dirs;
     return ret.GetStream(prov);
 }
        public MediaContainer GetItemsFromGroup(IProvider prov, int userid, string GroupId, BreadCrumbs info, bool nocast, int? filterID)
        {
            int groupID;
            int.TryParse(GroupId, out groupID);
            if (groupID == -1)
                return new MediaContainer() { ErrorString = "Invalid Group Id" };

            List<Video> retGroups = new List<Video>();
            AnimeGroup grp = RepoFactory.AnimeGroup.GetByID(groupID);

            if (grp == null)
                return new MediaContainer { ErrorString = "Invalid Group" };

            BaseObject ret =
                new BaseObject(prov.NewMediaContainer(MediaContainerTypes.Show, grp.GroupName, false, true, info));
            if (!ret.Init())
                return new MediaContainer();

            Contract_AnimeGroup basegrp = grp?.GetUserContract(userid);
            if (basegrp != null)
            {
                List<AnimeSeries> seriesList = grp.GetSeries();
                if (filterID.HasValue)
                {
                    GroupFilter filter = RepoFactory.GroupFilter.GetByID(filterID.Value);
                    if (filter != null)
                    {
                        if (filter.ApplyToSeries > 0)
                        {
                            if(filter.SeriesIds.ContainsKey(userid))
                                seriesList =
                                    seriesList.Where(a => filter.SeriesIds[userid].Contains(a.AnimeSeriesID)).ToList();
                        }
                    }
                }
                foreach (AnimeGroup grpChild in grp.GetChildGroups())
                {
                    var v = grpChild.GetPlexContract(userid);
                    if (v != null)
                    {
                        v.Type = "show";
                        v.GenerateKey(prov, userid);

                        v.Art = Helper.GetRandomFanartFromVideo(v) ?? v.Art;
                        v.Banner = Helper.GetRandomBannerFromVideo(v) ?? v.Banner;

                        if (nocast) v.Roles = null;
                        retGroups.Add(prov, v, info);
                        v.ParentThumb = v.GrandparentThumb = null;
                    }
                }
                foreach (AnimeSeries ser in seriesList)
                {
                    var v = ser.GetPlexContract(userid)?.Clone<Directory>();
                    if (v != null)
                    {
                        v.AirDate = ser.AirDate;
                        v.Group = basegrp;
                        v.Type = "show";
                        v.GenerateKey(prov, userid);
                        v.Art = Helper.GetRandomFanartFromVideo(v) ?? v.Art;
                        v.Banner = Helper.GetRandomBannerFromVideo(v) ?? v.Banner;
                        if (nocast) v.Roles = null;
                        retGroups.Add(prov, v, info);
                        v.ParentThumb = v.GrandparentThumb = null;
                    }
                }
            }
            ret.MediaContainer.RandomizeArt(retGroups);
            ret.Childrens = Helper.ConvertToDirectory(retGroups.OrderBy(a => a.AirDate).ToList());
            //FilterExtras(prov,ret.Childrens);
            return ret.GetStream(prov);
        }
        private MediaContainer GetItemsFromPlaylist(IProvider prov, int userid, string id, BreadCrumbs info)
        {
            var PlaylistID = -1;
            int.TryParse(id, out PlaylistID);

            if (PlaylistID == 0)
            {
                using (var session = DatabaseFactory.SessionFactory.OpenSession())
                {
                    var ret = new BaseObject(prov.NewMediaContainer(MediaContainerTypes.Show, "Playlists", true, true, info));
                    if (!ret.Init())
                        return new MediaContainer(); //Normal
                    var retPlaylists = new List<Video>();
                    var playlists = RepoFactory.Playlist.GetAll();
                    var sessionWrapper = session.Wrap();

                    foreach (var playlist in playlists)
                    {
                        var dir = new Directory();
                        dir.Key = prov.ShortUrl(prov.ConstructPlaylistIdUrl(userid, playlist.PlaylistID));
                        dir.Title = playlist.PlaylistName;
                        dir.Id = playlist.PlaylistID.ToString();
                        dir.AnimeType = JMMContracts.PlexAndKodi.AnimeTypes.AnimePlaylist.ToString();
                        var episodeID = -1;
                        if (int.TryParse(playlist.PlaylistItems.Split('|')[0].Split(';')[1], out episodeID))
                        {
                            var anime = RepoFactory.AnimeEpisode.GetByID(episodeID).GetAnimeSeries(sessionWrapper).GetAnime();
                            dir.Thumb = anime?.GetDefaultPosterDetailsNoBlanks(sessionWrapper)?.GenPoster();
                            dir.Art = anime?.GetDefaultFanartDetailsNoBlanks(sessionWrapper)?.GenArt();
                            dir.Banner = anime?.GetDefaultWideBannerDetailsNoBlanks(sessionWrapper)?.GenArt();
                        }
                        else
                        {
                            dir.Thumb = Helper.ConstructSupportImageLink("plex_404V.png");
                        }
                        dir.LeafCount = playlist.PlaylistItems.Split('|').Count().ToString();
                        dir.ViewedLeafCount = "0";
                        retPlaylists.Add(prov, dir, info);
                    }
                    retPlaylists = retPlaylists.OrderBy(a => a.Title).ToList();
                    ret.Childrens = retPlaylists;
                    return ret.GetStream(prov);
                }
            }
            if (PlaylistID > 0)
            {
                var playlist = RepoFactory.Playlist.GetByID(PlaylistID);
                var playlistItems = playlist.PlaylistItems.Split('|');
                var vids = new List<Video>();
                var ret =
                    new BaseObject(prov.NewMediaContainer(MediaContainerTypes.Episode, playlist.PlaylistName, true, true,
                        info));
                if (!ret.Init())
                    return new MediaContainer(); //Normal
                foreach (var item in playlistItems)
                {
                    try
                    {
                        var episodeID = -1;
                        int.TryParse(item.Split(';')[1], out episodeID);
                        if (episodeID < 0) return new MediaContainer() { ErrorString = "Invalid Episode ID" };
                        AnimeEpisode e = RepoFactory.AnimeEpisode.GetByID(episodeID);
                        if (e == null)
                            return new MediaContainer() { ErrorString = "Invalid Episode" };
                        KeyValuePair<AnimeEpisode, Contract_AnimeEpisode> ep =
                            new KeyValuePair<AnimeEpisode, Contract_AnimeEpisode>(e,
                                e.GetUserContract(userid));
                        if (ep.Value != null && ep.Value.LocalFileCount == 0)
                            continue;
                        AnimeSeries ser = RepoFactory.AnimeSeries.GetByID(ep.Key.AnimeSeriesID);
                        if (ser == null)
                            return new MediaContainer() { ErrorString = "Invalid Series" };
                        Contract_AnimeSeries con = ser.GetUserContract(userid);
                        if (con == null)
                            return new MediaContainer() { ErrorString = "Invalid Series, Contract not found" };
                        Video v = Helper.VideoFromAnimeEpisode(prov, con.CrossRefAniDBTvDBV2, ep, userid);
                        if (v != null && v.Medias != null && v.Medias.Count > 0)
                        {
                            Helper.AddInformationFromMasterSeries(v, con, ser.GetPlexContract(userid));
                            v.Type = "episode";
                            vids.Add(prov, v, info);
                            if (prov.ConstructFakeIosParent)
                                v.GrandparentKey =
                                    prov.Proxyfy(prov.ConstructFakeIosThumb(userid, v.ParentThumb,
                                        v.Art ?? v.ParentArt ?? v.GrandparentArt));
                            v.ParentKey = null;
                        }
                    }
                    catch (Exception e)
                    {
                        //Fast fix if file do not exist, and still is in db. (Xml Serialization of video info will fail on null)
                    }
                }
                ret.MediaContainer.RandomizeArt(vids);
                ret.Childrens = vids;
                return ret.GetStream(prov);
            }
            return new MediaContainer() { ErrorString = "Invalid Playlist" };
        }