public TVSeason PopulateSeasonFromJson(string seasonData)
        {
            TVSeason mySeason = new TVSeason();
            JObject seasonJObject = JObject.Parse(seasonData);


            mySeason.AirDate = "" + seasonJObject["air_date"];
            mySeason.TVDBID = "" + seasonJObject["id"];
            mySeason.Overview = "" + seasonJObject["overview"];
            mySeason.PosterPath = "" + seasonJObject["poster_path"];
            mySeason.SeasonNumber = (int)seasonJObject["season_number"];

            JArray myEpisodeArray = (JArray)seasonJObject.SelectToken("episodes");
            mySeason.EpisodeCount = myEpisodeArray.Count;

            mySeason.Episodes = new List<TVEpisode>();
            foreach (JToken episodeJToken in myEpisodeArray)
            {
                TVEpisode myEpisode = new TVEpisode
                {
                    EpisodeNumber = (int)episodeJToken["episode_number"],
                    SeasonNumber = mySeason.SeasonNumber,
                    Name = "" + episodeJToken["name"],
                    AirDate = "" + episodeJToken["air_date"],
                    StillPath = "" + episodeJToken["still_path"],
                    Overview = "" + episodeJToken["overview"]
                };
                mySeason.Episodes.Add(myEpisode);
            }

            return mySeason;
        }
        public static DetailsShowEpisodeTabsFragment NewInstance(TVSeason season, int EpisodePosition)
        {
            DetailsShowEpisodeTabsFragment frag = new DetailsShowEpisodeTabsFragment();

            Bundle args = new Bundle();
            string seasonSerialized = JsonConvert.SerializeObject(season);
            args.PutString("seasonSerialized", seasonSerialized);
            args.PutInt("position", EpisodePosition);

            frag.Arguments = args;

            return frag;
        }
        private async Task PopulateSeasonsAndSetUpFragments()
        {
            if (!TMDBFileLoading.TMDBShowFileExists(Intent.GetStringExtra("TMDBID")))
            {
                string myURL = "https://api.themoviedb.org/3/tv/" + Intent.GetStringExtra("TMDBID") +
                               "?api_key=661b76973b90b91e0df214904015fd4d";
                var client = new HttpClient();
                var data = await client.GetStringAsync(myURL);
                JObject o = JObject.Parse(data);
                _myShow.Name = o["name"] + "";
                _myShow.Network = o["networks"][0]["name"] + "";
                _myShow.NumberOfSeasons = (int) o["number_of_seasons"];
                _myShow.TMDBID = o["id"] + "";
                //_myShow.FullJson = data;
                _myShow.PosterLink = o["poster_path"] + "";
                _myShow.Overview = o["overview"] + "";
                _myShow.Language = o["languages"][0] + "";
                //SupportActionBar._title = _myShow.Name;
                //await _myShow.PopulateSeasonJSONListFromWeb();
                //_myTextView.Text = _myShow.Name + " is on " + _myShow.Network;

                //SetUpListViewFragment();
                _myShow.Seasons = new List<TVSeason>();
                for (int i = 1; i <= _myShow.NumberOfSeasons; i++)
                {
                    TVSeason mySeason = new TVSeason();
                    string mySeasonURL = "https://api.themoviedb.org/3/tv/" + Intent.GetStringExtra("TMDBID") +
                                         "/season/" + i + "?api_key=661b76973b90b91e0df214904015fd4d";
                    var seasonClient = new HttpClient();
                    var seasonData = await seasonClient.GetStringAsync(mySeasonURL);
                    JObject seasonJObject = JObject.Parse(seasonData);


                    mySeason.AirDate = "" + seasonJObject["air_date"];
                    mySeason.TVDBID = "" + seasonJObject["id"];
                    mySeason.Overview = "" + seasonJObject["overview"];
                    mySeason.PosterPath = "" + seasonJObject["poster_path"];
                    mySeason.SeasonNumber = (int) seasonJObject["season_number"];

                    JArray myEpisodeArray = (JArray) seasonJObject.SelectToken("episodes");
                    mySeason.EpisodeCount = myEpisodeArray.Count;

                    mySeason.Episodes = new List<TVEpisode>();
                    foreach (JToken episodeJToken in myEpisodeArray)
                    {
                        TVEpisode myEpisode = new TVEpisode
                        {
                            EpisodeNumber = (int) episodeJToken["episode_number"],
                            SeasonNumber = i,
                            Name = "" + episodeJToken["name"],
                            AirDate = "" + episodeJToken["air_date"],
                            StillPath = "" + episodeJToken["still_path"],
                            Overview = "" + episodeJToken["overview"]
                        };
                        mySeason.Episodes.Add(myEpisode);
                    }
                    _myShow.Seasons.Add(mySeason);
                }

                string showToSave = JsonConvert.SerializeObject(_myShow);
                TMDBFileSaving.saveTMDBShowJSON(Intent.GetStringExtra("TMDBID"), showToSave);

            }
            else
            {
                Console.WriteLine("Show has been saved");
                string loadedShow =  TMDBFileLoading.loadTMDBShowJSONFromFile(Intent.GetStringExtra("TMDBID"));
                _myShow = JsonConvert.DeserializeObject<TVShow>(loadedShow);
            }

            

            //string serialized = JsonConvert.SerializeObject(_myShow);
            //Console.UpdateTrakkedShowList(serialized);
            //_myShow = new TVShow();
            //_myShow = JsonConvert.DeserializeObject<TVShow>(serialized);
            //Console.UpdateTrakkedShowList(JsonConvert.SerializeObject(_myShow));
            // Console.UpdateTrakkedShowList("Last Episode of First Season is titled " +
            //                  _myShow.SeasonList[0].TVEpisodes[_myShow.SeasonList[0].TVEpisodes.Count - 1].Name);
            SetUpTitle();

            SetUpBothFragments();

            GetUpdatedParseObject();

            //IEnumerable<TVEpisodeForParse> trakkedEpisodeList = await GetParseEpisodeObject();

            //UpdateShowEpisodesWithTrakkedEpisode(trakkedEpisodeList);
        }
 public void UpdateSeason(TVSeason season)
 {
     _season = season;
     _adapter = new DetailsShowEpisodeTabsAdatper(this.FragmentManager, _season);
     _pager.Adapter = _adapter;
     _tabLayout.SetupWithViewPager(_pager);
     _tabLayout.SetOnTabSelectedListener(this);
 }
 public void UpdateSeason(TVSeason season)
 {
     _season = season;
     NotifyDataSetChanged();
 }
 public DetailsShowEpisodeTabsAdatper(FragmentManager fm, TVSeason season) : base(fm)
 {
     _season = season;
     PopulateEpisodeList();
 }
 public void ChangeInSeasonListView(TVSeason season)
 {
     _season = season;
     _adapter.UpdateSeason(_season);
     _adapter.NotifyDataSetChanged();
 }
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            if (savedInstanceState != null)
            {
                _season = new TVSeason();
                _season = JsonConvert.DeserializeObject<TVSeason>(savedInstanceState.GetString("seasonSerialized"));
                _position = savedInstanceState.GetInt("position", 0);
            }


            var view = inflater.Inflate(Resource.Layout.details_show_episode_tab_view, container, false);
            var backgroundLinearLayout = view.FindViewById<LinearLayout>(Resource.Id.detailsShowEpisodeLinearLayout);
            _adapter = new DetailsShowEpisodeTabsAdatper(this.FragmentManager, _season);
            _pager = view.FindViewById<ViewPager>(Resource.Id.detailsShowEpisodeViewPager);
            _tabLayout = view.FindViewById<TabLayout>(Resource.Id.detaislShowEpisodeTabLayout);
            _pager.Adapter = _adapter;
            _tabLayout.SetupWithViewPager(_pager);

            var pageMargin = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 4, Resources.DisplayMetrics);
            _pager.PageMargin = pageMargin;
            _pager.CurrentItem = _position;
            _tabLayout.SetOnTabSelectedListener(this);


            //backgroundLinearLayout.SetBackgroundColor(Color.Red);

            return view;
        }
 public override void OnCreate(Bundle savedInstanceState)
 {
     base.OnCreate(savedInstanceState);
     if (Arguments != null)
     {
         _season = new TVSeason();
         _season = JsonConvert.DeserializeObject<TVSeason>(Arguments.GetString("seasonSerialized"));
         _position = Arguments.GetInt("position", 0);
     }
 }
        private async void PopulateSeasons()
        {
            string myURL = "https://api.themoviedb.org/3/tv/1396?api_key=661b76973b90b91e0df214904015fd4d";
            var client = new HttpClient();
            var data = await client.GetStringAsync(myURL);
            JObject o = JObject.Parse(data);
            _myShow.Name = o["name"] + "";
            _myShow.Network = o["networks"][0]["name"] + "";
            _myShow.NumberOfSeasons = (int)o["number_of_seasons"];
            _myShow.TMDBID = o["id"] + "";
            //_myShow.FullJson = data;
            _myShow.PosterLink = o["poster_path"] + "";
            _myShow.Overview = o["overview"] + "";
            _myShow.Language = o["languages"][0] + "";
           // SupportActionBar._title = _myShow.Name;
            //await _myShow.PopulateSeasonJSONListFromWeb();
            //_myTextView.Text = _myShow.Name + " is on " + _myShow.Network;

            //SetUpListViewFragmnet();
            _myShow.Seasons = new List<TVSeason>();
            for (int i = 1; i <= _myShow.NumberOfSeasons; i++)
            {
                TVSeason mySeason = new TVSeason();
                string mySeasonURL = "https://api.themoviedb.org/3/tv/1396/season/"+i+"?api_key=661b76973b90b91e0df214904015fd4d";
                var seasonClient = new HttpClient();
                var seasonData = await seasonClient.GetStringAsync(mySeasonURL);
                JObject seasonJObject = JObject.Parse(seasonData);


                mySeason.AirDate = "" + seasonJObject["air_date"];
                mySeason.TVDBID = "" + seasonJObject["id"];
                mySeason.Overview = "" + seasonJObject["overview"];
                mySeason.PosterPath = "" + seasonJObject["poster_path"];
                mySeason.SeasonNumber = (int)seasonJObject["season_number"];
                
                JArray myEpisodeArray = (JArray)seasonJObject.SelectToken("episodes");
                mySeason.EpisodeCount = myEpisodeArray.Count;

                mySeason.Episodes = new List<TVEpisode>();
                for (int j = 0; j < myEpisodeArray.Count; j++)
                {
                    JToken episodeJToken = myEpisodeArray[j];

                    TVEpisode myEpisode = new TVEpisode();
                    myEpisode.EpisodeNumber = (int)episodeJToken["episode_number"];
                    myEpisode.Name = "" + episodeJToken["name"];
                    mySeason.Episodes.Add(myEpisode);
                    
                }



                _myShow.Seasons.Add(mySeason);
            }
            string serialized = JsonConvert.SerializeObject(_myShow);
            Console.WriteLine(serialized);
            _myShow = new TVShow();
            _myShow = JsonConvert.DeserializeObject<TVShow>(serialized);
            Console.WriteLine(JsonConvert.SerializeObject(_myShow));
            // Console.UpdateTrakkedShowList("Last Episode of First Season is titled " +
            //                  _myShow.SeasonList[0].TVEpisodes[_myShow.SeasonList[0].TVEpisodes.Count - 1].Name);
        }
        public static async Task<TVShow> DownloadTVShowAndSave(string tmdbID)
        {
            var show = new TVShow();

            var myURL = "https://api.themoviedb.org/3/tv/" + tmdbID +
                        "?api_key=661b76973b90b91e0df214904015fd4d";
            var client = new HttpClient();
            var data = await client.GetStringAsync(myURL);
            var o = JObject.Parse(data);
            show.Name = o["name"] + "";
            show.Network = o["networks"][0]["name"] + "";
            show.NumberOfSeasons = (int) o["number_of_seasons"];
            var mySeasonArray = (JArray) o.SelectToken("seasons");
            var elementsInSeasons = mySeasonArray.Count;

            var hasSeasonZero = mySeasonArray.Count > show.NumberOfSeasons;

            show.TMDBID = o["id"] + "";
            show.PosterLink = o["poster_path"] + "";
            show.Overview = o["overview"] + "";
            show.Language = o["languages"][0] + "";
            show.Seasons = new List<TVSeason>();

            var endAtSeason = hasSeasonZero ? 0 : 1;

            for (var i = show.NumberOfSeasons; i >= endAtSeason; i--)
            {
                var mySeason = new TVSeason();
                var mySeasonURL = "https://api.themoviedb.org/3/tv/" + tmdbID +
                                  "/season/" + i + "?api_key=661b76973b90b91e0df214904015fd4d";
                var seasonClient = new HttpClient();
                try
                {
                    var seasonData = await seasonClient.GetStringAsync(mySeasonURL);
                    var seasonJObject = JObject.Parse(seasonData);

                    mySeason.AirDate = "" + seasonJObject["air_date"];
                    mySeason.TVDBID = "" + seasonJObject["id"];
                    mySeason.Overview = "" + seasonJObject["overview"];
                    mySeason.PosterPath = "" + seasonJObject["poster_path"];
                    mySeason.SeasonNumber = (int) seasonJObject["season_number"];

                    var myEpisodeArray = (JArray) seasonJObject.SelectToken("episodes");
                    mySeason.EpisodeCount = myEpisodeArray.Count;

                    mySeason.Episodes = new List<TVEpisode>();
                    foreach (var episodeJToken in myEpisodeArray)
                    {
                        var myEpisode = new TVEpisode
                        {
                            EpisodeNumber = (int) episodeJToken["episode_number"],
                            SeasonNumber = i,
                            Name = "" + episodeJToken["name"],
                            AirDate = "" + episodeJToken["air_date"],
                            StillPath = "" + episodeJToken["still_path"],
                            Overview = "" + episodeJToken["overview"]
                        };
                        mySeason.Episodes.Add(myEpisode);
                    }
                    show.Seasons.Add(mySeason);
                }
                catch (Exception ex)
                {
                    //Takes care of 404, need to confirm
                    Console.WriteLine("I caught this" + ex);
                }
            }

            var creditsURL = "https://api.themoviedb.org/3/tv/" + tmdbID +
                             "/credits?api_key=661b76973b90b91e0df214904015fd4d";
            var creditClient = new HttpClient();
            var creditData = await creditClient.GetStringAsync(creditsURL);
            var creditJObject = JObject.Parse(creditData);

            show.Actors = new List<Actor>();

            var myActorArray = (JArray) creditJObject.SelectToken("cast");
            foreach (var actorJToken in myActorArray)
            {
                var actor = new Actor
                {
                    characterName = "" + actorJToken["character"],
                    creditId = "" + actorJToken["credit_id"],
                    tmdbID = "" + actorJToken["id"],
                    name = "" + actorJToken["name"],
                    profilePath = "" + actorJToken["profile_path"],
                    order = (int) actorJToken["order"]
                };
                show.Actors.Add(actor);
            }

            string showToSave = JsonConvert.SerializeObject(show);
            TMDBFileSaving.saveTMDBShowJSON(show.TMDBID, showToSave);


            return show;
        }