using System;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using FSI.BT.Tools.Global.Utilities;

namespace FSI.BT.Tools.SystemTrayMenu.Helper
{
    internal class AppContextMenu
    {
        public event Action ClickedOpenLog;

        public ContextMenuStrip Create()
        {
            ContextMenuStrip menu = new()
            {
                BackColor = SystemColors.Control,
            };

            AddItem(menu, "Settings", () => Global.UserInterface.SettingsForm.ShowSingleInstance());
            AddSeperator(menu);
            AddItem(menu, "Log File", () => ClickedOpenLog?.Invoke());
            AddSeperator(menu);
            //AddItem(menu, "Frequently Asked Questions", Config.ShowHelpFAQ);
            AddItem(menu, "Support SystemTrayMenu", Config.ShowSupportSystemTrayMenu);
            AddItem(menu, "About SystemTrayMenu", About);
            //AddItem(menu, "Check for updates", () => GitHubUpdate.ActivateNewVersionFormOrCheckForUpdates(showWhenUpToDate: true));
            AddSeperator(menu);
            AddItem(menu, "Restart", AppRestart.ByAppContextMenu);
            AddItem(menu, "Exit app", Application.Exit);

            return menu;
        }

        private static void AddSeperator(ContextMenuStrip menu)
        {
            menu.Items.Add(new ToolStripSeparator());
        }

        private static void AddItem(
                ContextMenuStrip menu,
                string text,
                Action actionClick)
        {
            ToolStripMenuItem toolStripMenuItem = new()
            {
                Text = Translator.GetText(text),
            };

            toolStripMenuItem.Click += (sender, e) => actionClick();
            menu.Items.Add(toolStripMenuItem);
        }

        private static void About()
        {
            FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(
                Assembly.GetEntryAssembly().Location);
            Global.UserInterface.AboutBox aboutBox = new()
            {
                AppTitle = versionInfo.ProductName,
                AppDescription = versionInfo.FileDescription,
                AppVersion = $"Version {versionInfo.FileVersion}",
                AppCopyright = versionInfo.LegalCopyright,
                AppMoreInfo = versionInfo.LegalCopyright,
                AppImage = SystemTrayMenu.Properties.Resources.SystemTrayMenu.ToBitmap(),
            };

            aboutBox.AppDetailsButton = true;
            aboutBox.ShowDialog();
        }
    }
}