public void ToggleWatchedStatusOnEpisode(string userid, string episodeid, string watchedstatus)
        {
            try
            {
                int  aep     = 0;
                int  usid    = 0;
                bool wstatus = false;
                if (!int.TryParse(episodeid, out aep))
                {
                    return;
                }
                if (!int.TryParse(userid, out usid))
                {
                    return;
                }
                if (!bool.TryParse(watchedstatus, out wstatus))
                {
                    return;
                }

                AnimeEpisodeRepository repEps = new AnimeEpisodeRepository();
                AnimeEpisode           ep     = repEps.GetByID(aep);
                if (ep == null)
                {
                    return;
                }

                ep.ToggleWatchedStatus(wstatus, true, DateTime.Now, false, false, usid, true);
                ep.GetAnimeSeries().UpdateStats(true, false, true);
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.ToString(), ex);
            }
        }
        public override void ProcessCommand()
        {
            logger.Info("Processing CommandRequest_TraktShowScrobble: {0}", AnimeEpisodeID);

            try
            {
                if (!ServerSettings.WebCache_Trakt_Send || string.IsNullOrEmpty(ServerSettings.Trakt_Username))
                {
                    return;
                }

                AnimeEpisodeRepository repEpisodes = new AnimeEpisodeRepository();
                AnimeEpisode           ep          = repEpisodes.GetByID(AnimeEpisodeID);
                if (ep != null)
                {
                    // before scrobbling try to ensure the episode is in the users collection by sync'ing the series
                    //TraktTVHelper.SyncCollectionToTrakt_Series(ep.AnimeSeries);

                    TraktTVHelper.MarkEpisodeWatched(ep);
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error processing CommandRequest_TraktShowScrobble: {0} - {1}", AnimeEpisodeID, ex.ToString());
                return;
            }
        }
        public override void ProcessCommand()
        {
            logger.Info("Processing CommandRequest_TraktCollectionEpisode: {0}-{1}", AnimeEpisodeID, Action);

            try
            {
                logger.Info("CommandRequest_TraktCollectionEpisode - DEBUG01");
                if (!ServerSettings.Trakt_IsEnabled || string.IsNullOrEmpty(ServerSettings.Trakt_AuthToken)) return;
                logger.Info("CommandRequest_TraktCollectionEpisode - DEBUG02");

                AnimeEpisodeRepository repEpisodes = new AnimeEpisodeRepository();
                AnimeEpisode ep = repEpisodes.GetByID(AnimeEpisodeID);
                if (ep != null)
                {
                    logger.Info("CommandRequest_TraktCollectionEpisode - DEBUG03");
                    TraktSyncType syncType = TraktSyncType.CollectionAdd;
                    if (ActionEnum == TraktSyncAction.Remove) syncType = TraktSyncType.CollectionRemove;
                    TraktTVHelper.SyncEpisodeToTrakt(ep, syncType);
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error processing CommandRequest_TraktCollectionEpisode: {0} - {1} - {2}", AnimeEpisodeID,
                    Action,
                    ex.ToString());
                return;
            }
        }
        public override void ProcessCommand()
        {
            logger.Info("Processing CommandRequest_TraktShowEpisodeUnseen: {0}", AnimeEpisodeID);

            try
            {
                AnimeEpisodeRepository repEps = new AnimeEpisodeRepository();
                AnimeEpisode           ep     = repEps.GetByID(AnimeEpisodeID);
                if (ep == null)
                {
                    logger.Error("Could not find anime epiosde: {0}", AnimeEpisodeID);
                    return;
                }

                TraktTVHelper.SyncEpisodeToTrakt(ep, TraktSyncType.HistoryRemove);
            }
            catch (Exception ex)
            {
                logger.Error("Error processing CommandRequest_TraktShowEpisodeUnseen: {0} - {1}", AnimeEpisodeID, ex.ToString());
                return;
            }
        }
        public System.IO.Stream VoteAnime(string userid, string objectid, string votevalue, string votetype)
        {
            Respond rsp = new Respond();

            rsp.code = 500;

            int    objid  = 0;
            int    usid   = 0;
            int    vt     = 0;
            double vvalue = 0;

            if (!int.TryParse(objectid, out objid))
            {
                return(KodiHelper.GetStreamFromXmlObject(rsp));
            }
            if (!int.TryParse(userid, out usid))
            {
                return(KodiHelper.GetStreamFromXmlObject(rsp));
            }
            if (!int.TryParse(votetype, out vt))
            {
                return(KodiHelper.GetStreamFromXmlObject(rsp));
            }
            if (!double.TryParse(votevalue, NumberStyles.Any, CultureInfo.InvariantCulture, out vvalue))
            {
                return(KodiHelper.GetStreamFromXmlObject(rsp));
            }
            using (var session = JMMService.SessionFactory.OpenSession())
            {
                if (vt == (int)enAniDBVoteType.Episode)
                {
                    AnimeEpisodeRepository repEpisodes = new AnimeEpisodeRepository();
                    AnimeEpisode           ep          = repEpisodes.GetByID(session, objid);
                    AniDB_Anime            anime       = ep.GetAnimeSeries().GetAnime();
                    if (anime == null)
                    {
                        rsp.code = 404;
                        return(KodiHelper.GetStreamFromXmlObject(rsp));
                    }
                    string msg = string.Format("Voting for anime episode: {0} - Value: {1}", ep.AnimeEpisodeID, vvalue);
                    logger.Info(msg);

                    // lets save to the database and assume it will work
                    AniDB_VoteRepository repVotes = new AniDB_VoteRepository();
                    List <AniDB_Vote>    dbVotes  = repVotes.GetByEntity(ep.AnimeEpisodeID);
                    AniDB_Vote           thisVote = null;
                    foreach (AniDB_Vote dbVote in dbVotes)
                    {
                        if (dbVote.VoteType == (int)enAniDBVoteType.Episode)
                        {
                            thisVote = dbVote;
                        }
                    }

                    if (thisVote == null)
                    {
                        thisVote          = new AniDB_Vote();
                        thisVote.EntityID = ep.AnimeEpisodeID;
                    }
                    thisVote.VoteType = vt;

                    int iVoteValue = 0;
                    if (vvalue > 0)
                    {
                        iVoteValue = (int)(vvalue * 100);
                    }
                    else
                    {
                        iVoteValue = (int)vvalue;
                    }

                    msg = string.Format("Voting for anime episode Formatted: {0} - Value: {1}", ep.AnimeEpisodeID, iVoteValue);
                    logger.Info(msg);
                    thisVote.VoteValue = iVoteValue;
                    repVotes.Save(thisVote);
                    CommandRequest_VoteAnime cmdVote = new CommandRequest_VoteAnime(anime.AnimeID, vt, Convert.ToDecimal(vvalue));
                    cmdVote.Save();
                }

                if (vt == (int)enAniDBVoteType.Anime)
                {
                    AnimeSeriesRepository repSeries = new AnimeSeriesRepository();
                    AnimeSeries           ser       = repSeries.GetByID(session, objid);
                    AniDB_Anime           anime     = ser.GetAnime();
                    if (anime == null)
                    {
                        rsp.code = 404;
                        return(KodiHelper.GetStreamFromXmlObject(rsp));
                    }
                    string msg = string.Format("Voting for anime: {0} - Value: {1}", anime.AnimeID, vvalue);
                    logger.Info(msg);

                    // lets save to the database and assume it will work
                    AniDB_VoteRepository repVotes = new AniDB_VoteRepository();
                    List <AniDB_Vote>    dbVotes  = repVotes.GetByEntity(anime.AnimeID);
                    AniDB_Vote           thisVote = null;
                    foreach (AniDB_Vote dbVote in dbVotes)
                    {
                        // we can only have anime permanent or anime temp but not both
                        if (vt == (int)enAniDBVoteType.Anime || vt == (int)enAniDBVoteType.AnimeTemp)
                        {
                            if (dbVote.VoteType == (int)enAniDBVoteType.Anime ||
                                dbVote.VoteType == (int)enAniDBVoteType.AnimeTemp)
                            {
                                thisVote = dbVote;
                            }
                        }
                        else
                        {
                            thisVote = dbVote;
                        }
                    }

                    if (thisVote == null)
                    {
                        thisVote          = new AniDB_Vote();
                        thisVote.EntityID = anime.AnimeID;
                    }
                    thisVote.VoteType = vt;

                    int iVoteValue = 0;
                    if (vvalue > 0)
                    {
                        iVoteValue = (int)(vvalue * 100);
                    }
                    else
                    {
                        iVoteValue = (int)vvalue;
                    }

                    msg = string.Format("Voting for anime Formatted: {0} - Value: {1}", anime.AnimeID, iVoteValue);
                    logger.Info(msg);
                    thisVote.VoteValue = iVoteValue;
                    repVotes.Save(thisVote);
                    CommandRequest_VoteAnime cmdVote = new CommandRequest_VoteAnime(anime.AnimeID, vt, Convert.ToDecimal(vvalue));
                    cmdVote.Save();
                }
                rsp.code = 200;
                return(KodiHelper.GetStreamFromXmlObject(rsp));
            }
        }
        private System.IO.Stream GetItemsFromPlaylist(int userid, string id, HistoryInfo info)
        {
            var PlaylistID = -1;

            int.TryParse(id, out PlaylistID);
            var playlistRepository = new PlaylistRepository();
            var repo = new AnimeEpisodeRepository();

            if (PlaylistID == 0)
            {
                using (var session = JMMService.SessionFactory.OpenSession())
                {
                    var ret = new PlexObject(PlexHelper.NewMediaContainer(MediaContainerTypes.Show, info, false));
                    if (!ret.Init())
                    {
                        return(new MemoryStream());
                    }
                    var retPlaylists = new List <Video>();
                    var playlists    = playlistRepository.GetAll();
                    foreach (var playlist in playlists)
                    {
                        var dir = new Directory();
                        dir.Key   = PlexHelper.ConstructPlaylistIdUrl(userid, playlist.PlaylistID);
                        dir.Title = playlist.PlaylistName;
                        var episodeID = -1;
                        if (int.TryParse(playlist.PlaylistItems.Split('|')[0].Split(';')[1], out episodeID))
                        {
                            var anime = repo.GetByID(session, episodeID).GetAnimeSeries(session).GetAnime(session);
                            dir.Thumb = anime.GetDefaultPosterDetailsNoBlanks(session).GenPoster();
                            dir.Art   = anime.GetDefaultFanartDetailsNoBlanks(session).GenArt();
                        }
                        else
                        {
                            dir.Thumb = PlexHelper.ConstructSupportImageLink("plex_404V.png");
                        }
                        dir.LeafCount       = playlist.PlaylistItems.Split('|').Count().ToString();
                        dir.ViewedLeafCount = "0";
                        retPlaylists.Add(dir, info);
                    }
                    retPlaylists  = retPlaylists.OrderBy(a => a.Title).ToList();
                    ret.Childrens = retPlaylists;
                    return(ret.GetStream());
                }
            }
            if (PlaylistID > 0)
            {
                //iOS Hack, since it uses the previous thumb, as overlay image on the episodes
                bool iosHack = false;
                if (WebOperationContext.Current != null && WebOperationContext.Current.IncomingRequest.Headers.AllKeys.Contains("X-Plex-Product"))
                {
                    string kh = WebOperationContext.Current.IncomingRequest.Headers.Get("X-Plex-Product").ToUpper();
                    if (kh.Contains(" IOS"))
                    {
                        iosHack = true;
                    }
                }

                var playlist      = playlistRepository.GetByID(PlaylistID);
                var playlistItems = playlist.PlaylistItems.Split('|');
                var vids          = new List <Video>();
                var ret           = new PlexObject(PlexHelper.NewMediaContainer(MediaContainerTypes.Episode, info, true));
                if (!ret.Init())
                {
                    return(new MemoryStream());
                }
                using (var session = JMMService.SessionFactory.OpenSession())
                {
                    foreach (var item in playlistItems)
                    {
                        var episodeID = -1;
                        int.TryParse(item.Split(';')[1], out episodeID);
                        if (episodeID < 0)
                        {
                            return(new MemoryStream());
                        }
                        var ep     = repo.GetByID(session, episodeID);
                        var v      = new Video();
                        var locals = ep.GetVideoLocals(session);
                        if ((locals == null) || (locals.Count == 0))
                        {
                            continue;
                        }
                        var current = locals[0];
                        try
                        {
                            PlexHelper.PopulateVideo(v, current, JMMType.File, userid);
                            if (!string.IsNullOrEmpty(v.Duration))
                            {
                                vids.Add(v, info);
                                if (iosHack)
                                {
                                    v.Art   = v.Thumb;
                                    v.Thumb = ret.MediaContainer.ParentThumb;
                                }
                            }
                        }
                        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;
                    return(ret.GetStream());
                }
            }
            return(new MemoryStream());
        }