using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using InABox.Wpf.Editors;
using Microsoft.Xaml.Behaviors.Core;

namespace InABox.DynamicGrid;

public class DynamicTabItem : TabItem
{

    public event DynamicTabItemCloseEvent OnCloseTab;

        
    public static readonly DependencyProperty CloseTabCommandProperty =
        DependencyProperty.Register(nameof(CloseTabCommand), typeof(ICommand), typeof(DynamicTabItem));

    public ICommand CloseTabCommand
    {
        get { return base.GetValue(CloseTabCommandProperty) as ICommand; }
        set { base.SetValue(CloseTabCommandProperty, value); }
    }
        
    public static readonly DependencyProperty CanCloseProperty =
        DependencyProperty.Register(nameof(CanClose), typeof(bool), typeof(DynamicTabItem), new PropertyMetadata(false));
        
    public bool CanClose
    {
        get => (bool)GetValue(CanCloseProperty);
        set => SetValue(CanCloseProperty, value);
    }
        
    public event DynamicTabItemContextMenuEvent OnContextMenuOpening;

    public static readonly DependencyProperty ContextMenuCommandProperty =
        DependencyProperty.Register(nameof(ContextMenuCommand), typeof(ICommand), typeof(DynamicTabItem));

    public ICommand ContextMenuCommand
    {
        get { return base.GetValue(ContextMenuCommandProperty) as ICommand; }
        set { base.SetValue(ContextMenuCommandProperty, value); }
    }
        
    public static readonly DependencyProperty CanRenameProperty =
        DependencyProperty.Register(nameof(CanRename), typeof(bool), typeof(DynamicTabItem), new PropertyMetadata(false));
        
    public bool CanRename
    {
        get => (bool)GetValue(CanRenameProperty);
        set => SetValue(CanRenameProperty, value);
    }
        
    /// <summary>
    /// Called when a tab is renamed. Note that changing <see cref="DynamicTabItemRenamedEventArgs.NewName"/> will change the new name of the tab.
    /// </summary>
    public event DynamicTabItemRenamedEvent OnTabRenamed;
        
    static DynamicTabItem()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(DynamicTabItem), new FrameworkPropertyMetadata(typeof(DynamicTabItem)));
    }

    public DynamicTabItem() : base()
    {
        VerticalAlignment = VerticalAlignment.Stretch;
        CloseTabCommand = new ActionCommand(() =>
        {
            bool bConfirm = false;
            if (OnCloseTab != null)
            {
                var args = new DynamicTabControlEventArgs() { TabItem = this };
                OnCloseTab?.Invoke(this, args);
                bConfirm = !args.Cancel;

            }
            else
                bConfirm = MessageBox.Show("Are you sure you wish to close this tab?", "Close Tab", MessageBoxButton.YesNo) ==
                           MessageBoxResult.Yes;
                
            if (bConfirm)
            {
                var parent = Parent as DynamicTabControl;
                if (parent != null)
                {
                    parent.Items.Remove(this);
                    parent.ChangedCommand.Execute(null);
                }
            }
        });

        ContextMenuCommand = new ActionCommand(() =>
        {

            ContextMenu menu = new ContextMenu();
            if (CanRename)
            {
                menu.Items.Add(new MenuItem()
                {
                    Header = "Rename Tab", Command = new ActionCommand(() =>
                    {
                        String tabname = Header.ToString() ?? "";
                        if (TextEdit.Execute("Rename Tab", ref tabname))
                        {
                            var args = new DynamicTabItemRenamedEventArgs() { OldName = Header.ToString() ?? "", NewName = tabname };
                            OnTabRenamed?.Invoke(this, args);
                            Header = args.NewName;
                            var parent = Parent as DynamicTabControl;
                            if (parent != null)
                                parent.ChangedCommand.Execute(null);
                        }
                    })
                });
            }

            if (OnContextMenuOpening != null)
            {
                var args = new DynamicTabItemContextMenuEventArgs { Menu = menu };
                OnContextMenuOpening.Invoke(this, args);
                if (args.Cancel == false)
                    args.Menu.IsOpen = true;
            }
            else
                menu.IsOpen = CanRename;
        });

    }

}