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

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

            grdData.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick));
            RadDragAndDropManager.AddDragQueryHandler(grdData, OnDragQuery);
        }        

        private void OnDragQuery(object sender, DragDropQueryEventArgs e)
        {                        
            if (e.Options.Status != DragStatus.DragQuery)
            {
                return;
            }

            ViewModel.HandleGridTaskDrag(e);
        }

        private void OnCellDoubleClick(object sender, RadRoutedEventArgs e)
        {
            ViewModel.OnEdit();
        } 

        private void GridRowDetailsVisibilityChanged(object sender, GridViewRowDetailsEventArgs e)
        {            
            if (e.Visibility != Visibility.Visible)
            {
                return;
            }

            e.Row.IsSelected = true;
            if (ExpandedRow != null)
            {
                ExpandedRow.DetailsVisibility = Visibility.Collapsed;
            }
            ExpandedRow = e.Row;
            ViewModel.OnLoadNotes();
        }

        private void GridContextMenuOpened(object sender, RoutedEventArgs e)
        {
            Utils.GridContextMenuOpened(sender, e);
        }

        public GridViewRow ExpandedRow { get; set; }

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