using System.ComponentModel;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Baza_Reklam.Classes.Helpers
{
    public class ValidationHelper
    {
        //public static bool FormIsValid(Form form)
        //{
        //    bool valid = true;
        //    Validate(form, form.Controls, valid);
        //    form.Focus();
        //    return form.Validate();
        //}

        //public static bool FormIsValid(Form form, Control topLevelControl)
        //{
        //    bool valid = true;
        //    Validate(form, topLevelControl.Controls, valid);
        //    form.Focus();
        //    return form.Validate();
        //}

        //public  static  void  Validate(Form form, Control.ControlCollection controls, bool valid)
        //{
        //    foreach (Control control in controls)
        //    {
        //        if (control is RadioButton) continue;
        //        control.Focus();
        //        valid = form.Validate();
        //        if (control is TabControl)
        //        {
        //            TabControl tabControl = (TabControl) control;
        //            int index = tabControl.SelectedIndex;
        //            foreach (TabPage tabPage in tabControl.TabPages)
        //            {
        //                tabControl.SelectedTab = tabPage;
        //                Validate(form, tabPage.Controls, valid);
        //            }
        //            tabControl.SelectedIndex = index;

        //        }
        //        else if (control.HasChildren)
        //        {
        //            Validate(form, control.Controls, valid);
        //        }

        //    }
        //}

        public static bool IsTextBoxEmpty(TextBox textBox, string errorMessage, CancelEventArgs cancelEventArgs, ErrorProvider errorProvider)
        {
            StringBuilder sb = new StringBuilder();
            if (string.IsNullOrEmpty(textBox.Text))
            {
                cancelEventArgs.Cancel = true;
                sb.Append(errorMessage);
            }
            else
                cancelEventArgs.Cancel = false;
            errorProvider.SetError(textBox, sb.ToString());
            return cancelEventArgs.Cancel;
        }

        public static bool IsTextBoxValid(TextBox textBox, string regex, string errorMessage, CancelEventArgs cancelEventArgs, ErrorProvider errorProvider)
        {
            StringBuilder sb = new StringBuilder();

            if (!Regex.IsMatch(textBox.Text, regex, RegexOptions.IgnoreCase))
            {
                cancelEventArgs.Cancel = true;
                sb.Append(errorMessage);
            }
            else
                cancelEventArgs.Cancel = false;
            errorProvider.SetError(textBox, sb.ToString());
            return cancelEventArgs.Cancel;
        }

        public static bool IsComboValueSelected(ComboBox comboBox, string errorMessage, CancelEventArgs cancelEventArgs, ErrorProvider errorProvider)
        {
            StringBuilder sb = new StringBuilder();
            if (comboBox.SelectedIndex < 0 || comboBox.SelectedItem == null)
            {
                cancelEventArgs.Cancel = true;
                sb.Append(errorMessage);
            }
            else
                cancelEventArgs.Cancel = false;

            errorProvider.SetError(comboBox, sb.ToString());
            return cancelEventArgs.Cancel;
        }

	        public static bool IsComboValueSelected(ComboBox comboBox, int defaultIndex, string errorMessage, CancelEventArgs cancelEventArgs, ErrorProvider errorProvider)
	        {
	            StringBuilder sb = new StringBuilder();
	            if (comboBox.SelectedIndex < 0 || comboBox.SelectedIndex == defaultIndex || comboBox.SelectedItem == null)
	            {
	                cancelEventArgs.Cancel = true;
	                sb.Append(errorMessage);
	            }
	            else
	                cancelEventArgs.Cancel = false;
	
	            errorProvider.SetError(comboBox, sb.ToString());
	            return cancelEventArgs.Cancel;
	        }
    }
}