context: using BrightIdeasSoftware;
  using Microsoft.Win32;
  using Sdl.Community.Hooks;
  using Sdl.Community.Parser;
  using Sdl.Community.Qualitivity.Dialogs;
  using Sdl.Community.Qualitivity.Dialogs.Export;
  using Sdl.Community.Qualitivity.Panels.Activity_Records;
  using Sdl.Community.Qualitivity.Panels.Document_Reports;
  using Sdl.Community.Qualitivity.Panels.Properties;
  using Sdl.Community.Qualitivity.Panels.QualityMetrics;
  using Sdl.Community.Qualitivity.Progress;
  using Sdl.Community.Qualitivity.Tracking;
  using Sdl.Community.Report;
  using Sdl.Community.Structures.Configuration;
  using Sdl.Community.Structures.Documents;
  using Sdl.Community.Structures.Profile;
  using Sdl.Community.Structures.PropertyView;
  using Sdl.Community.TM.Database;
  using Sdl.Desktop.IntegrationApi;
  using Sdl.Desktop.IntegrationApi.Extensions;
  using Sdl.ProjectAutomation.Core;
  using Sdl.ProjectAutomation.FileBased;
  using Sdl.TranslationStudioAutomation.IntegrationApi;
  using Sdl.TranslationStudioAutomation.IntegrationApi.Presentation.DefaultLocations;
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Diagnostics;
  using System.Drawing;
  using System.Globalization;
  using System.IO;
  using System.Linq;
  using System.Windows.Forms;
  using Sdl.Desktop.IntegrationApi.Interfaces;
  using Activity = Sdl.Community.Structures.Projects.Activities.Activity;
  using DocumentActivities = Sdl.Community.Structures.Projects.Activities.DocumentActivities;
  using Project = Sdl.Community.Structures.Projects.Project;
  using QualityMetric = Sdl.Community.Structures.Documents.Records.QualityMetric;
  using Settings = Sdl.Community.Structures.Configuration.Settings;
  
  namespace Sdl.Community.Qualitivity.Panels.Main
  {
  
  
  	[View(
  		Id = "QualitivityViewController",
  		Name = "Qualitivity",
  		Description = "Qualitivity",
  		Icon = "QualitivityApp_Icon",
  		LocationByType = typeof(TranslationStudioDefaultViews.TradosStudioViewsLocation),
  		AllowViewParts = true
  		)]
  	public class QualitivityViewController : AbstractViewController
  	{
  
  
  		private Timer Timer4ProjectArea { get; set; }
  
  		private static readonly Lazy<QualitivityViewControl> _viewContent = new Lazy<QualitivityViewControl>();
  		private static readonly Lazy<QualitivityNavigationControl> _viewNavigation = new Lazy<QualitivityNavigationControl>();
  
  		public FileBasedProject CurrentSelectedProject { get; set; }
  		public static ProjectsController ProjectsController { get; set; }
  		public ProjectInfo CurrentProjectInfo { get; set; }
  
  		private bool IsLoading { get; set; }
  		public Project SelectedProject { get; set; }
  		public List<Activity> SelectedActivities { get; set; }
  
  
  		protected override IUIControl GetContentControl()
  		{
  			return _viewContent.Value;
  		}
  		protected override IUIControl GetExplorerBarControl()
  		{
  			return _viewNavigation.Value;
  		}
  
  
  		public event EventHandler CheckEnabledObjectsEvent;
  		public void CheckEnabledObjects()
  		{
  
  			try
  			{
  
  				#region  |  navigation selection  |
  
  
  
  				TimeTrackerProjectNewEnabled = true;
  
  				_viewNavigation.Value.newTimeTrackerProjectToolStripMenuItem.Enabled = TimeTrackerProjectNewEnabled;
  				_viewNavigation.Value.editTimeTrackerProjectToolStripMenuItem.Enabled = TimeTrackerProjectEditEnabled;
  				_viewNavigation.Value.removeTimeTrackerProjectToolStripMenuItem.Enabled = TimeTrackerProjectRemoveEnabled;
  
  
  				if (_viewNavigation.Value.treeView_navigation.SelectedNode != null && _viewNavigation.Value.treeView_navigation.Nodes.Count > 0)
  				{
  					if (_viewNavigation.Value.treeView_navigation.SelectedNode.Tag != null
  						&& _viewNavigation.Value.treeView_navigation.SelectedNode.Tag.GetType() == typeof(Project))
  					{
  						ProjectActivityNewEnabled = true;
  						_viewNavigation.Value.newProjectActivityToolStripMenuItem.Enabled = true;
  						_viewContent.Value.newProjectActivityToolStripMenuItem.Enabled = true;
  					}
  					else if (_viewNavigation.Value.treeView_navigation.SelectedNode.Tag == null
  						|| _viewNavigation.Value.treeView_navigation.SelectedNode.Tag.GetType() == typeof(CompanyProfile))
  					{
  						if (_viewNavigation.Value.treeView_navigation.SelectedNode.Nodes.Count > 0)
  						{
  							ProjectActivityNewEnabled = true;
  							_viewNavigation.Value.newProjectActivityToolStripMenuItem.Enabled = true;
  							_viewContent.Value.newProjectActivityToolStripMenuItem.Enabled = true;
  						}
  						else
  						{
  							ProjectActivityNewEnabled = false;
  							_viewNavigation.Value.newProjectActivityToolStripMenuItem.Enabled = false;
  							_viewContent.Value.newProjectActivityToolStripMenuItem.Enabled = false;
  
  							_viewNavigation.Value.editTimeTrackerProjectToolStripMenuItem.Enabled = true;
  							_viewNavigation.Value.removeTimeTrackerProjectToolStripMenuItem.Enabled = true;
  						}
  					}
  					else if (_viewContent.Value.objectListView1.SelectedItems.Count > 0)
  					{
  						ProjectActivityNewEnabled = true;
  						_viewNavigation.Value.newProjectActivityToolStripMenuItem.Enabled = true;
  						_viewContent.Value.newProjectActivityToolStripMenuItem.Enabled = true;
  					}
  					else
  					{
  						ProjectActivityNewEnabled = false;
  						_viewNavigation.Value.newProjectActivityToolStripMenuItem.Enabled = false;
  						_viewContent.Value.newProjectActivityToolStripMenuItem.Enabled = false;
  					}
  				}
  				else
  				{
  					FillActivityDataView(new List<Activity>());
  
  					ProjectActivityNewEnabled = false;
  					_viewNavigation.Value.newProjectActivityToolStripMenuItem.Enabled = false;
  					_viewContent.Value.newProjectActivityToolStripMenuItem.Enabled = false;
  				}
  
  				if (_viewNavigation.Value.treeView_navigation.SelectedNode != null
  					&& _viewContent.Value.objectListView1.SelectedItems.Count > 0)
  				{
  					ProjectActivityEditEnabled = true;
  					ProjectActivityRemoveEnabled = true;
  
  					_viewContent.Value.editProjectActivityToolStripMenuItem.Enabled = true;
  					_viewContent.Value.removeProjectActivityToolStripMenuItem.Enabled = true;
  				}
  				else
  				{
  					ProjectActivityEditEnabled = false;
  					ProjectActivityRemoveEnabled = false;
  
  					_viewContent.Value.editProjectActivityToolStripMenuItem.Enabled = false;
  					_viewContent.Value.removeProjectActivityToolStripMenuItem.Enabled = false;
  				}
  
  				if (_viewContent.Value.objectListView1.SelectedItems.Count > 1)
  				{
  					ProjectActivityMergeEnabled = true;
  					_viewContent.Value.mergeProjectActivitiesToolStripMenuItem.Enabled = true;
  
  					ProjectActivityDuplicateEnabled = true;
  					_viewContent.Value.duplicateTheProjectActivityToolStripMenuItem.Enabled = true;
  				}
  				else if (_viewContent.Value.objectListView1.SelectedItems.Count == 1)
  				{
  					ProjectActivityMergeEnabled = false;
  					_viewContent.Value.mergeProjectActivitiesToolStripMenuItem.Enabled = false;
  
  					ProjectActivityDuplicateEnabled = true;
  					_viewContent.Value.duplicateTheProjectActivityToolStripMenuItem.Enabled = true;
  				}
  				else
  				{
  					ProjectActivityMergeEnabled = false;
  					_viewContent.Value.mergeProjectActivitiesToolStripMenuItem.Enabled = false;
  
  					ProjectActivityDuplicateEnabled = false;
  					_viewContent.Value.duplicateTheProjectActivityToolStripMenuItem.Enabled = false;
  				}
  
  				if (_viewContent.Value.objectListView1.SelectedItems.Count > 0)
  				{
  					ProjectActivityCreateReportEnabled = true;
  					_viewContent.Value.createAnActivitiesReportToolStripMenuItem.Enabled = true;
  				}
  				else
  				{
  					ProjectActivityCreateReportEnabled = false;
  					_viewContent.Value.createAnActivitiesReportToolStripMenuItem.Enabled = false;
  				}
  				if (_viewNavigation.Value.treeView_navigation.SelectedNode != null)
  				{
  
  					ProjectActivityExportExcelEnabled = true;
  					_viewContent.Value.exportActivitiesToExcelToolStripMenuItem.Enabled = true;
  				}
  				else
  				{
  					ProjectActivityExportExcelEnabled = false;
  					_viewContent.Value.exportActivitiesToExcelToolStripMenuItem.Enabled = false;
  				}
  				#endregion
  
  
  				#region  |  Quality Metric  |
  
  				IsEnabledCreateNewQualityMetric = true;
  
  
  				#endregion
  
  				if (CheckEnabledObjectsEvent != null)
  					CheckEnabledObjectsEvent(this, EventArgs.Empty);
  
  				QualitivityViewPropertiesController.NavigationTreeView = _viewNavigation.Value.treeView_navigation;
  				QualitivityViewPropertiesController.ObjectListView = _viewContent.Value.objectListView1;
  				QualitivityViewPropertiesController.UpdateActivityPropertiesViewer();
  
  			}
  			catch (Exception ex)
  			{
  				MessageBox.Show(ex.Message);
  			}
  
  		}
  
  
  
  		private bool IsActive { get; set; }
  		protected override void Initialize(IViewContext context)
  		{
  			ActivationChanged += StudioTimeTrackerViewController_ActivationChanged;
  
  			SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
  
  			IsLoading = true;
  
  			IsEnabledCreateNewActivityProject = false;
  			IsEnabledCreateNewQualityMetric = false;
  
  			TimeTrackerProjectNewEnabled = true;
  			TimeTrackerProjectEditEnabled = false;
  			TimeTrackerProjectRemoveEnabled = false;
  
  
  			ProjectActivityNewEnabled = false;
  			ProjectActivityEditEnabled = false;
  			ProjectActivityRemoveEnabled = false;
  
  
  			ProjectActivityStartTrackerEnabled = true;
  			ProjectActivityStopTrackerEnabled = false;
  
  			ProjectActivityDuplicateEnabled = false;
  			ProjectActivityMergeEnabled = false;
  
  			ProjectActivityCreateReportEnabled = false;
  			ProjectActivityExportExcelEnabled = false;
  
  
  			#region  |  initialize the tracker cache  |
  
  
  			Tracked.TrackingState = Tracked.TimerState.None;
  			Tracked.TrackingTimer = new Stopwatch();
  			Tracked.TrackingStart = null;
  			Tracked.TrackingEnd = null;
  			Tracked.TrackingPaused = new Stopwatch();
  
  
  			Tracked.DocumentSegmentPairs = new Dictionary<string, SegmentPair>();
  			Tracked.ActiveDocument = null;
  			Tracked.DictCacheDocumentItems = new Dictionary<string, TrackedDocuments>();
  
  
  			#endregion
  
  			LoadCurrencies();
  
  
  			Tracked.Settings = new Settings();
  
  			Sdl.Community.TM.Database.Helper.InitializeDatabases(Tracked.Settings.ApplicationPaths.ApplicationMyDocumentsDatabasePath);
  
  
  
  			var query = new Query();
  
  
  			query.InitializeSettings(Tracked.Settings.ApplicationPaths.ApplicationMyDocumentsDatabaseSettingsPath
  				, Tracked.Settings.ViewSettings.ViewProperties
  				, Tracked.Settings.BackupSettings.BackupProperties
  				, Tracked.Settings.GeneralSettings.GeneralProperties
  				, Tracked.Settings.TrackingSettings.TrackingProperties);
  
  
  
  
  			Tracked.Settings.ViewSettings.ViewProperties = query.GetViewSettings(Tracked.Settings.ApplicationPaths.ApplicationMyDocumentsDatabaseSettingsPath);
  			Tracked.Settings.BackupSettings.BackupProperties = query.GetBackupSettings(Tracked.Settings.ApplicationPaths.ApplicationMyDocumentsDatabaseSettingsPath);
  			Tracked.Settings.GeneralSettings.GeneralProperties = query.GetGeneralSettings(Tracked.Settings.ApplicationPaths.ApplicationMyDocumentsDatabaseSettingsPath);
  			Tracked.Settings.TrackingSettings.TrackingProperties = query.GetTrackerSettings(Tracked.Settings.ApplicationPaths.ApplicationMyDocumentsDatabaseSettingsPath);
  
  
  
  			Tracked.Settings.QualityMetricGroupSettings.QualityMetricGroups = query.GetQualityMetricGroupSettings(Tracked.Settings.ApplicationPaths.ApplicationMyDocumentsDatabaseSettingsPath, null);
  			foreach (var qmg in Tracked.Settings.QualityMetricGroupSettings.QualityMetricGroups)
  				if (qmg.IsDefault)
  					Tracked.Settings.QualityMetricGroup = qmg;
  
  
  
  			Tracked.Settings.UserProfile = new UserProfile();
  			var userProfiles = query.GetUserProfiles(Tracked.Settings.ApplicationPaths.ApplicationMyDocumentsDatabaseSettingsPath, null);
  			if (userProfiles.Count > 0)
  				Tracked.Settings.UserProfile = userProfiles[0];
  
  
  			Tracked.Settings.CompanyProfiles = query.GetCompanyProfiles(Tracked.Settings.ApplicationPaths.ApplicationMyDocumentsDatabaseSettingsPath, null);
  			Tracked.Settings.LanguageRateGroups = query.GetLanguageRateGroupSetttings(Tracked.Settings.ApplicationPaths.ApplicationMyDocumentsDatabaseSettingsPath, null);
  
  
  			Tracked.TrackingProjects = new TrackingProjects
  			{
  				TrackerProjects =
  					query.GetProjects(Tracked.Settings.ApplicationPaths.ApplicationMyDocumentsDatabaseProjectsPath,
  						"", "", "", "")
  			};
  
  
  			var defaultActivityViewGroupsIsOn = Convert.ToBoolean(Tracked.Settings.GetGeneralProperty("defaultActivityViewGroupsIsOn").Value);
  			_viewContent.Value.objectListView1.ShowGroups = defaultActivityViewGroupsIsOn;
  			_viewContent.Value.linkLabel_turn_off_groups.Text = defaultActivityViewGroupsIsOn ? "Turn off groups" : "Turn on groups";
  
  
  			var reportProcessor = new Processor();
  			reportProcessor.InitializeWriteFlagsFolder(Tracked.Settings.ApplicationPaths.ApplicationTrackChangesReportPath, new List<string> { "empty" });
  
  
  
  			ProjectsController = SdlTradosStudio.Application.GetController<ProjectsController>();
  
  
  			InitializeTrackingEventsTab();
  
  			InitializeNavigationControl();
  
  			InitializeViewControl();
  
  			CheckEnabledObjects();
  
  
  
  			Timer4ProjectArea = new Timer { Interval = 1000 };
  			Timer4ProjectArea.Tick += Timer4ProjectArea_Tick;
  			Timer4ProjectArea.Start();
  
  
  			Viewer.IsTracking = Convert.ToBoolean(Tracked.Settings.GetTrackingProperty("recordKeyStokes").Value);
  			if (Viewer.IsTracking)
  				Viewer.StartTracking();
  			else
  				Viewer.StopTracking();
  
  			IsLoading = false;
  
  
  
  			if (Convert.ToBoolean(Tracked.Settings.GetTrackingProperty("startOnLoad").Value))
  				StartTimeTracking();
  
  
  			CheckBackupStatus();
  
  			ProjectsController.SelectedProjectsChanged += projectsController_SelectedProjectsChanged;
  
  		}
  
  	
  		private void projectsController_SelectedProjectsChanged(object sender, EventArgs e)
  		{
  
  			if (!TimeTrackerProjectNewEnabled)
  			{
  				IsEnabledCreateNewActivityProject = false;
  			}
  			else if (ProjectsController.SelectedProjects.FirstOrDefault() != null)
  			{
  				CurrentSelectedProject = ProjectsController.SelectedProjects.FirstOrDefault();
  
gt: 				if (CurrentSelectedProject != null)