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

namespace NotepadEnhanced.Refactexing
{
    class RegexOptionsCheckedListBox : CheckedListBox
    {
        public RegexOptionsCheckedListBox()
        {
            Items.Clear();
            Items.Add(RegexOptions.IgnorePatternWhitespace);
            Items.Add(RegexOptions.ExplicitCapture);
            Items.Add(RegexOptions.CultureInvariant);
            Items.Add(RegexOptions.IgnoreCase);
            Items.Add(RegexOptions.RightToLeft);
            Items.Add(RegexOptions.Multiline);
            Items.Add(RegexOptions.Singleline);
        }

        /// <summary>
        /// Uncheck all checkboxes
        /// </summary>
        public void UncheckAll()
        {
            for (int i = 0; i < this.Items.Count; i++)
                this.SetItemChecked(i, false);
        }

        public void SetItemChecked(RegexOptions option)
        {
            for (int i = 0; i < Items.Count; i++)
            {
                RegexOptions itemOp = (RegexOptions) Items[i];

                if (option == itemOp)
                {
                    SetItemChecked(i, true);
                    break;
                }
            }
        }

        /// <summary>
        /// Gets or sets the checked regex options
        /// </summary>
        [Browsable(false)]
        public RegexOptions Options
        {
            get
            {
                var options = RegexOptions.None;

                foreach (var item in CheckedItems)
                {
                    options |= (RegexOptions)item;
                }

                return options;
            }
            set
            {
                UncheckAll();

                var vals = Enum.GetValues(value.GetType());

                foreach (var val in vals)
                {
                    RegexOptions option = (RegexOptions) val;

                    if (value.HasFlag(option))
                    {
                        SetItemChecked(option);
                    }
                }
            }
        }
    }
}
