context: //***********************************************************************
  // Assembly         : LatestMediaHandler
  // Author           : cul8er
  // Created          : 05-09-2010
  //
  // Last Modified By : ajs
  // Last Modified On : 30-09-2015
  // Description      : 
  //
  // Copyright        : Open Source software licensed under the GNU/GPL agreement.
  //***********************************************************************
  extern alias LMHNLog;
  
  using MediaPortal.Dialogs;
  using MediaPortal.GUI.Library;
  using MediaPortal.Video.Database;
  
  using LMHNLog.NLog;
  
  using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Globalization;
  using System.Threading;
  using System.Threading.Tasks;
  using System.Timers;
  
  using WindowPlugins.GUITVSeries;
  using System.Runtime.CompilerServices;
  
  using ViewTypes = LatestMediaHandler.LatestsFacadeType;
  using ResultTypes = LatestMediaHandler.LatestsFacadeSubType;
  
  namespace LatestMediaHandler
  {
    internal class LatestTVSeriesHandler
    {
      #region declarations
  
      private Logger logger = LogManager.GetCurrentClassLogger();
  
      private VideoHandler episodePlayer = null;
  
      private int countTVSeries = 0;
      private LatestsCollection latestTVSeries = null;
      private Hashtable latestTVSeriesForPlay;
  
      private ArrayList facadeCollection = new ArrayList();
      internal ArrayList images = new ArrayList();
      internal ArrayList imagesThumbs = new ArrayList();
  
      private int showFanart = 1;
      private bool needCleanup = false;
      private int needCleanupCount = 0;
      private int currentFacade = 0;
  
      private static Object lockObject = new object();
  
      #endregion
  
      public const int ControlID = 919199940;
      public const int Play1ControlID = 91919994;
      public const int Play2ControlID = 91919995;
      public const int Play3ControlID = 91919996;
      public const int Play4ControlID = 91919904;
  
      public List<LatestsFacade> ControlIDFacades;
      public List<int> ControlIDPlays;
  
      public bool MainFacade
      {
        get { return CurrentFacade.ControlID == ControlID; }
      }
  
      public LatestsFacade CurrentFacade
      {
        get { return ControlIDFacades[currentFacade]; }
      }
  
      public LatestsFacade LatestFacade
      {
        get { return ControlIDFacades[ControlIDFacades.Count - 1]; }
      }
  
      public int NeedCleanupCount
      {
        get { return needCleanupCount; }
        set { needCleanupCount = value; }
      }
  
      public bool NeedCleanup
      {
        get { return needCleanup; }
        set { needCleanup = value; }
      }
  
      public int ShowFanart
      {
        get { return showFanart; }
        set { showFanart = value; }
      }
  
      public ArrayList Images
      {
        get { return images; }
        set { images = value; }
      }
  
      public ArrayList FacadeCollection
      {
        get { return facadeCollection; }
        set { facadeCollection = value; }
      }
  
      public LatestTVSeriesHandler(int id = ControlID)
      {
        ControlIDFacades = new List<LatestsFacade>();
        ControlIDPlays = new List<int>();
        //
        ControlIDFacades.Add(new LatestsFacade(id, "TVSeries"));
        if (id == ControlID)
        {
          ControlIDPlays.Add(Play1ControlID);
          ControlIDPlays.Add(Play2ControlID);
          ControlIDPlays.Add(Play3ControlID);
          ControlIDPlays.Add(Play4ControlID);
        }
  
        CurrentFacade.UnWatched = Utils.LatestTVSeriesWatched;
        switch (Utils.LatestTVSeriesView)
        {
          case 0:
            CurrentFacade.Type = ViewTypes.Latests;
            break;
          case 1:
            CurrentFacade.Type = ViewTypes.Watched;
            break;
          case 2:
            CurrentFacade.Type = ViewTypes.Rated;
            break;
          case 3:
            CurrentFacade.Type = ViewTypes.Next;
            break;
        }
  
        switch (Utils.LatestTVSeriesType)
        {
          case 0:
            CurrentFacade.SubType = ResultTypes.Episodes;
            break;
          case 1:
            CurrentFacade.SubType = ResultTypes.Seasons;
            break;
          case 2:
            CurrentFacade.SubType = ResultTypes.Series;
            break;
        }
  
        Utils.ClearSelectedTVSeriesProperty(CurrentFacade);
        EmptyLatestMediaProperties();
      }
  
      internal LatestTVSeriesHandler(LatestsFacade facade) : this (facade.ControlID)
      {
        ControlIDFacades[ControlIDFacades.Count - 1] = facade;
      }
  
      internal void MyContextMenu()
      {
        try
        {
          IDialogbox dlg = (GUIDialogMenu) GUIWindowManager.GetWindow((int) GUIWindow.Window.WINDOW_DIALOG_MENU);
          if (dlg == null) return;
  
          dlg.Reset();
          dlg.SetHeading(924);
  
          //Add Details Menu Item
          //Play Menu Item
          GUIListItem pItem = new GUIListItem(Translation.Play);
          dlg.Add(pItem);
          pItem.ItemId = 1;
  
          pItem = new GUIListItem(Translation.EpisodeDetails);
          dlg.Add(pItem);
          pItem.ItemId = 2;
  
          pItem = new GUIListItem(Translation.ToSeries);
          dlg.Add(pItem);
          pItem.ItemId = 3;
  
          //Add Display Menu Item
          if (CurrentFacade.Type == LatestsFacadeType.Latests)
          {
            pItem = new GUIListItem(Translation.DisplayNextEpisodes);
            dlg.Add(pItem);
            pItem.ItemId = 4;
          }
          else
          {
            pItem = new GUIListItem(Translation.DisplayLatestEpisodes);
            dlg.Add(pItem);
            pItem.ItemId = 4;
          }
  
          //Add Watched/Unwatched Filter Menu Item
          if (!CurrentFacade.UnWatched)
          {
            pItem = new GUIListItem(Translation.ShowUnwatchedEpisodes);
            dlg.Add(pItem);
            pItem.ItemId = 5;
          }
          else
          {
            pItem = new GUIListItem(Translation.ShowAllEpisodes);
            dlg.Add(pItem);
            pItem.ItemId = 5;
          }
  
          //Add TV Series Latests SubType
          pItem = new GUIListItem();
          // pItem.Label = "[^] "+(resultType == ResultTypes.Episodes ? Translation.ShowLatestsEpisodes : (resultType == ResultTypes.Seasons ? Translation.ShowLatestsSeasons : Translation.ShowLatestsSeries));
          pItem.Label = "[^] " + CurrentFacade.Title + " " + CurrentFacade.SubTitle;
          pItem.ItemId = 6;
          dlg.Add(pItem);
  
          //Add TV Series Latests Type
          pItem = new GUIListItem();
          pItem.Label = "[^] " + CurrentFacade.Title;
          pItem.ItemId = 7;
          dlg.Add(pItem);
  
          // Update
          pItem = new GUIListItem();
          pItem.Label = Translation.Update;
          pItem.ItemId = 8;
          dlg.Add(pItem);
  
          //Show Dialog
          dlg.DoModal(Utils.ActiveWindow);
  
          if (dlg.SelectedLabel == -1)
            return;
  
          switch (dlg.SelectedId)
          {
            case 1:
            {
              PlayTVSeries(GUIWindowManager.GetWindow(Utils.ActiveWindow));
              break;
            }
            case 2:
            case 3:
            {
              ShowInfo(dlg.SelectedId == 3);
              break;
            }
            case 4:
            {
              CurrentFacade.Type = (CurrentFacade.Type == LatestsFacadeType.Latests ? LatestsFacadeType.Next : LatestsFacadeType.Latests);
              TVSeriesUpdateLatest();
              break;
            }
            case 5:
            {
              CurrentFacade.UnWatched = !CurrentFacade.UnWatched;
              TVSeriesUpdateLatest();
              break;
            }
            case 6:
            {
              IDialogbox ldlg = (GUIDialogMenu) GUIWindowManager.GetWindow((int) GUIWindow.Window.WINDOW_DIALOG_MENU);
              if (ldlg == null) return;
  
              ldlg.Reset();
              ldlg.SetHeading(924);
  
              //Add Details Menu Item
              // pItem = new GUIListItem(((Utils.LatestTVSeriesType == 0) ? "[x] " : string.Empty) + Translation.ShowLatestsEpisodes);
              pItem = new GUIListItem((CurrentFacade.SubType == ResultTypes.Episodes ? "[x] " : string.Empty) + Translation.ShowLatestsEpisodes);
              ldlg.Add(pItem);
              pItem.ItemId = 1;
  
              pItem = new GUIListItem((CurrentFacade.SubType == ResultTypes.Seasons ? "[x] " : string.Empty) + Translation.ShowLatestsSeasons);
              ldlg.Add(pItem);
              pItem.ItemId = 2;
  
              pItem = new GUIListItem((CurrentFacade.SubType == ResultTypes.Series ? "[x] " : string.Empty) + Translation.ShowLatestsSeries);
              ldlg.Add(pItem);
              pItem.ItemId = 3;
  
              //Show Dialog
              ldlg.DoModal(Utils.ActiveWindow);
  
              if (ldlg.SelectedLabel == -1)
                return;
  
              // Utils.LatestTVSeriesType = ldlg.SelectedId - 1;
              switch (ldlg.SelectedId - 1)
              {
                case 0:
                  CurrentFacade.SubType = ResultTypes.Episodes;
                  break;
                case 1:
                    CurrentFacade.SubType = ResultTypes.Seasons;
                  break;
                case 2:
                    CurrentFacade.SubType = ResultTypes.Series;
                  break;
              }
              TVSeriesUpdateLatest();
              break;
            }
            case 7:
            {
              IDialogbox ldlg = (GUIDialogMenu)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_MENU);
              if (ldlg == null) return;
  
              ldlg.Reset();
              ldlg.SetHeading(924);
  
              //Add Types Menu Items
              pItem = new GUIListItem((CurrentFacade.Type == LatestsFacadeType.Latests ? "[x] " : string.Empty) + Translation.LabelLatestAdded);
              ldlg.Add(pItem);
              pItem.ItemId = 1;
  
              pItem = new GUIListItem((CurrentFacade.Type == LatestsFacadeType.Watched ? "[x] " : string.Empty) + Translation.LabelLatestWatched);
              ldlg.Add(pItem);
              pItem.ItemId = 2;
  
              pItem = new GUIListItem((CurrentFacade.Type == LatestsFacadeType.Rated ? "[x] " : string.Empty) + Translation.LabelHighestRated);
              ldlg.Add(pItem);
              pItem.ItemId = 3;
  
              pItem = new GUIListItem((CurrentFacade.Type == LatestsFacadeType.Next ? "[x] " : string.Empty) + Translation.DisplayNextEpisodes);
              ldlg.Add(pItem);
              pItem.ItemId = 4;
  
              //Show Dialog
              ldlg.DoModal(Utils.ActiveWindow);
  
              if (ldlg.SelectedLabel == -1)
                return;
  
              switch (ldlg.SelectedId - 1)
              {
                case 0:
                  CurrentFacade.Type = LatestsFacadeType.Latests;
                  break;
                case 1:
                  CurrentFacade.Type = LatestsFacadeType.Watched;
                  break;
                case 2:
                  CurrentFacade.Type = LatestsFacadeType.Rated;
                  break;
              }
                TVSeriesUpdateLatest();
              break;
            }
            case 8:
            {
              TVSeriesUpdateLatest();
              break;
            }
          }
        }
        catch (Exception ex)
        {
          logger.Error("MyContextMenu: " + ex.ToString());
        }
      }
  
      private void ShowInfo(bool toseries = false)
      {
        try
        {
          GUIWindow fWindow = GUIWindowManager.GetWindow(Utils.ActiveWindow);
          int FocusControlID = fWindow.GetFocusControlId();
  
          int idx = -1 ;
          if (ControlIDPlays.Contains(FocusControlID))
          {
            idx = ControlIDPlays.IndexOf(FocusControlID);
          }
          //
          // CurrentFacade.Facade = Utils.GetLatestsFacade(CurrentFacade.ControlID);
          if (CurrentFacade.Facade != null && CurrentFacade.Facade.Focus && CurrentFacade.Facade.SelectedListItem != null)
          {
            idx = CurrentFacade.Facade.SelectedListItem.ItemId-1;
          }
          //
          if (idx >= 0)
          {
            string sHyp = string.Empty;
            if (toseries)
              sHyp = "seriesid:" + latestTVSeries[idx].SeriesIndex + 
                      ((CurrentFacade.SubType == ResultTypes.Seasons || CurrentFacade.SubType == ResultTypes.Episodes) ? 
                      "|seasonidx:" + Utils.RemoveLeadingZeros(latestTVSeries[idx].SeasonIndex) : string.Empty) ;
            else
              sHyp = "seriesid:" + latestTVSeries[idx].SeriesIndex + 
                      ((CurrentFacade.SubType == ResultTypes.Seasons || CurrentFacade.SubType == ResultTypes.Episodes) ? 
                      "|seasonidx:" + Utils.RemoveLeadingZeros(latestTVSeries[idx].SeasonIndex) : string.Empty) +
                      ((CurrentFacade.SubType == ResultTypes.Episodes) ? "|episodeidx:" + Utils.RemoveLeadingZeros(latestTVSeries[idx].EpisodeIndex) : string.Empty);
            GUIWindowManager.ActivateWindow(9811, sHyp, false);
          }
        }
        catch (Exception ex)
        {
          logger.Error("ShowInfo: " + ex.ToString());
        }
      }
  
      /// <summary>
      /// Returns latest added tvseries thumbs from TVSeries db.
      /// </summary>
      /// <param name="watched">Only watched TVSeries</param>
      /// <returns>Resultset of matching data</returns>
      private LatestsCollection GetLatestTVSeries()
      {
  
gt:       latestTVSeries = new LatestsCollection();