using System.Windows.Forms;

namespace VMwareLauncher
{
    public class TrayIcon
    {
        private static NotifyIcon trayIcon = new NotifyIcon();
        private static MenuItem itemKeepLauncherRunning = new MenuItem();
        private static MenuItem itemHelp = new MenuItem();
        private static ContextMenu contextMenu = new ContextMenu();

        private static bool keepLauncherRunning { get; set; }
        public static bool KeepLauncherRunning
        {
            get
            {
                return keepLauncherRunning;
            }
            set
            {
                itemKeepLauncherRunning.Checked = value;
                keepLauncherRunning = value;
            }
        }

        public void Create()
        {
            // Assign icon to tray object.
            trayIcon.Icon = Properties.Resources.VMwareLauncher;
            trayIcon.DoubleClick += TrayIcon_DoubleClick;

            itemKeepLauncherRunning.Text = "Keep Launcher Running";
            itemKeepLauncherRunning.Click += KeepLauncherRunning_Click;
            contextMenu.MenuItems.Add(itemKeepLauncherRunning);

            itemHelp.Text ="Help";
            itemHelp.Click += ItemHelp_Click;
            contextMenu.MenuItems.Add(itemHelp);

            // Wire context menu to tray icon and show.
            trayIcon.ContextMenu = contextMenu;
            trayIcon.Visible = true;
        }

        private void TrayIcon_DoubleClick(object sender, System.EventArgs e)
        {
            var proc = new ProcessControl();
            proc.ProcessName = ProcessControl.DefaultVmwarePath;
            proc.StartProcess();
        }

        private void ItemHelp_Click(object sender, System.EventArgs e)
        {
            trayIcon.ShowBalloonTip(5000,
                "VMware Launcher",
                "VMware Launcher will continue to run in the background until Workstation has been terminated.",
                ToolTipIcon.Info);
        }

        private void KeepLauncherRunning_Click(object sender, System.EventArgs e)
        {
            if (KeepLauncherRunning == false)
            {
                KeepLauncherRunning = true;
            }
            else
            {
                KeepLauncherRunning = false;
            }
        }

        public void Destroy()
        {
            trayIcon.ShowBalloonTip(3000,
                "VMware Launcher",
                "VMware Launcher has stopped all background Workstation services. Enjoy your day :-)",
                 ToolTipIcon.None);
            trayIcon.Dispose();
        }
    }
}
