/// <summary>
        /// Metoda wyolywana w odpowiedzi na klikniecie "ustawien"
        /// Otwiera nowe okno
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Settings_OnClick(object sender, RoutedEventArgs e)
        {
            Window_Settings wdswSettings = new Window_Settings(ApplicationProfile, lcm);

            wdswSettings.ShowDialog();

            if (wdswSettings.needsSettingsUpdate)                                                                                                                                          // Jesli zaszly zmiany
            {
                this.ApplicationProfile = wdswSettings.Get_New_Settings();                                                                                                                 // Zapisujemy je do zmiennej
                Stop_Data_Aqquire       = true;                                                                                                                                            // Zmieniamy flage aby Ambi nie wysylalo danych
                channelsManagement      = new ChannelsManagement(ApplicationProfile.ChannelSettings, (int)SystemParameters.PrimaryScreenHeight, (int)SystemParameters.PrimaryScreenWidth); // Ustawienia
                Stop_Data_Aqquire       = false;                                                                                                                                           // Zmieniamy flage na nowo
            }

            if (this.ApplicationProfile.BaudRate != serialDeviceController.Baud_rate)
            {
                Stop_Data_Aqquire      = true;                                  // Upewniamy sie ze nie bedzie wysylania danych
                serialDeviceController = ApplicationProfile.LastCOMconnected == "" ? new DeviceController(ApplicationProfile.BaudRate) : new DeviceController(ApplicationProfile.BaudRate, ApplicationProfile.LastCOMconnected);
                Stop_Data_Aqquire      = false;                                 // Flaga zmieniona - kontroler COM zajmie sie sprawa nie wysylania
            }
        }
        /// <summary>
        /// Glowne okno zaladowane
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            // [1] - Check License
            if (!isLicenseFilePresent)
            {
                TextBox_StatusBar_Bottom.Text = "No license present";
            }
            else
            {
                try
                {
                    lcm.LoadLicenseDetails();
                    TextBox_StatusBar_Bottom.Text = String.Format("Registered to {0} {1}", lcm.GetByKeyName("FirstName"),
                                                                  lcm.GetByKeyName("LastName"));
                }
                catch
                {
                    // ignored
                }
            }


            // [3] - Load profile
            ApplicationProfile = AppProfileManager.Load_ApplicationProfile();      // Try to load the profile if does not exists
            // [3a] - See if we should be minimized
            if (ApplicationProfile.Autostart)
            {
                this.WindowState = WindowState.Minimized;                               // This starts minimized app if we choosed to AutoStart application
            }
            // [3b] - Assign proper value to brightness slider
            Slider_Brightness.Value = ApplicationProfile.ChannelSettings.Brightness;
            // [4] - Configure COM communications
            serialDeviceController = ApplicationProfile.LastCOMconnected == "" ? new DeviceController(ApplicationProfile.BaudRate) : new DeviceController(ApplicationProfile.BaudRate, ApplicationProfile.LastCOMconnected);
            // [4a] - Subscribe to event
            serialDeviceController.OnCOMdisconnected += (o, args) =>
            {
                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
                {
                    ToggleButton_Start.Content = "Start";                      // Zmieniamy napis na guziku
                    isRGBdataAqquireRunning    = false;                        // Zmieniamy status naszej flagi
                    Stop_Data_Aqquire          = true;                         // Zmieniamy flage aby Ambi nie wysylalo danych

                    TextBlock_StatusBar_1.Text = " Disconnected";
                }));
            };

            // [5] - Ustaw podstawowe ustawienia
            channelsManagement = new ChannelsManagement(ApplicationProfile.ChannelSettings, (int)SystemParameters.PrimaryScreenHeight, (int)SystemParameters.PrimaryScreenWidth);    // Ustawienia
            // [6] - Konfiguracja TASKa dla chwytania danych z ekranu
            #region Task - lapanie danych
            tsk_RGBdata = new Task(() =>
            {
                while (true)
                {
                    if (!(Stop_Data_Aqquire))
                    {
                        if (serialDeviceController.DeviceConnected)
                        {
                            // Funkcje sluzace do zlapania ekranu
                            Surface s = rgbManagement.CaptureScreen(
                                (int)SystemParameters.PrimaryScreenWidth,
                                (int)SystemParameters.PrimaryScreenHeight
                                );
                            DataRectangle dr = s.LockRectangle(LockFlags.None);
                            DataStream gs    = dr.Data;

                            foreach (LightChannel channel in channelsManagement.LightChannels)
                            {
                                //TODO: Sprawdzamy brightness
                                //rgbManagement.AverageScreenRGBfromChannel(gs, channel, channelsManagement.Pixel_Min_Treshold);

                                try
                                {
                                    rgbManagement.AverageScreen(gs, channel, channelsManagement.Pixel_Min_Treshold,
                                                                ApplicationProfile.ChannelSettings.Brightness);
                                }
                                catch
                                {
                                }
                                //TODO : Ta funkcja zostanie "fade out"

                                //if (channelsManagement.Average_Strong_Colors)
                                //{

                                //    //RgBcapture.AdjustAverageRGB(channel, channelsManagement.Average_Strong_Colors_Adjustment);
                                //}
                            }

                            serialDeviceController.Send_Data_To_Serial(new byte[1] {
                                0x01
                            });                                                                                                            // wypychamy dane na pasek

                            serialDeviceController.Send_Data_To_Serial(rgbManagement.CalculateRGBarray(channelsManagement.LightChannels)); // wypychamy dane na pasek

                            serialDeviceController.Send_Data_To_Serial(new byte[1] {
                                0x02
                            });                  // wypychamy dane na pasek

                            s.UnlockRectangle(); // <---------------------  UNLOCK !!!
                            s.Dispose();
                        }
                    }
                }
            });
            #endregion
            // [4b] - See if we must AutoRun our process :D
            if (ApplicationProfile.AutoRun)
            {
                ToggleButton_Start.IsEnabled = !ToggleButton_Start.IsEnabled;   // Toggle button state
                Action_Start();                                                 // Start Ambi
            }
            // [7] Tray configs
        }