using Inspectron.Settings.WindowsWizard.Enums;
using Inspectron.Settings.WindowsWizard.Interfaces;

namespace Inspectron.Settings.WindowsWizard
{
    public partial class SetupPage : Form
    {
        public SetupPage()
        {
            InitializeComponent();
        }

        public SetupPage PreviousPage { get; set; }
        public SetupPage NextPage { get; set; }
        public FlowLayoutPanel CurrentLayout { get; set; }
        
        public static SetupPage Begin(string tabName,string title=null,Func<bool> expertModeCheck=null)
        {
            
            var sp = new SetupPage();
            if (title != null)
            {
                sp.Text = title;
            }
            sp.ExpertModeCheck = expertModeCheck;
            if (sp.ExpertModeCheck == null)
            {
                sp.checkBox1.Visible = false;
            }
            TabPage tp = new TabPage();
            tp.Text = tabName;
            FlowLayoutPanel panel = new FlowLayoutPanel();
            panel.Dock = DockStyle.Fill;
            panel.FlowDirection = FlowDirection.TopDown;
            tp.Controls.Add(panel);
            sp.tabControl1.TabPages.Add(tp);
            sp.CurrentLayout = panel;
            return sp;
        }

        public Func<bool> ExpertModeCheck { get; set; }
        List<TabPage> _expertPages=new List<TabPage>();
        public SetupPage Next(string tabName,bool forExpert=false)
        {
            TabPage tp = new TabPage();
            tp.Text = tabName;
            
            FlowLayoutPanel panel = new FlowLayoutPanel();
            panel.Dock = DockStyle.Fill;
            panel.FlowDirection = FlowDirection.TopDown;
            tp.Controls.Add(panel);
            
            
            if (forExpert)
            {
                _expertPages.Add(tp);
            }
            else
            {
                this.tabControl1.TabPages.Add(tp);
            }
            this.CurrentLayout = panel;

            return this;
        }

        public static string SplitCamelCase(string input)
        {
            return System.Text.RegularExpressions.Regex.Replace(input, "([A-Z])", " $1", System.Text.RegularExpressions.RegexOptions.Compiled).Trim();
        }

        public ESetupResult SetupResult { get; private set; } = ESetupResult.Cancel;

        public void ShowSetup()
        {
            SetupPage start = this;
            while (start.PreviousPage!=null)
            {
                start = start.PreviousPage;
            }

            start.Show();
        }
        public void ShowSetupDialog()
        {
            SetupPage start = this;
            while (start.PreviousPage != null)
            {
                start = start.PreviousPage;
            }

            start.ShowDialog();
        }
        public SetupPage FinishWith(Action setupFinished)
        {
            this.SetupFinished = setupFinished;
            return this;
        }

        public Action SetupFinished { get; set; }

        public SetupPage AddItem(ISetupItem item)
        {
            CurrentLayout.Controls.Add(item as UserControl);
            return this;
        }

        public SetupPage AddItem<T>(object obj, string prp,Action<T> itemAction=null) where T:ISetupItem,new()
        {
            T pe = new T();
            itemAction?.Invoke(pe);
            pe.SetProperty(obj, prp);
            (pe as UserControl).Width = 500;
            
            this.AddItem(pe);
            return this;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            SetupFinished?.Invoke();
            this.Close();



        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                if ((ExpertModeCheck?.Invoke() ?? true))
                {
                    foreach (TabPage page in _expertPages)
                    {
                        this.tabControl1.TabPages.Add(page);
                    }
                }
                else
                {
                    checkBox1.Checked = false;
                }
                
            }
            else
            {
                foreach (TabPage page in _expertPages)
                {
                    this.tabControl1.TabPages.Remove(page);
                    
                }
            }
        }
    }
}
