public Mainform()
        {
            InitializeComponent();

            // for player control embedded in UI
            Program.SetWebBrowserFeatures();

            this.Icon = Bitmaps.Azure_Explorer_ico;

            // USER SETTINSG CHECKS & UPDATES
            // upgrade settings from previous version
            if (Properties.Settings.Default.CallUpgrade)
            {
                Properties.Settings.Default.Upgrade();
                Properties.Settings.Default.CallUpgrade = false;
            }

            // if installation file has been downloaded, let's delete it now
            if (!string.IsNullOrEmpty(Properties.Settings.Default.DeleteInstallationFile))
            {
                try
                {
                    File.Delete(Properties.Settings.Default.DeleteInstallationFile);
                    Properties.Settings.Default.DeleteInstallationFile = string.Empty;
                    Properties.Settings.Default.Save();
                }
                catch
                {

                }
            }

            _configurationXMLFiles = Application.StartupPath + Constants.PathConfigFiles;

            // AME Standard preset folder
            if ((Properties.Settings.Default.WAMEPresetXMLFilesCurrentFolder == string.Empty) || (!Directory.Exists(Properties.Settings.Default.WAMEPresetXMLFilesCurrentFolder)))
            {
                Properties.Settings.Default.WAMEPresetXMLFilesCurrentFolder = Application.StartupPath + Constants.PathAMEFiles;
            }

            // AME Premium Workflow preset folder
            if ((Properties.Settings.Default.PremiumWorkflowPresetXMLFilesCurrentFolder == string.Empty) || (!Directory.Exists(Properties.Settings.Default.PremiumWorkflowPresetXMLFilesCurrentFolder)))
            {
                Properties.Settings.Default.PremiumWorkflowPresetXMLFilesCurrentFolder = Application.StartupPath + Constants.PathPremiumWorkflowFiles;
            }

            // AME Standard preset folder
            if ((Properties.Settings.Default.AMEStandardPresetXMLFilesCurrentFolder == string.Empty) || (!Directory.Exists(Properties.Settings.Default.AMEStandardPresetXMLFilesCurrentFolder)))
            {
                Properties.Settings.Default.AMEStandardPresetXMLFilesCurrentFolder = Application.StartupPath + Constants.PathAMEStdFiles;
            }

            // Default Slate Image
            if ((Properties.Settings.Default.DefaultSlateCurrentFolder == string.Empty) || (!Directory.Exists(Properties.Settings.Default.DefaultSlateCurrentFolder)))
            {
                Properties.Settings.Default.DefaultSlateCurrentFolder = Application.StartupPath + Constants.PathDefaultSlateJPG;
            }

            Program.SaveAndProtectUserConfig(); // to save settings 

            _HelpFiles = Application.StartupPath + Constants.PathHelpFiles;

            AMSLogin form = new AMSLogin();

            if (form.ShowDialog() == DialogResult.Cancel)
            {
                Environment.Exit(0);
            }

            _credentials = form.LoginCredentials;

            DisplaySplashDuringLoading = true;
            ThreadPool.QueueUserWorkItem((x) =>
            {
                using (var splashForm = new Splash(_credentials.AccountName))
                {
                    splashForm.Show();
                    while (DisplaySplashDuringLoading)
                        Application.DoEvents();
                    splashForm.Close();
                }
            });

            // Get the service context.
            _context = Program.ConnectAndGetNewContext(_credentials, true);

            // mainform title
            toolStripStatusLabelConnection.Text = String.Format("Version {0}", Assembly.GetExecutingAssembly().GetName().Version) + " - Connected to " + _context.Credentials.ClientId;

            // notification title
            notifyIcon1.Text = string.Format(notifyIcon1.Text, _context.Credentials.ClientId);

            // name of the ams acount in the title of the form - useful when several instances to navigate with icons
            this.Text = string.Format(this.Text, _context.Credentials.ClientId);

            // Let's check storage credentials
            if (string.IsNullOrEmpty(_credentials.StorageKey))
            {
                havestoragecredentials = false;
            }
            else
            {
                bool ret = Program.ConnectToStorage(_context, _credentials);
            }

            if ((GetLatestMediaProcessorByName(Constants.ZeniumEncoder) == null) && (GetLatestMediaProcessorByName(Constants.AzureMediaEncoderPremiumWorkflow) == null))
            {
                AMEPremiumWorkflowPresent = false;
                encodeAssetWithPremiumWorkflowToolStripMenuItem.Enabled = false;  //menu
                ContextMenuItemPremiumWorkflow.Enabled = false; // mouse context menu
            }

            if (GetLatestMediaProcessorByName(Constants.AzureMediaEncoderStandard) == null)
            {
                AMEStandardPresent = false;
                encodeAssetsWithAMEStandardToolStripMenuItem.Visible = false;
                encodeAssetWithAMEStandardToolStripMenuItem.Visible = false;
                toolStripSeparator35.Visible = false;
                toolStripSeparator32.Visible = false;

                // subclipping
                subclipLiveStreamsarchivesToolStripMenuItem.Visible = false;
                subclipProgramsToolStripMenuItem.Visible = false;
                subclipToolStripMenuItem.Visible = false;
            }

            if (GetLatestMediaProcessorByName(Constants.AzureMediaFaceDetector) == null)
            {
                AMFaceDetectorPresent = false;
                ProcessFaceDetectortoolStripMenuItem.Visible = false;
                toolStripMenuItemFaceDetector.Visible = false;
            }

            if (GetLatestMediaProcessorByName(Constants.AzureMediaMotionDetector) == null)
            {
                AMMotionDetectorPresent = false;
                ProcessMotionDetectortoolStripMenuItem.Visible = false;
                toolStripMenuItemMotionDetector.Visible = false;
            }

            if (GetLatestMediaProcessorByName(Constants.AzureMediaRedactor) == null)
            {
                AMRedactorPresent = false;
                ProcessRedactortoolStripMenuItem.Visible = false;
                toolStripMenuItemRedactor.Visible = false;
            }

            if (GetLatestMediaProcessorByName(Constants.AzureMediaStabilizer) == null)
            {
                AMStabilizerPresent = false;
                ProcessStabilizertoolStripMenuItem.Visible = false;
                toolStripMenuItemStabilizer.Visible = false;
            }

            // Timer Auto Refresh
            TimerAutoRefresh = new System.Timers.Timer(Properties.Settings.Default.AutoRefreshTime * 1000);
            TimerAutoRefresh.Elapsed += new ElapsedEventHandler(OnTimedEvent);

            // Let's check if there is one streaming unit running
            if (_context.StreamingEndpoints.AsEnumerable().Where(o => o.State == StreamingEndpointState.Running).ToList().Count == 0)
                TextBoxLogWriteLine("There is no streaming endpoint running in this account.", true); // Warning

            // Let's check if there is one streaming scale units
            if (_context.StreamingEndpoints.Where(o => o.ScaleUnits > 0).ToList().Count == 0)
                TextBoxLogWriteLine("There is no reserved unit streaming endpoint in this account. Dynamic packaging and live streaming output will not work.", true); // Warning

            // Let's check if there is enough streaming scale units for the channels
            double nbchannels = (double)_context.Channels.Count();
            double nbse = (double)_context.StreamingEndpoints.Where(o => o.ScaleUnits > 0).ToList().Count;
            if (nbse > 0 && nbchannels > 0 && (nbchannels / nbse) > 5)
                TextBoxLogWriteLine("There are {0} channels and {1} streaming endpoint(s). Recommandation is to provision at least 1 streaming endpoint per group of 5 channels.", nbchannels, nbse, true); // Warning

            // let's check the encoding reserved unit and type
            try
            {
                if ((_context.EncodingReservedUnits.FirstOrDefault().CurrentReservedUnits == 0) && (_context.EncodingReservedUnits.FirstOrDefault().ReservedUnitType != ReservedUnitType.Basic))
                    TextBoxLogWriteLine("There is no Media Reserved Unit (encoding will use a shared pool) but unit type is not set to S1 (Basic).", true); // Warning
            }
            catch // can occur on test account
            {
                MediaRUFeatureOn = false;
                TextBoxLogWriteLine("There is an error when accessing to the Media Reserved Units API. Some controls are disabled in the processors tab.", true); // Warning
            }

            // nb assets limits
            int nbassets = _context.Assets.Count();
            largeAccount = nbassets > triggerForLargeAccountNbAssets;
            if (largeAccount)
            {
                TextBoxLogWriteLine("This account contains a lot of assets. Some queries are disabled."); // Warning
            }
            if (nbassets > (0.75 * maxNbAssets))
            {
                TextBoxLogWriteLine("This account contains {0} assets. Warning, the limit is {1}.", nbassets, maxNbAssets, true); // Warning
            }
            // nb jobs limits
            int nbjobs = _context.Jobs.Count();
            /*
            if (nbjobs > triggerForLargeAccountNbJobs)
            {
                TextBoxLogWriteLine("This account contains a lot of jobs. Sorting is disabled."); // Warning
            }
            */
            if (nbjobs > (0.75 * maxNbJobs))
            {
                TextBoxLogWriteLine("This account contains {0} jobs. Warning, the limit is {1}.", nbjobs, maxNbJobs, true); // Warning
            }

            ApplySettingsOptions(true);
        }
        private bool EncodingRUFeatureOn = true; // On some test account, there is no Encoding RU so let's switch to OFF the feature in that case

        public Mainform()
        {
            InitializeComponent();
            this.Icon = Bitmaps.Azure_Explorer_ico;

            // USER SETTINSG CHECKS & UPDATES
            if (Properties.Settings.Default.CallUpgrade) // upgrade settings from previous version
            {
                Properties.Settings.Default.Upgrade();
                Properties.Settings.Default.CallUpgrade = false;
            }
            _configurationXMLFiles = Application.StartupPath + Constants.PathConfigFiles;

            // AME Standard preset folder
            if ((Properties.Settings.Default.WAMEPresetXMLFilesCurrentFolder == string.Empty) || (!Directory.Exists(Properties.Settings.Default.WAMEPresetXMLFilesCurrentFolder)))
            {
                Properties.Settings.Default.WAMEPresetXMLFilesCurrentFolder = Application.StartupPath + Constants.PathAMEFiles;
            }

            // AME Premium Workflow preset folder
            if ((Properties.Settings.Default.PremiumWorkflowPresetXMLFilesCurrentFolder == string.Empty) || (!Directory.Exists(Properties.Settings.Default.PremiumWorkflowPresetXMLFilesCurrentFolder)))
            {
                Properties.Settings.Default.PremiumWorkflowPresetXMLFilesCurrentFolder = Application.StartupPath + Constants.PathPremiumWorkflowFiles;
            }

            // AME Standard preset folder
            if ((Properties.Settings.Default.AMEStandardPresetXMLFilesCurrentFolder == string.Empty) || (!Directory.Exists(Properties.Settings.Default.AMEStandardPresetXMLFilesCurrentFolder)))
            {
                Properties.Settings.Default.AMEStandardPresetXMLFilesCurrentFolder = Application.StartupPath + Constants.PathAMEStdFiles;
            }

            // Default Slate Image
            if ((Properties.Settings.Default.DefaultSlateCurrentFolder == string.Empty) || (!Directory.Exists(Properties.Settings.Default.DefaultSlateCurrentFolder)))
            {
                Properties.Settings.Default.DefaultSlateCurrentFolder = Application.StartupPath + Constants.PathDefaultSlateJPG;
            }

            Program.SaveAndProtectUserConfig(); // to save settings 

            _HelpFiles = Application.StartupPath + Constants.PathHelpFiles;

            AMSLogin form = new AMSLogin();

            if (form.ShowDialog() == DialogResult.Cancel)
            {
                Environment.Exit(0);
            }

            _credentials = form.LoginCredentials;

            DisplaySplashDuringLoading = true;
            ThreadPool.QueueUserWorkItem((x) =>
            {
                using (var splashForm = new Splash(_credentials.AccountName))
                {
                    splashForm.Show();
                    while (DisplaySplashDuringLoading)
                        Application.DoEvents();
                    splashForm.Close();
                }
            });

            // Get the service context.
            _context = Program.ConnectAndGetNewContext(_credentials);

            // Dynamic filter
            _contextdynmanifest = new MediaServiceContextForDynManifest(_credentials);
            _contextdynmanifest.AccessToken = _context.Credentials.AccessToken;
            _contextdynmanifest.CheckForRedirection();

            // mainform title
            toolStripStatusLabelConnection.Text = String.Format("Version {0}", Assembly.GetExecutingAssembly().GetName().Version) + " - Connected to " + _context.Credentials.ClientId;

            // notification title
            notifyIcon1.Text = string.Format(notifyIcon1.Text, _context.Credentials.ClientId);

            // name of the ams acount in the title of the form - useful when several instances to navigate with icons
            this.Text = string.Format(this.Text, _context.Credentials.ClientId);

            // Let's check storage credentials
            if (string.IsNullOrEmpty(_credentials.StorageKey))
            {
                havestoragecredentials = false;
            }
            else
            {
                bool ret = Program.ConnectToStorage(_context, _credentials);
            }

            if ((GetLatestMediaProcessorByName(Constants.ZeniumEncoder) == null) && (GetLatestMediaProcessorByName(Constants.AzureMediaEncoderPremiumWorkflow) == null))
            {
                AMEPremiumWorkflowPresent = false;
                encodeAssetWithPremiumWorkflowToolStripMenuItem.Enabled = false;  //menu
                ContextMenuItemPremiumWorkflow.Enabled = false; // mouse context menu
            }

            if (GetLatestMediaProcessorByName(Constants.AzureMediaEncoderStandard) == null)
            {
                AMEStandardPresent = false;
                encodeAssetsWithAMEStandardToolStripMenuItem.Visible = false;
                encodeAssetWithAMEStandardToolStripMenuItem.Visible = false;
                toolStripSeparator35.Visible = false;
                toolStripSeparator32.Visible = false;
            }


            if (GetLatestMediaProcessorByName(Constants.AzureMediaHyperlapse) == null)
            {
                HyperlapsePresent = false;
                processAssetsWithHyperlapseToolStripMenuItem.Enabled = false;
                processAssetsWithHyperlapseToolStripMenuItem1.Enabled = false;
            }

            // Timer Auto Refresh
            TimerAutoRefresh = new System.Timers.Timer(Properties.Settings.Default.AutoRefreshTime * 1000);
            TimerAutoRefresh.Elapsed += new ElapsedEventHandler(OnTimedEvent);

            // Let's check if there is one streaming unit running
            if (_context.StreamingEndpoints.AsEnumerable().Where(o => o.State == StreamingEndpointState.Running).ToList().Count == 0)
                TextBoxLogWriteLine("There is no streaming endpoint running in this account.", true); // Warning

            // Let's check if there is one streaming scale units
            if (_context.StreamingEndpoints.Where(o => o.ScaleUnits > 0).ToList().Count == 0)
                TextBoxLogWriteLine("There is no reserved unit streaming endpoint in this account. Dynamic packaging and live streaming output will not work.", true); // Warning

            // Let's check if there is enough streaming scale units for the channels
            double nbchannels = (double)_context.Channels.Count();
            double nbse = (double)_context.StreamingEndpoints.Where(o => o.ScaleUnits > 0).ToList().Count;
            if (nbse > 0 && nbchannels > 0 && (nbchannels / nbse) > 5)
                TextBoxLogWriteLine("There are {0} channels and {1} streaming endpoint(s). Recommandation is to provision at least 1 streaming endpoint per group of 5 channels.", nbchannels, nbse, true); // Warning

            // let's check the encoding reserved unit and type
            try
            {
                if ((_context.EncodingReservedUnits.FirstOrDefault().CurrentReservedUnits == 0) && (_context.EncodingReservedUnits.FirstOrDefault().ReservedUnitType != ReservedUnitType.Basic))
                    TextBoxLogWriteLine("There is no reserved encoding unit (encoding will use a shared pool) but unit type is not set to BASIC.", true); // Warning
            }
            catch // can occur on test account
            {
                EncodingRUFeatureOn = false;
                TextBoxLogWriteLine("There is an error when accessing to the Encoding Reserved Units API. Some controls are disabled in the processors tab.", true); // Warning
            }
            ApplySettingsOptions(true);
        }