using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CustomSpellMenu
{
    public partial class MainPage : UserControl
    {
        C1.Silverlight.C1ContextMenu _contextMenu;

        public MainPage()
        {
            InitializeComponent();

            // create common context menu for rich both text boxes
            _contextMenu = new C1.Silverlight.C1ContextMenu();
            for (int i = 0; i < 4; i++)
            {
                var mi = new C1.Silverlight.C1MenuItem();
                mi.Header = "Item " + i.ToString();
                mi.Click += mi_Click;
                _contextMenu.Items.Add(mi);
            }

            //-----------------------------------------------------------------------------
            #region method 1: clone our items and add clones to spell-checker menu

            // add spell-checker to rich text box
            // NOTE: do this *before* adding the context menu
            var spell1 = new C1.Silverlight.SpellChecker.C1SpellChecker();
            spell1.MainDictionary.Load(GetResourceStream("C1Spell_en-US.dct"));
            _rtb1.SpellChecker = spell1;

            // add context menu to rich text box
            // NOTE: do this *after* adding the spell-checker
            _rtb1.MouseRightButtonDown += (s, e) =>
            {
                e.Handled = true;
                _contextMenu.Show(_rtb1, e.GetPosition(_rtb1));
            };

            // clone items in the main context menu and add them to the spell-checker menu
            // this is easy to do, but assumes that a single event handler ('mi_Click') 
            // will be used for all options in the main context menu.
            spell1.ContextMenuCreated += (s, e) =>
                {
                    var ctx = e.Menu;
                    ctx.Items.Add(new C1.Silverlight.C1Separator());
                    foreach (C1.Silverlight.C1MenuItem mi in _contextMenu.Items)
                    {
                        var item = new C1.Silverlight.C1MenuItem();
                        item.Header = mi.Header;
                        item.Tag = mi; // save reference to original item
                        item.Click += mi_Click;
                        ctx.Items.Add(item);
                    }
                };

            #endregion

            //-----------------------------------------------------------------------------
            #region method 2: add spell-checker items to our menu

            // add spell-checker to rich text box
            // NOTE: do this *before* adding the context menu
            var spell2 = new C1.Silverlight.SpellChecker.C1SpellChecker();
            spell2.MainDictionary.Load(GetResourceStream("C1Spell_en-US.dct"));
            _rtb2.SpellChecker = spell1;

            // add context menu to rich text box
            // NOTE: do this *after* adding the spell-checker
            _rtb2.MouseRightButtonDown += (s, e) =>
            {
                e.Handled = true;
                _contextMenu.Show(_rtb1, e.GetPosition(_rtb1));
            };

            // move items regular context menu to spell-checker menu when showing
            // the menu, move them back when the spell-checker menu closes.
            // this is slightly more complex than option 1, but preserves the
            // original click handlers (more self-contained, no need for a single
            // event handler).
            spell2.ContextMenuCreated += (s, e) =>
            {
                var ctx = e.Menu;
                ctx.Items.Add(new C1.Silverlight.C1Separator());

                // move items from main menu to spell menu
                var movedItems = new List<object>();
                while (_contextMenu.Items.Count > 0)
                {
                    var item = _contextMenu.Items[0];
                    _contextMenu.Items.RemoveAt(0);
                    ctx.Items.Add(item);
                    movedItems.Add(item);
                }

                // move items back to main menu when context menu closes
                ctx.Closed += (ss,ee) => 
                {
                    foreach (var item in movedItems)
                    {
                        ctx.Items.Remove(item);
                        _contextMenu.Items.Add(item);
                    }
                };
            };

            #endregion
        }

        // handle clicks on menu items
        void mi_Click(object sender, C1.Silverlight.SourcedEventArgs e)
        {
            var mi = e.Source as C1.Silverlight.C1MenuItem;
            MessageBox.Show(string.Format("thanks for clicking item '{0}'", mi.Header));
        }

        // get a stream from an embedded resource 
        System.IO.Stream GetResourceStream(string rn)
        {
            var asm = this.GetType().Assembly;
            foreach (var resName in asm.GetManifestResourceNames())
            {
                if (resName.EndsWith(rn, StringComparison.OrdinalIgnoreCase))
                    return asm.GetManifestResourceStream(resName);
            }
            return null;
        }
    }
}
