public System.IO.Stream GetMetadata(string UserId, string TypeId, string Id)
        {
            try
            {
                int type;
                int.TryParse(TypeId, out type);
                JMMUser user = KodiHelper.GetUser(UserId);
                switch ((JMMType)type)
                {
                case JMMType.Group:
                    return(GetItemsFromGroup(user.JMMUserID, Id));

                case JMMType.GroupFilter:
                    return(GetGroupsFromFilter(user.JMMUserID, Id));

                case JMMType.GroupUnsort:
                    return(GetUnsort(user.JMMUserID));

                case JMMType.Serie:
                    return(GetItemsFromSerie(user.JMMUserID, Id));

                case JMMType.File:
                    return(InternalGetFile(user.JMMUserID, Id));
                }
                return(new MemoryStream());
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.ToString(), ex);
                return(new MemoryStream());
            }
        }
        public System.IO.Stream GetFilters(string uid)
        {
            JMMUser user = KodiHelper.GetUser(uid);

            if (user == null)
            {
                return(new MemoryStream());
            }
            int        userid = user.JMMUserID;
            KodiObject ret    = new KodiObject(KodiHelper.NewMediaContainer("Anime", false));

            if (!ret.Init())
            {
                return(new MemoryStream());
            }
            List <Video> dirs = new List <Video>();

            try
            {
                using (var session = JMMService.SessionFactory.OpenSession())
                {
                    GroupFilterRepository            repGF  = new GroupFilterRepository();
                    List <GroupFilter>               allGfs = repGF.GetAll(session);
                    Dictionary <int, HashSet <int> > gstats = StatsCache.Instance.StatUserGroupFilter[userid];
                    foreach (GroupFilter gg in allGfs.ToArray())
                    {
                        if ((!StatsCache.Instance.StatUserGroupFilter.ContainsKey(userid)) ||
                            (!StatsCache.Instance.StatUserGroupFilter[userid].ContainsKey(gg.GroupFilterID)))
                        {
                            allGfs.Remove(gg);
                        }
                    }


                    AnimeGroupRepository repGroups = new AnimeGroupRepository();
                    allGfs.Insert(0, new GroupFilter()
                    {
                        GroupFilterName = "All", GroupFilterID = -999
                    });
                    foreach (GroupFilter gg in allGfs)
                    {
                        Random    rnd = new Random(123456789);
                        Directory pp  = new Directory();
                        pp.Key = KodiHelper.ServerUrl(int.Parse(ServerSettings.JMMServerPort),
                                                      MainWindow.PathAddressKodi + "/GetMetadata/" + userid + "/" +
                                                      (int)JMMType.GroupFilter + "/" + gg.GroupFilterID);
                        pp.PrimaryExtraKey = pp.Key;
                        pp.Title           = gg.GroupFilterName;
                        HashSet <int> groups;
                        groups = gg.GroupFilterID == -999 ? new HashSet <int>(repGroups.GetAllTopLevelGroups(session).Select(a => a.AnimeGroupID)) : gstats[gg.GroupFilterID];
                        if (groups.Count != 0)
                        {
                            bool repeat;
                            int  nn = 0;
                            pp.LeafCount       = groups.Count.ToString();
                            pp.ViewedLeafCount = "0";
                            do
                            {
                                repeat = true;
                                int                grp  = groups.ElementAt(rnd.Next(groups.Count));
                                AnimeGroup         ag   = repGroups.GetByID(grp);
                                List <AnimeSeries> sers = ag.GetSeries(session);
                                if (sers.Count > 0)
                                {
                                    AnimeSeries ser  = sers[rnd.Next(sers.Count)];
                                    AniDB_Anime anim = ser.GetAnime(session);
                                    if (anim != null)
                                    {
                                        ImageDetails poster = anim.GetDefaultPosterDetailsNoBlanks(session);
                                        ImageDetails fanart = anim.GetDefaultFanartDetailsNoBlanks(session);
                                        if (poster != null)
                                        {
                                            pp.Thumb = poster.GenPoster();
                                        }
                                        if (fanart != null)
                                        {
                                            pp.Art = fanart.GenArt();
                                        }
                                        if (poster != null)
                                        {
                                            repeat = false;
                                        }
                                    }
                                }
                                nn++;
                                if ((repeat) && (nn == 15))
                                {
                                    repeat = false;
                                }
                            } while (repeat);
                            dirs.Add(pp);
                        }
                    }
                    VideoLocalRepository repVids = new VideoLocalRepository();
                    List <VideoLocal>    vids    = repVids.GetVideosWithoutEpisode();
                    if (vids.Count > 0)
                    {
                        JMMContracts.KodiContracts.Directory pp = new JMMContracts.KodiContracts.Directory();
                        pp.Key = pp.PrimaryExtraKey = KodiHelper.ServerUrl(int.Parse(ServerSettings.JMMServerPort),
                                                                           MainWindow.PathAddressKodi + "/GetMetadata/0/" + (int)JMMType.GroupUnsort + "/0");
                        pp.Title = "Unsort";
                        pp.Thumb = KodiHelper.ServerUrl(int.Parse(ServerSettings.JMMServerPort),
                                                        MainWindow.PathAddressKodi + "/GetSupportImage/plex_unsort.png");
                        pp.LeafCount       = vids.Count.ToString();
                        pp.ViewedLeafCount = "0";
                        dirs.Add(pp);
                    }
                    dirs = dirs.OrderBy(a => a.Title).ToList();
                }
                ret.Childrens = dirs;
                return(ret.GetStream());
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.ToString(), ex);
                return(new MemoryStream());
            }
        }
        public System.IO.Stream Search(string UserId, string limit, string query, bool searchTag)
        {
            KodiObject ret = new KodiObject(KodiHelper.NewMediaContainer("Search", false));

            ret.MediaContainer.Title2 = "Search Results for '" + query + "'...";
            AniDB_AnimeRepository repAnime  = new AniDB_AnimeRepository();
            AnimeSeriesRepository repSeries = new AnimeSeriesRepository();

            int lim;

            if (!int.TryParse(limit, out lim))
            {
                lim = 100;
            }
            JMMUser user = KodiHelper.GetUser(UserId);

            if (user == null)
            {
                return(new MemoryStream());
            }
            List <Video>       ls  = new List <Video>();
            int                cnt = 0;
            List <AniDB_Anime> animes;

            if (searchTag)
            {
                animes = repAnime.SearchByTag(query);
            }
            else
            {
                animes = repAnime.SearchByName(query);
            }
            foreach (AniDB_Anime anidb_anime in animes)
            {
                if (!user.AllowedAnime(anidb_anime))
                {
                    continue;
                }
                AnimeSeries ser = repSeries.GetByAnimeID(anidb_anime.AnimeID);
                if (ser != null)
                {
                    Contract_AnimeSeries cserie = ser.ToContract(ser.GetUserRecord(user.JMMUserID), true);
                    Video v = KodiHelper.FromSerieWithPossibleReplacement(cserie, ser, user.JMMUserID);

                    //proper naming
                    v.OriginalTitle = "";
                    foreach (AniDB_Anime_Title title in anidb_anime.GetTitles())
                    {
                        if (title.TitleType == "official" || title.TitleType == "main")
                        {
                            v.OriginalTitle += "{" + title.TitleType + ":" + title.Language + "}" + title.Title + "|";
                        }
                    }
                    v.OriginalTitle = v.OriginalTitle.Substring(0, v.OriginalTitle.Length - 1);
                    //proper naming end

                    //experiment
                    Characters c = new Characters();
                    c.CharactersList = new List <Character>();
                    c.CharactersList = GetCharactersFromAniDB(anidb_anime);
                    v.CharactersList = new List <Characters>();
                    v.CharactersList.Add(c);
                    //experiment END

                    switch (anidb_anime.AnimeTypeEnum)
                    {
                    case enAnimeType.Movie:
                        v.SourceTitle = "Anime Movies";
                        break;

                    case enAnimeType.OVA:
                        v.SourceTitle = "Anime Ovas";
                        break;

                    case enAnimeType.Other:
                        v.SourceTitle = "Anime Others";
                        break;

                    case enAnimeType.TVSeries:
                        v.SourceTitle = "Anime Series";
                        break;

                    case enAnimeType.TVSpecial:
                        v.SourceTitle = "Anime Specials";
                        break;

                    case enAnimeType.Web:
                        v.SourceTitle = "Anime Web Clips";
                        break;
                    }

                    ls.Add(v);
                    cnt++;
                    if (cnt == lim)
                    {
                        break;
                    }
                }
            }
            ret.MediaContainer.Childrens = ls;
            return(ret.GetStream());
        }
        public System.IO.Stream GetFile(string Id)
        {
            JMMUser user = KodiHelper.GetUser("0");

            return(InternalGetFile(user.JMMUserID, Id));
        }