public Response VoteAnime(IProvider prov, string userid, string objectid, string votevalue,
            string votetype)
        {
            Response rsp = new Response();
            rsp.Code = "400";
            rsp.Message = "Bad Request";
            try
            {
                int objid = 0;
                int usid = 0;
                int vt = 0;
                double vvalue = 0;
                if (!int.TryParse(objectid, out objid))
                    return rsp;
                if (!int.TryParse(userid, out usid))
                    return rsp;
                if (!int.TryParse(votetype, out vt))
                    return rsp;
                if (!double.TryParse(votevalue, NumberStyles.Any, CultureInfo.InvariantCulture, out vvalue))
                    return rsp;
                using (var session = DatabaseFactory.SessionFactory.OpenSession())
                {
                    ISessionWrapper sessionWrapper = session.Wrap();

                    if (vt == (int) enAniDBVoteType.Episode)
                    {
                        AnimeEpisode ep = RepoFactory.AnimeEpisode.GetByID(objid);
                        if (ep == null)
                        {
                            rsp.Code = "404";
                            rsp.Message = "Episode Not Found";
                            return rsp;
                        }
                        AniDB_Anime anime = ep.GetAnimeSeries().GetAnime();
                        if (anime == null)
                        {
                            rsp.Code = "404";
                            rsp.Message = "Anime Not Found";
                            return 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
                        List<AniDB_Vote> dbVotes = RepoFactory.AniDB_Vote.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;
                        RepoFactory.AniDB_Vote.Save(thisVote);
                        CommandRequest_VoteAnime cmdVote = new CommandRequest_VoteAnime(anime.AnimeID, vt,
                            Convert.ToDecimal(vvalue));
                        cmdVote.Save();
                    }

                    if (vt == (int)enAniDBVoteType.Anime)
                    {
                        AnimeSeries ser = RepoFactory.AnimeSeries.GetByID(objid);
                        AniDB_Anime anime = ser.GetAnime();
                        if (anime == null)
                        {
                            rsp.Code = "404";
                            rsp.Message = "Anime Not Found";
                            return 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
                        List<AniDB_Vote> dbVotes = RepoFactory.AniDB_Vote.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;
                        RepoFactory.AniDB_Vote.Save(thisVote);
                        CommandRequest_VoteAnime cmdVote = new CommandRequest_VoteAnime(anime.AnimeID, vt,
                            Convert.ToDecimal(vvalue));
                        cmdVote.Save();
                    }
                    rsp.Code = "200";
                    rsp.Message = null;
                }
            }
            catch (Exception ex)
            {
                rsp.Code = "500";
                rsp.Message = "Internal Error : " + ex;
                logger.Error( ex,ex.ToString());
            }
            return rsp;
        }
        public Response TraktScrobble(IProvider prov, string animeId, string type, string progress, string status)
        {
            Response rsp = new Response();
            rsp.Code = "400";
            rsp.Message = "Bad Request";
            try
            {
                int typeTrakt;
                int statusTrakt;
                Providers.TraktTV.ScrobblePlayingStatus statusTraktV2 = Providers.TraktTV.ScrobblePlayingStatus.Start;
                float progressTrakt;

                int.TryParse(status, out statusTrakt);

                switch (statusTrakt)
                {
                    case (int)Providers.TraktTV.ScrobblePlayingStatus.Start:
                        statusTraktV2 = Providers.TraktTV.ScrobblePlayingStatus.Start;
                        break;
                    case (int)Providers.TraktTV.ScrobblePlayingStatus.Pause:
                        statusTraktV2 = Providers.TraktTV.ScrobblePlayingStatus.Pause;
                        break;
                    case (int)Providers.TraktTV.ScrobblePlayingStatus.Stop:
                        statusTraktV2 = Providers.TraktTV.ScrobblePlayingStatus.Stop;
                        break;
                }

                float.TryParse(progress, out progressTrakt);
                progressTrakt = progressTrakt / 10;
                int.TryParse(type, out typeTrakt);
                switch (typeTrakt)
                {
                    // Movie
                    case (int)Providers.TraktTV.ScrobblePlayingType.movie:
                        rsp.Code = Providers.TraktTV.TraktTVHelper.Scrobble(
                            Providers.TraktTV.ScrobblePlayingType.movie, animeId,
                            statusTraktV2, progressTrakt).ToString();
                        rsp.Message = "Movie Scrobbled";
                        break;
                    // TV episode
                    case (int)Providers.TraktTV.ScrobblePlayingType.episode:
                        rsp.Code =
                            Providers.TraktTV.TraktTVHelper.Scrobble(Providers.TraktTV.ScrobblePlayingType.episode,
                                animeId,
                                statusTraktV2, progressTrakt).ToString();
                        rsp.Message = "Episode Scrobbled";
                        break;
                        //error
                }
            }
            catch (Exception ex)
            {
                rsp.Code = "500";
                rsp.Message = "Internal Error : " + ex;
                logger.Error( ex,ex.ToString());
            }
            return rsp;
        }
        public Response ToggleWatchedStatusOnEpisode(IProvider prov, string userid, string episodeid, string watchedstatus)
        {
            Response rsp = new Response();
            rsp.Code = "400";
            rsp.Message = "Bad Request";
            try
            {
                int aep = 0;
                int usid = 0;
                bool wstatus = false;
                if (!int.TryParse(episodeid, out aep))
                    return rsp;
                if (!int.TryParse(userid, out usid))
                    return rsp;
                wstatus = false;
                if (watchedstatus == "True" || watchedstatus == "true" || watchedstatus == "1")
                    wstatus = true;

                AnimeEpisode ep = RepoFactory.AnimeEpisode.GetByID(aep);
                if (ep == null)
                {
                    rsp.Code = "404";
                    rsp.Message = "Episode Not Found";
                    return rsp;
                }
                ep.ToggleWatchedStatus(wstatus, true, DateTime.Now, false, false, usid, true);
                ep.GetAnimeSeries().UpdateStats(true, false, true);
                rsp.Code = "200";
                rsp.Message = null;
            }
            catch (Exception ex)
            {
                rsp.Code = "500";
                rsp.Message = "Internal Error : " + ex;
                logger.Error( ex,ex.ToString());
            }
            return rsp;
        }
        public Response ToggleWatchedStatusOnSeries(IProvider prov, string userid, string seriesid,
			string watchedstatus)
        {
            //prov.AddResponseHeaders();

            Response rsp = new Response();
            rsp.Code = "400";
            rsp.Message = "Bad Request";
            try
            {
                int aep = 0;
                int usid = 0;
                bool wstatus = false;
                if (!int.TryParse(seriesid, out aep))
                    return rsp;
                if (!int.TryParse(userid, out usid))
                    return rsp;
                wstatus = false;
                if (watchedstatus == "True" || watchedstatus == "true" || watchedstatus == "1")
                    wstatus = true;

                AnimeSeries series = RepoFactory.AnimeSeries.GetByID(aep);
                if (series == null)
                {
                    rsp.Code = "404";
                    rsp.Message = "Episode Not Found";
                    return rsp;
                }

                List<AnimeEpisode> eps = series.GetAnimeEpisodes();
                foreach (AnimeEpisode ep in eps)
                {
                    if (ep.EpisodeTypeEnum == enEpisodeType.Credits) continue;
                    if (ep.EpisodeTypeEnum == enEpisodeType.Trailer) continue;

                    ep.ToggleWatchedStatus(wstatus, true, DateTime.Now, false, false, usid, true);
                }

                series.UpdateStats(true, false, true);
                rsp.Code = "200";
                rsp.Message = null;
            }
            catch (Exception ex)
            {
                rsp.Code = "500";
                rsp.Message = "Internal Error : " + ex;
                logger.Error( ex,ex.ToString());
            }
            return rsp;
        }
 public Response GetVersion()
 {
     Response rsp = new Response();
     try
     {
         rsp.Code = "200";
         rsp.Message = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString();
         return rsp;
     }
     catch (Exception e)
     {
         logger.Error( e,e.ToString());
         rsp.Code = "500";
         rsp.Message = "System Error, see JMMServer logs for more information";
     }
     return rsp;
 }