context: /*
  Copyright (C) 2007, 2008 Kristian Bisgaard Lassen
  Copyright (C) 2010 Kristian Helkjaer Lassen
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 3 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  
  You should have received a copy of the GNU Lesser General Public
  License along with this library. If not, see <http://www.gnu.org/licenses/>.
   */
  
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Drawing;
  using System.Data;
  using System.Text;
  using System.Windows.Forms;
  using System.Reflection;
  using System.Collections;
  
  using ZoneFiveSoftware.Common.Data;
  using ZoneFiveSoftware.Common.Data.Fitness;
  using ZoneFiveSoftware.Common.Visuals;
  using ZoneFiveSoftware.Common.Visuals.Fitness;
  using ZoneFiveSoftware.Common.Visuals.Chart;
  using ZoneFiveSoftware.Common.Visuals.Util;
  using ZoneFiveSoftware.Common.Visuals.Mapping;
  using GpsRunningPlugin.Properties;
  using GpsRunningPlugin.Util;
  using TrailsPlugin;
  using TrailsPlugin.Data;
  using TrailsPlugin.Utils;
  using TrailsPlugin.UI.MapLayers;
  
  namespace GpsRunningPlugin.Source
  {
      public partial class HighScoreViewer : UserControl
      {
          private readonly Form popupForm;
          private IDictionary<GoalParameter, IDictionary<GoalParameter, IDictionary<bool, IList<Result>>>> cachedResults;
          private String speedUnit;
  #if ST_2_1
          //private object m_DetailPage = null;
  #else
          private IDetailPage m_DetailPage = null;
          private IDailyActivityView m_view = null;
          private TrailPointsLayer m_layer = null;
          private IList<IActivity> m_activities = new List<IActivity>();
          IList<IValueRangeSeries<DateTime>> m_pauses = null;
          private bool m_showPage = false;
  
          //Activity page
          public HighScoreViewer(IDetailPage detailPage, IDailyActivityView view)
             : this()
          {
              m_DetailPage = detailPage;
              m_view = view;
              m_layer = TrailPointsLayer.Instance(m_view);
              if (m_DetailPage != null)
              {
                  //expandButton.Visible = true;
              }
              if (Settings.ShowTrailsHint)
              {
                  String oneTimeMessage = "The Trails plugin can be used as viewer for High Scores. Trails provides many more features, for instance graphs and top lists. Just select the HighScore trail. See the HighScore or Trails documentation for more information. This message will not be shown again.";
                  DialogResult r = MessageDialog.Show(oneTimeMessage, "Trails Plugin", MessageBoxButtons.OK, MessageBoxIcon.Information);
                  if (r == DialogResult.OK)
                  {
                      Settings.ShowTrailsHint = false;
                  }
              }
          }
          //popup dialog
          internal HighScoreViewer(IDailyActivityView view)
              : this(true)
          {
              m_view = view;
              m_layer = TrailPointsLayer.Instance((IView)view);
              this.ShowPage("");
          }
          internal HighScoreViewer(IActivityReportsView view)
              : this(true)
          {
              m_layer = TrailPointsLayer.Instance((IView)view);
              this.ShowPage("");
          }
  
          //UniqueRoutes sendto
          public HighScoreViewer(IList<IActivity> activities, IDailyActivityView view)
              : this(view)
          {
              this.Activities = activities;
          }
          public HighScoreViewer(IList<IActivity> activities, IActivityReportsView view)
              : this(view)
          {
              this.Activities = activities;
          }
  
          //Trails analyze
          public HighScoreViewer(IList<IActivity> activities, IList<IValueRangeSeries<DateTime>> pauses, IDailyActivityView view, System.Windows.Forms.ProgressBar progressBar)
              : this(view)
          {
              this.progressBar = progressBar;
              this.m_pauses = pauses;
              this.Activities = activities;
          }
  #endif
          private HighScoreViewer()
          {
              InitializeComponent();
              InitControls();
              this.hsControl.setViewer(this);
  
              paceBox.DropDownStyle = ComboBoxStyle.DropDownList;
              viewBox.DropDownStyle = ComboBoxStyle.DropDownList;
  
              //speedUnit = getMostUsedSpeedUnit(m_activities);
              //paceBox.SelectedItem = speedUnit;
              if (Settings.ShowTable)
              {
                  viewBox.SelectedItem = ZoneFiveSoftware.Common.Visuals.CommonResources.Text.LabelList;
              }
              else
              {
                  viewBox.SelectedItem = StringResources.Graph;
              }
  
              paceBox.SelectedIndexChanged += new EventHandler(paceBox_SelectedIndexChanged);
              viewBox.SelectedIndexChanged += new EventHandler(viewBox_SelectedIndexChanged);
          }
  
          ////Compatibility with old UniqueRoutes send to
          //public HighScoreViewer(IList<IActivity> aAct, bool showDialog, bool dummy)
          //    : this(showDialog)
          //{
          //    this.Activities = aAct;
          //}
  
          //Create popup
          private HighScoreViewer(bool showDialog)
              : this()
          {
              if (showDialog)
              {
                  //Theme and Culture must be set manually
                  this.ThemeChanged(
  #if ST_2_1
                    Plugin.GetApplication().VisualTheme
  #else
                    Plugin.GetApplication().SystemPreferences.VisualTheme
  #endif
  );
                  this.UICultureChanged(
  #if ST_2_1
                    new System.Globalization.CultureInfo("en")
  #else
                    Plugin.GetApplication().SystemPreferences.UICulture
  #endif
  );
                  popupForm = new Form();
                  popupForm.Controls.Add(this);
                  popupForm.Size = Settings.WindowSize;
                  popupForm.Icon = Icon.FromHandle(Properties.Resources.Image_32_HighScore.GetHicon());
                  this.Size = new Size(Parent.Size.Width - 17, Parent.Size.Height - 38);
                  this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                          | System.Windows.Forms.AnchorStyles.Right | System.Windows.Forms.AnchorStyles.Bottom)));
                  popupForm.StartPosition = FormStartPosition.CenterScreen;
                  popupForm.FormClosed += new FormClosedEventHandler(popupForm_FormClosed);
                  popupForm.Resize += new EventHandler(popupForm_Resize);
                  popupForm.Show();
              }
          }
  
          void InitControls()
          {
              paceBox.Items.Add(CommonResources.Text.LabelPace);
              paceBox.Items.Add(CommonResources.Text.LabelSpeed);
              viewBox.Items.Add(StringResources.Graph);
              viewBox.Items.Add(ZoneFiveSoftware.Common.Visuals.CommonResources.Text.LabelList);
  
              copyTableMenuItem.Image = ZoneFiveSoftware.Common.Visuals.CommonResources.Images.DocumentCopy16;
              summaryList.LabelProvider = new ResultLabelProvider();
              this.summaryListToolTipTimer.Tick += new System.EventHandler(ToolTipTimer_Tick);
          }
  
          public IList<IActivity> Activities
          {
              set
              {
                  //Make sure activities is not null
                  if (null == value) { m_activities.Clear(); }
                  else { m_activities = value; }
  
gt:                 if (popupForm != null)