using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using C1.Silverlight;

namespace C1.Silverlight.FlexGridBook
{
    /// <summary>
    /// Context menu for tab buttons (select sheets).
    /// </summary>
    public class SheetButtonContextMenu : C1ContextMenu
    {
        // ** fields
        StackPanel _btnPanel;
        C1FlexGridBook _grid;

        // ** ctor

        public SheetButtonContextMenu(StackPanel sp)
        {
            _btnPanel = sp;
            _grid = C1.Util.Util.GetParentOfType<C1FlexGridBook>(_btnPanel);
            _btnPanel.MouseRightButtonDown += btnPanel_MouseRightButtonDown;
        }

        // ** overrides/event handlers

        // show menu to select sheet
        void btnPanel_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            // we're taking care of this
            e.Handled = true;

            // rebuild item list
            this.Items.Clear();
            foreach (var sheet in _grid.Sheets)
            {
                AddMenuItem(sheet);
            }

            // show the menu
            var pt = e.GetPosition(_btnPanel);
            pt.Y = _btnPanel.RenderSize.Height - this.Items.Count * (this.FontSize + 10);
            Show(_btnPanel, pt);
        }

        // ** implementation

        C1MenuItem AddMenuItem(Sheet sheet)
        {
            var mi = new C1MenuItem();
            mi.Header = sheet.Name as string;
            mi.IsCheckable = true;
            mi.IsChecked = sheet.IsSelected;
            Items.Add(mi);
            mi.Checked += (s, e) =>
                {
                    sheet.IsSelected = true;
                };
            return mi;
        }
    }
}
