using System;
using System.Windows.Forms;
using CefSharp;

namespace XeroBrowser
{
    public class MenuHandler : IContextMenuHandler
    {
        public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
        {
            // Remove any existent option using the Clear method of the model
            //
            //model.Clear();

            Console.WriteLine(@"Context menu opened !");

            // You can add a separator in case that there are more items on the list
            if (model.Count > 0)
            {
                model.AddSeparator();
            }


            // Add a new item to the list using the AddItem method of the model
            //model.AddItem((CefMenuCommand)113, "Copy link address");
            model.AddItem((CefMenuCommand)114, "Paste");

            // Add a separator
            //model.AddSeparator();

        }

        public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
        {
            // React to the first ID (show dev tools method)
            if (commandId == (CefMenuCommand)26501)
            {
                browser.GetHost().ShowDevTools();
                return true;
            }

            // React to the second ID (show dev tools method)
            if (commandId == (CefMenuCommand)26502)
            {
                browser.GetHost().CloseDevTools();
                return true;
            }


            // Any new item should be handled through a new if statement
            if ((int)commandId == 113)
            {
                //using System.Windows.Forms;
                try
                {
                    Clipboard.SetText(parameters.SourceUrl);
                }
                catch (ArgumentException) { }
            }
            return false;


#pragma warning disable CS0162 // Unreachable code detected
/*
            if ((int)commandId == 114)
            {
                //using System.Windows.Forms;
                Clipboard.GetText();
            }
#pragma warning restore CS0162 // Unreachable code detected
            return false;
*/

            // Return false should ignore the selected option of the user !
        }

        public void OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame)
        {

        }

        public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
        {
            return false;
        }
    }
}