using System.ComponentModel.Composition;
using System.Windows;
using TaskIt.Modules.Tasks.ViewModels;
using TaskIt.Web;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.DragDrop;

namespace TaskIt.Modules.Tasks.Views
{
    public partial class TreeView
    {
        public TreeView()
        {
            InitializeComponent();
            CompositionInitializer.SatisfyImports(this);
            DataContext = ViewModel;

            RadDragAndDropManager.AddDropQueryHandler(tvwTree, OnDropQuery);
            RadDragAndDropManager.AddDropInfoHandler(tvwTree, OnDropInfo);

            ViewModel.FirstLoad += OnFirstLoad;
        }

        void OnFirstLoad(object sender, RoutedEventArgs e)
        {
            tvwTree.ItemPrepared += OnTreeViewItemPrepared;
        }

        static void OnTreeViewItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)
        {
            var treeViewItem = e.PreparedItem;
            if (treeViewItem.FullPath == "TaskIt.Modules.Tasks.ViewModels.CategoriesTreeViewNodeModel")
            {
                if (!treeViewItem.IsExpanded)
                {
                    treeViewItem.IsExpanded = true;
                }
            }
            if (treeViewItem.FullPath == "TaskIt.Modules.Tasks.ViewModels.CategoriesTreeViewNodeModel\\Actions")
            {
                if (!treeViewItem.IsSelected)
                {
                    treeViewItem.IsSelected = true;
                }                
            }
        }   

        private static void OnDropQuery(object sender, DragDropQueryEventArgs e)
        {
            e.QueryResult = IsDropNode(e.Options.Destination.DataContext);
            e.Handled = true;
        }

        private static bool IsDropNode(object nodeDataContext)
        {
            // Cannot drop on top-level node or All Categories node
            return !(nodeDataContext is CategoriesTreeViewNodeModel) &&
                   !(nodeDataContext is ProjectsTreeViewNodeModel) &&
                   !(nodeDataContext is ContextsTreeViewNodeModel) &&
                   !(nodeDataContext is ContactsTreeViewNodeModel) &&
                   !(nodeDataContext is TaskCategory && ((TaskCategory)nodeDataContext).ID == 0);
        }

        private void OnDropInfo(object sender, DragDropEventArgs e)
        {
            var treeViewItem = e.Options.Destination as RadTreeViewItem;
            if (treeViewItem == null)
            {
                return;
            }

            ViewModel.HandleTreeViewItemDrop(e, treeViewItem.Item);
            ViewModel.NodeRightClicked += OnNodeRightClicked;
        }

        void TreeViewContextMenuOpened(object sender, RoutedEventArgs e)
        {
            var treeViewItem = cmTreeView.GetClickedElement<RadTreeViewItem>();
            if (treeViewItem == null)
            {
                // The user has right-clicked in an open area outside of a tree node, so we don't want the context menu to appear
                cmTreeView.Visibility = Visibility.Collapsed;
                return;
            }
            ViewModel.ContextMenuOpened(treeViewItem);
        }

        void OnNodeRightClicked(object sender, RoutedEventArgs e)
        {
            ViewModel.RightClickedTreeViewItem.ExpandAll();
            cmTreeView.Visibility = Visibility.Visible;
        }

        [Import]
        public TreeViewModel ViewModel { get; set; }
    }
}