using System;
using System.Globalization;
using System.Windows.Forms;
using FastReport.Utils;
using FastReport.Design;
using System.IO;
#if !MONO
using FastReport.Design.Toolbars;
using FastReport.DevComponents.DotNetBar;
#endif

namespace FastReport.Forms
{
    internal partial class PluginsOptions : DesignerOptionsPage
    {
        private Designer designer;

        private void Localize()
        {
            MyRes res = new MyRes("Forms,UIOptions");
            tab1.Text = res.Get("");
            lblUIStyle.Text = res.Get("Appearance");
            cbxUIStyle.Items.AddRange(UIStyleUtils.UIStyleNames);
            lblIconPack.Text = res.Get("IconPack");
            cbxIconPack.Items.Add(res.Get("IconPack,Pack1"));
            cbxIconPack.Items.Add(res.Get("IconPack,Pack2"));
            cbRibbon.Text = res.Get("Ribbon");
            cbWelcome.Text = res.Get("Welcome");
            cbDisableHotkeys.Text = res.Get("DisableHotkeys");
            lblRestart.Text = res.Get("Restart");
            btnResetConfig.Text = res.Get("Reset");
            res = new MyRes("Forms,PluginsOptions");
            tab2.Text = res.Get("");
            lblHint.Text = res.Get("Hint");
            btnAdd.Text = res.Get("Add");
            btnRemove.Text = res.Get("Remove");
            lblNote.Text = res.Get("Note");
            btnFonts.Text = res.Get("Fonts");
            btnExportsMenuEdit.Text = res.Get("ExportsEditor");

            res = new MyRes("Forms,UIOptions,RightToLeft");
            lblRightToLeft.Text = res.Get("");
            cbxRightToLeft.Items.Add(res.Get("Auto"));
            cbxRightToLeft.Items.Add(res.Get("No"));
            cbxRightToLeft.Items.Add(res.Get("Yes"));
            cbxRightToLeft.SelectedIndex = 0;
        }

        public override void UpdateDpiDependencies()
        {
            base.UpdateDpiDependencies();
            btnUp.Image = this.GetImage(208);
            btnDown.Image = this.GetImage(209);
        }

        private string GetToolTipText(string location)
        {
            if (location == null || !File.Exists(location)) return "";


            FileInfo info = new FileInfo(location);
            var versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(location);

            return info.Name + "\n" +
                    Res.Get("Forms,PluginsOptions,Description") + " " +
                    versionInfo.FileDescription + "\n" +
                    Res.Get("Forms,PluginsOptions,CompanyName") + " " +
                    versionInfo.CompanyName + "\n" +
                    Res.Get("Forms,PluginsOptions,Version") + " " +
                    versionInfo.FileVersion + "\n" +
                    Res.Get("Forms,PluginsOptions,Date") + " " + info.CreationTime.ToString() + "\n" +
                    Res.Get("Forms,PluginsOptions,Size") + " " + FileSize.ConvertToString(info.Length) + "\n" +
                    Res.Get("Forms,PluginsOptions,Path") + "\n" + location;
        }

        private void AddPlugin(string name)
        {
            dgPlugins.Rows.Add(this.GetImage(89), Path.GetFileName(name));
            string toolTip = GetToolTipText(name);

            dgPlugins.Rows[dgPlugins.Rows.Count - 1].Cells[1].Tag = name;
            dgPlugins.Rows[dgPlugins.Rows.Count - 1].Cells[0].ToolTipText = toolTip;
            dgPlugins.Rows[dgPlugins.Rows.Count - 1].Cells[1].ToolTipText = toolTip;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Filter = Res.Get("FileFilters,Assembly");
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    AddPlugin(dialog.FileName);
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (dgPlugins.SelectedRows.Count == 0)
                return;
            dgPlugins.Rows.Remove(dgPlugins.SelectedRows[0]);
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            if (dgPlugins.SelectedRows.Count == 0)
                return;

            DataGridViewRow row = dgPlugins.SelectedRows[0];
            int index = row.Index;
            if (index > 0)
            {
                dgPlugins.Rows.Remove(row);
                dgPlugins.Rows.Insert(index - 1, row);
                row.Selected = true;
            }
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            if (dgPlugins.SelectedRows.Count == 0)
                return;

            DataGridViewRow row = dgPlugins.SelectedRows[0];
            int index = row.Index;
            if (index < dgPlugins.Rows.Count - 1)
            {
                dgPlugins.Rows.Remove(row);
                dgPlugins.Rows.Insert(index + 1, row);
                row.Selected = true;
            }

        }

        private void dgPlugins_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool enabled = dgPlugins.SelectedRows.Count != 0;
            btnRemove.Enabled = enabled;
            btnUp.Enabled = enabled;
            btnDown.Enabled = enabled;
        }

        private void btnFonts_Click(object sender, EventArgs e)
        {
            using (FormsFonts fontsForm = new FormsFonts())
            {
                if (fontsForm.ShowDialog() == DialogResult.OK)
                {
#if !MONO
                    designer.ActiveReportTab.Editor.UpdateFont();
                    designer.ActiveReportTab.FRXEditor.UpdateFont();
#endif
                }
            }
        }


        private void BtnExportsMenuEdit_Click(object sender, EventArgs e)
        {
#if !COMMUNITY
            using (ExportsOptionsEditorForm exportsEditorForm = new ExportsOptionsEditorForm())
            {
                exportsEditorForm.ShowDialog();
            }
#endif
        }

        private void cbxRightToLeft_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblRestart.Visible = true;
        }

        private void btnResetConfig_Click(object sender, EventArgs e)
        {
            lblRestart.Visible = true;
            Config.CleanupOnExit = true;
        }

        private void SetConfigRightToLeft()
        {
            switch (cbxRightToLeft.SelectedIndex)
            {
                case 0:
                    Config.RightToLeft = CultureInfo.CurrentCulture.TextInfo.IsRightToLeft;
                    break;
                case 1:
                    Config.RightToLeft = false;
                    break;
                case 2:
                    Config.RightToLeft = true;
                    break;
                default:
                    Config.RightToLeft = false;
                    break;
            }
        }

        private void LoadRightToLeft()
        {
            XmlItem uiOptions = Config.Root.FindItem("UIOptions");
            string rtl = uiOptions.GetProp("RightToLeft");

            if (!String.IsNullOrEmpty(rtl))
            {
                switch (rtl)
                {
                    case "Auto":
                        cbxRightToLeft.SelectedIndex = 0;
                        break;
                    case "No":
                        cbxRightToLeft.SelectedIndex = 1;
                        break;
                    case "Yes":
                        cbxRightToLeft.SelectedIndex = 2;
                        break;
                    default:
                        cbxRightToLeft.SelectedIndex = 1;
                        break;
                }
                SetConfigRightToLeft();
            }
        }

        private void SaveRightToLeft()
        {
            XmlItem uiOptions = Config.Root.FindItem("UIOptions");

            switch (cbxRightToLeft.SelectedIndex)
            {
                case 0:
                    uiOptions.SetProp("RightToLeft", "Auto");
                    break;
                case 1:
                    uiOptions.SetProp("RightToLeft", "No");
                    break;
                case 2:
                    uiOptions.SetProp("RightToLeft", "Yes");
                    break;
                default:
                    uiOptions.SetProp("RightToLeft", "No");
                    break;
            }
            SetConfigRightToLeft();
        }

        public override void Init()
        {
            cbxUIStyle.SelectedIndex = (int)designer.UIStyle;
            cbxIconPack.SelectedIndex = Config.IconPack;
#if COMMUNITY
            btnExportsMenuEdit.Visible = false;
            cbRibbon.Visible = false;
#endif
#if MONO
            cbRibbon.Enabled = false;
            cbWelcome.Enabled = false;
            cbWelcome.Enabled = false;
            cbDisableHotkeys.Enabled = false;
#else
            cbRibbon.Checked = Config.UseRibbon;
            cbWelcome.Checked = Config.WelcomeShowOnStartup;
            cbWelcome.Visible = Config.WelcomeEnabled;
            cbDisableHotkeys.Checked = Config.DisableHotkeys;
#endif

            XmlItem pluginsItem = Config.Root.FindItem("Plugins");
            for (int i = 0; i < pluginsItem.Count; i++)
            {
                XmlItem xi = pluginsItem[i];
                AddPlugin(xi.GetProp("Name"));
            }

            lblNote.Width = tab2.Width - lblNote.Left * 2;
            lblNote.Height = tab2.Height - lblNote.Top;
            dgPlugins_SelectedIndexChanged(this, EventArgs.Empty);

            LoadRightToLeft(); // load and apply Right to Left option
            lblRestart.Visible = false;
        }

        public override void Done(DialogResult result)
        {
            if (result == DialogResult.OK)
            {
#if !MONO
                //HACK
                if (cbRibbon.Checked == true && Config.UseRibbon == false)
                {
                    foreach (Bar bar in designer.DotNetBarManager.Bars)
                        if (bar is ToolbarBase)
                            bar.Hide();
                }
                else if (cbRibbon.Checked == false && Config.UseRibbon == true)
                {
                    foreach (Bar bar in designer.DotNetBarManager.Bars)
                        if (bar is ToolbarBase)
                            bar.Show();
                }

                Config.WelcomeShowOnStartup = cbWelcome.Checked;
                Config.UseRibbon = cbRibbon.Checked;
                Config.DisableHotkeys = cbDisableHotkeys.Checked;
#endif
                Config.UIStyle = (UIStyle)cbxUIStyle.SelectedIndex;
                designer.UIStyle = (UIStyle)cbxUIStyle.SelectedIndex;
                Config.IconPack = cbxIconPack.SelectedIndex;

                XmlItem pluginsItem = Config.Root.FindItem("Plugins");
                pluginsItem.Clear();

                foreach (DataGridViewRow item in dgPlugins.Rows)
                {
                    XmlItem xi = pluginsItem.Add();
                    xi.Name = "Plugin";
                    xi.SetProp("Name", item.Cells[1].Tag.ToString());
                }

                SaveRightToLeft(); // save and apply Right to Left option
            }
        }

        public PluginsOptions(Designer designer) : base()
        {
            this.designer = designer;

            InitializeComponent();
            Localize();
        }
    }
}

