private MediaContainer GetFromFile(IProvider prov, int userid, string Id, BreadCrumbs info)
 {
     int id;
     if (!int.TryParse(Id, out id))
         return new MediaContainer() { ErrorString = "Invalid File Id" };
     VideoLocal vi = RepoFactory.VideoLocal.GetByID(id);
     BaseObject ret =
         new BaseObject(prov.NewMediaContainer(MediaContainerTypes.File,
             Path.GetFileNameWithoutExtension(vi.FileName ?? ""),
             true, false, info));
     Video v2 = Helper.VideoFromVideoLocal(prov, vi, userid);
     List<Video> dirs = new List<Video>();
     dirs.EppAdd(prov, v2, info, true);
     v2.Thumb = Helper.ConstructSupportImageLink("plex_404.png");
     v2.ParentThumb = Helper.ConstructSupportImageLink("plex_unsort.png");
     if (prov.ConstructFakeIosParent)
         v2.GrandparentKey =
             prov.Proxyfy(prov.ConstructFakeIosThumb(userid, v2.ParentThumb,
                 v2.Art ?? v2.ParentArt ?? v2.GrandparentArt));
     v2.ParentKey = null;
     if (prov.UseBreadCrumbs)
         v2.Key = prov.ShortUrl(ret.MediaContainer.Key);
     ret.MediaContainer.Childrens = dirs;
     return ret.GetStream(prov);
 }
        private MediaContainer GetFromEpisode(IProvider prov, int userid, string Id, BreadCrumbs info)
        {
            int id;
            if (!int.TryParse(Id, out id))
                return new MediaContainer() { ErrorString = "Invalid Episode Id" };
            BaseObject ret =
                new BaseObject(prov.NewMediaContainer(MediaContainerTypes.Episode, "Episode", true, true, info));
            using (var session = DatabaseFactory.SessionFactory.OpenSession())
            {
                List<Video> dirs = new List<Video>();
                ISessionWrapper sessionWrapper = session.Wrap();

                AnimeEpisode e = RepoFactory.AnimeEpisode.GetByID(id);
                if (e == null)
                    return new MediaContainer() { ErrorString = "Invalid Episode Id" };
                KeyValuePair<AnimeEpisode, Contract_AnimeEpisode> ep =
                    new KeyValuePair<AnimeEpisode, Contract_AnimeEpisode>(e,
                        e.GetUserContract(userid));
                if (ep.Value != null && ep.Value.LocalFileCount == 0)
                    return new MediaContainer() { ErrorString = "Episode do not have videolocals" };
                AniDB_Episode aep = ep.Key.AniDB_Episode;
                if (aep == null)
                    return new MediaContainer() { ErrorString = "Invalid Episode AniDB link not found" };
                AnimeSeries ser = RepoFactory.AnimeSeries.GetByID(ep.Key.AnimeSeriesID);
                if (ser == null)
                    return new MediaContainer() { ErrorString = "Invalid Serie" };
                AniDB_Anime anime = ser.GetAnime();
                Contract_AnimeSeries con = ser.GetUserContract(userid);
                if (con == null)
                    return new MediaContainer() { ErrorString = "Invalid Serie, Contract not found" };
                try
                {
                    Video v = Helper.VideoFromAnimeEpisode(prov, con.CrossRefAniDBTvDBV2, ep, userid);
                    if (v != null)
                    {
                        Video nv = ser.GetPlexContract(userid);
                        Helper.AddInformationFromMasterSeries(v, con, ser.GetPlexContract(userid), prov is KodiProvider);
                        if (v.Medias != null && v.Medias.Count > 0)
                        {
                            v.Type = "episode";
                            dirs.EppAdd(prov, v, info, true);
                            if (prov.ConstructFakeIosParent)
                                v.GrandparentKey =
                                    prov.Proxyfy(prov.ConstructFakeIosThumb(userid, v.ParentThumb,
                                        v.Art ?? v.ParentArt ?? v.GrandparentArt));
                            v.ParentKey = null;
                        }
                        if (prov.UseBreadCrumbs)
                            v.Key = prov.ShortUrl(ret.MediaContainer.Key);
                        ret.MediaContainer.Art = Helper.ReplaceSchemeHost(nv.Art ?? nv.ParentArt ?? nv.GrandparentArt);
                    }
                    ret.MediaContainer.Childrens = dirs;
                    return ret.GetStream(prov);
                }
                catch (Exception ex)
                {
                    //Fast fix if file do not exist, and still is in db. (Xml Serialization of video info will fail on null)
                }
            }
            return new MediaContainer() { ErrorString = "Episode Not Found" };
        }
        //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);
            }
        }
        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 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" };
        }