private void ComboBoxProgramsSelectedIndexChanged(object sender, EventArgs e)
        {
            ProgramEntry programToCheck;

            if (comboBoxPrograms.SelectedIndex != -1)
            {
                programToCheck = SqlCommunication.CheckProgram(comboBoxPrograms.SelectedItem.ToString());
            }
            else
            {
                MessageBox.Show("No program selected", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (programToCheck.Name != null)
            {
                triggerOffset      = programToCheck.TriggerOffset;
                startWastingOffset = programToCheck.StartWastingOffset;
                wasteOffset        = programToCheck.WasteOffset;

                logo1Tolerance = 1 - programToCheck.Logo1Camera1Confidence;
                logo2Tolerance = 1 - programToCheck.Logo1Camera2Confidence;

                machine = programToCheck.Machine;
            }
        }
        private void ButtonOkClick(object sender, EventArgs e)
        {
            string programName = textBoxProgramName.Text.Replace(' ', '_');

            if (programName == string.Empty)
            {
                MessageBox.Show("Program name has not been entered", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ProgramEntry programToCheck = SqlCommunication.CheckProgram(programName);

            if (programToCheck.Name == null)
            {
                manageProgramsForm.AddProgramToComboBox(programName);

                Close();
            }
            else
            {
                MessageBox.Show("Program already exists", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
        public ProgramEntry CheckProgram()
        {
            ProgramEntry programEntry = SqlCommunication.CheckProgram(scanValue);

            return(programEntry);
        }
        private void ButtonOkClick(object sender, EventArgs e)
        {
            ProgramEntry programToCheck;

            string shapeTypeCamera1 = string.Empty;
            string shapeTypeCamera2 = string.Empty;

            if (comboBoxPrograms.SelectedIndex != -1)
            {
                programToCheck = SqlCommunication.CheckProgram(comboBoxPrograms.SelectedItem.ToString());
            }
            else
            {
                MessageBox.Show("No program selected", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (startWastingOffset == 0 || startWastingOffset == 0 || wasteOffset == 0 || logo1Tolerance == 0 || logo2Tolerance == 0 || machine == string.Empty)
            {
                MessageBox.Show("Product configuration values not set", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!Directory.Exists(Directory.GetCurrentDirectory() + $"\\Programs\\{comboBoxPrograms.SelectedItem}"))
            {
                Directory.CreateDirectory(Directory.GetCurrentDirectory() + $"\\Programs\\{comboBoxPrograms.SelectedItem}");
            }

            if (!checkBoxCamera1.Checked && !checkBoxCamera2.Checked)
            {
                MessageBox.Show("At least one camera must be activated", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (checkBoxCamera1.Checked)
            {
                if (comboBoxColorsCamera1.SelectedIndex == -1)
                {
                    MessageBox.Show("No color selected for camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (checkBoxLogoCamera1.Checked)
                {
                    if (numericUpDownLogosCamera1.Value == 0)
                    {
                        MessageBox.Show("Camera 1 logos activated but no logo set", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else if (numericUpDownLogosCamera1.Value == 1)
                    {
                        if (logo1Camera1RegionCoords[0] == 0 && logo1Camera1RegionCoords[1] == 0 && logo1Camera1RegionCoords[2] == 0 && logo1Camera1RegionCoords[3] == 0)
                        {
                            MessageBox.Show("Logo 1 region not set for camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    else if (numericUpDownLogosCamera1.Value == 2)
                    {
                        if (logo1Camera1RegionCoords[0] == 0 && logo1Camera1RegionCoords[1] == 0 && logo1Camera1RegionCoords[2] == 0 && logo1Camera1RegionCoords[3] == 0)
                        {
                            MessageBox.Show("Logo 1 region not set for camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        if (logo2Camera1RegionCoords[0] == 0 && logo2Camera1RegionCoords[1] == 0 && logo2Camera1RegionCoords[2] == 0 && logo2Camera1RegionCoords[3] == 0)
                        {
                            MessageBox.Show("Logo 2 region not set for camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }

                    if (!Directory.Exists(Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera1"))
                    {
                        Directory.CreateDirectory(Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera1");
                    }
                    HOperatorSet.RotateImage(hoImageCamera1Rotated, out HObject hImageRotatedCamera1, -90, "constant");

                    HalconProcedures.CreateLogos(hImageRotatedCamera1,
                                                 Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera1\Logo1.shm",
                                                 Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera1\Logo2.shm",
                                                 comboBoxColorsCamera1.SelectedItem.ToString().ToLower(),
                                                 Convert.ToInt32(numericUpDownLogosCamera1.Value),
                                                 logo1Camera1RegionCoords[0],
                                                 logo1Camera1RegionCoords[1],
                                                 logo1Camera1RegionCoords[2],
                                                 logo1Camera1RegionCoords[3],
                                                 logo2Camera1RegionCoords[0],
                                                 logo2Camera1RegionCoords[1],
                                                 logo2Camera1RegionCoords[2],
                                                 logo2Camera1RegionCoords[3]);
                }

                if (checkBoxShapeCamera1.Checked)
                {
                    if (comboBoxShapeTypeCamera1.SelectedIndex == -1)
                    {
                        MessageBox.Show("No shape type selected for camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        shapeTypeCamera1 = comboBoxShapeTypeCamera1.SelectedItem.ToString();
                    }
                }

                if (textBoxOcrCamera1.Text == string.Empty)
                {
                    MessageBox.Show("No OCR text for camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            if (checkBoxCamera2.Checked)
            {
                if (comboBoxColorsCamera2.SelectedIndex == -1)
                {
                    MessageBox.Show("No color selected for camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (checkBoxLogoCamera2.Checked)
                {
                    if (numericUpDownLogosCamera2.Value == 0)
                    {
                        MessageBox.Show("Camera 2 logos activated but no logo set", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else if (numericUpDownLogosCamera2.Value == 1)
                    {
                        if (logo1Camera2RegionCoords[0] == 0 && logo1Camera2RegionCoords[1] == 0 && logo1Camera2RegionCoords[2] == 0 && logo1Camera2RegionCoords[3] == 0)
                        {
                            MessageBox.Show("Logo 1 region not set for camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    else if (numericUpDownLogosCamera2.Value == 2)
                    {
                        if (logo1Camera2RegionCoords[0] == 0 && logo1Camera2RegionCoords[1] == 0 && logo1Camera2RegionCoords[2] == 0 && logo1Camera2RegionCoords[3] == 0)
                        {
                            MessageBox.Show("Logo 1 region not set for camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        if (logo2Camera2RegionCoords[0] == 0 && logo2Camera2RegionCoords[1] == 0 && logo2Camera2RegionCoords[2] == 0 && logo2Camera2RegionCoords[3] == 0)
                        {
                            MessageBox.Show("Logo 2 region not set for camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }

                    if (!Directory.Exists(Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera2"))
                    {
                        Directory.CreateDirectory(Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera2");
                    }

                    HOperatorSet.RotateImage(hoImageCamera2Rotated, out HObject hImageRotatedCamera2, -90, "constant");

                    HalconProcedures.CreateLogos(hImageRotatedCamera2,
                                                 Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera2\Logo1.shm",
                                                 Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera2\Logo2.shm",
                                                 comboBoxColorsCamera2.SelectedItem.ToString().ToLower(),
                                                 Convert.ToInt32(numericUpDownLogosCamera2.Value),
                                                 logo1Camera2RegionCoords[0],
                                                 logo1Camera2RegionCoords[1],
                                                 logo1Camera2RegionCoords[2],
                                                 logo1Camera2RegionCoords[3],
                                                 logo2Camera2RegionCoords[0],
                                                 logo2Camera2RegionCoords[1],
                                                 logo2Camera2RegionCoords[2],
                                                 logo2Camera2RegionCoords[3]);
                }

                if (checkBoxShapeCamera2.Checked)
                {
                    if (comboBoxShapeTypeCamera2.SelectedIndex == -1)
                    {
                        MessageBox.Show("No shape type selected for camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        shapeTypeCamera2 = comboBoxShapeTypeCamera2.SelectedItem.ToString();
                    }
                }

                if (textBoxOcrCamera2.Text == string.Empty)
                {
                    MessageBox.Show("No OCR text for camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            if (programToCheck.Name == null)
            {
                string colorCamera1 = string.Empty;
                string colorCamera2 = string.Empty;

                if (comboBoxColorsCamera1.SelectedIndex != -1)
                {
                    colorCamera1 = comboBoxColorsCamera1.SelectedItem.ToString().ToLower();
                }

                if (comboBoxColorsCamera2.SelectedIndex != -1)
                {
                    colorCamera2 = comboBoxColorsCamera2.SelectedItem.ToString().ToLower();
                }

                SqlCommunication.InsertProgram(comboBoxPrograms.SelectedItem.ToString(),
                                               triggerOffset,
                                               startWastingOffset,
                                               wasteOffset,
                                               colorCamera1,
                                               colorCamera2,
                                               checkBoxCamera1.Checked,
                                               checkBoxCamera2.Checked,
                                               checkBoxLogoCamera1.Checked,
                                               checkBoxLogoCamera2.Checked,
                                               Convert.ToInt32(numericUpDownLogosCamera1.Value),
                                               Convert.ToInt32(numericUpDownLogosCamera2.Value),
                                               $"C:\\Hirschmann\\Hirschmann\\bin\\Debug\\Programs\\{comboBoxPrograms.SelectedItem}\\Camera1\\Logo1.shm".Replace('\\', ' '),
                                               $"C:\\Hirschmann\\Hirschmann\\bin\\Debug\\Programs\\{comboBoxPrograms.SelectedItem}\\Camera1\\Logo2.shm".Replace('\\', ' '),
                                               $"C:\\Hirschmann\\Hirschmann\\bin\\Debug\\Programs\\{comboBoxPrograms.SelectedItem}\\Camera2\\Logo1.shm".Replace('\\', ' '),
                                               $"C:\\Hirschmann\\Hirschmann\\bin\\Debug\\Programs\\{comboBoxPrograms.SelectedItem}\\Camera2\\Logo2.shm".Replace('\\', ' '),
                                               checkBoxShapeCamera1.Checked,
                                               checkBoxShapeCamera2.Checked,
                                               shapeTypeCamera1.ToLower(),
                                               shapeTypeCamera2.ToLower(),
                                               textBoxOcrCamera1.Text.Replace(" ", string.Empty),
                                               textBoxOcrCamera2.Text.Replace(" ", string.Empty)
                                               );

                AddLog($"Manage Programs: Program {comboBoxPrograms.SelectedItem.ToString()} created");
            }
            else
            {
                string colorCamera1 = string.Empty;
                string colorCamera2 = string.Empty;

                if (comboBoxColorsCamera1.SelectedIndex != -1)
                {
                    colorCamera1 = comboBoxColorsCamera1.SelectedItem.ToString().ToLower();
                }

                if (comboBoxColorsCamera2.SelectedIndex != -1)
                {
                    colorCamera2 = comboBoxColorsCamera2.SelectedItem.ToString().ToLower();
                }

                SqlCommunication.UpdateProgramManageProgramsForm(comboBoxPrograms.SelectedItem.ToString(),
                                                                 triggerOffset,
                                                                 startWastingOffset,
                                                                 wasteOffset,
                                                                 colorCamera1,
                                                                 colorCamera2,
                                                                 checkBoxCamera1.Checked,
                                                                 checkBoxCamera2.Checked,
                                                                 checkBoxLogoCamera1.Checked,
                                                                 checkBoxLogoCamera2.Checked,
                                                                 Convert.ToInt32(numericUpDownLogosCamera1.Value),
                                                                 Convert.ToInt32(numericUpDownLogosCamera2.Value),
                                                                 checkBoxShapeCamera1.Checked,
                                                                 checkBoxShapeCamera2.Checked,
                                                                 shapeTypeCamera1.ToLower(),
                                                                 shapeTypeCamera2.ToLower(),
                                                                 textBoxOcrCamera1.Text.Replace(" ", string.Empty),
                                                                 textBoxOcrCamera2.Text.Replace(" ", string.Empty)
                                                                 );

                AddLog($"Manage Programs: Program {comboBoxPrograms.SelectedItem.ToString()} updated");
            }

            Close();
        }