using System.Reflection;
using Inspectron.Settings.WindowsWizard.Interfaces;

namespace Inspectron.Settings.WindowsWizard.Controls
{
    public partial class EnumEditor : UserControl,ISetupItem
    {
        private PropertyInfo _prp;
        private object _obj;

        public EnumEditor()
        {
            InitializeComponent();
        }

        public void SetProperty(object obj, string propertyName)
        {
            GroupBox gb = new GroupBox();
            gb.Text = PropHelper.SplitCamelCase(propertyName);
            this.Controls.Add(gb);
            gb.Dock = DockStyle.Fill;
            FlowLayoutPanel flp = new FlowLayoutPanel();

            gb.Controls.Add(flp);
            flp.Dock = DockStyle.Fill;
            _prp = PropHelper.GetPrp(obj, propertyName);
            _obj = obj;
            var names = Enum.GetNames(_prp.PropertyType);
            var values = Enum.GetValues(_prp.PropertyType);
            var curValue = _prp.GetValue(_obj);
            int cnt = 0;
            foreach (string name in names)
            {
                var rb = new RadioButton() {Text = name};
                var cnt1 = cnt;
                rb.CheckedChanged += (sender, args) =>
                {
                    if (rb.Checked) _prp.SetValue(_obj, values.GetValue(cnt1));
                };
                flp.Controls.Add(rb);
                if ((int)curValue == (int)values.GetValue(cnt1))
                {
                    rb.Checked = true;
                }
                cnt++;
            }
            this.Controls.Add(gb);
        }
    }
}
