using System.Reflection;
using Inspectron.Settings.WindowsWizard.Interfaces;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace Inspectron.Settings.WindowsWizard.Controls
{
    public partial class PathEditor : UserControl,ISetupItem
    {
        
        private PropertyInfo _prp;
        private object _obj;
        public PathEditor() 
        {
            InitializeComponent();
        }

        public bool FolderPick { get; set; }

        public string SelectedFolderPath
        {
            get => textBox1.Text;
            set
            {
                if (_obj == null) return;
                textBox1.Text = value;
                _prp.SetValue(_obj,value);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var dialog = new CommonOpenFileDialog();
            dialog.IsFolderPicker = FolderPick;
            dialog.InitialDirectory = SelectedFolderPath;
            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                SelectedFolderPath = dialog.FileName;
                FileChanged();
            }
        }

        public event Action FileChanged = delegate { };
        public void SetProperty(object obj, string propertyName)
        {
            label1.Text = PropHelper.SplitCamelCase(propertyName);
            _prp = PropHelper.GetPrp(obj, propertyName);
            _obj = obj;
            SelectedFolderPath =(string)_prp.GetValue(obj);
            if(SelectedFolderPath!=null) FileChanged();
        }

        public void HidePath()
        {
            textBox1.Visible = false;
        }
    }
}
