using System;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight.Messaging;
using TaskIt.Infrastructure;
using TaskIt.Modules.Tasks.Core;
using TaskIt.Modules.Tasks.ViewModels;
using Telerik.Windows;
using Telerik.Windows.Controls.DragDrop;

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

            ViewModel.ViewProjectForm += OnViewProjectForm;
            ViewModel.ViewContextForm += OnViewContextForm;
            ViewModel.ViewContactForm += OnViewContactForm;
            ViewModel.ConfirmDeletion += OnConfirmDeletion;
            this.AddHandler(RadDragAndDropManager.DragQueryEvent, new EventHandler<DragDropQueryEventArgs>(AllowAllDragsEventHandler), true);

            Messenger.Default.Register<ViewEntityFormArgs>(this, OnViewForm);
            Messenger.Default.Register<ViewListArgs>(this, OnViewList);

            ListView = new List();
            DetailsView = new Details();
            ProjectDetailsView = new ProjectDetails();
            ContextDetailsView = new ContextDetails();
            ContactDetailsView = new ContactDetails();
            PageContainer.Content = ListView;  
        }

        void OnViewForm(ViewEntityFormArgs args)
        {
            if (PageContainer.Content == DetailsView ||
                PageContainer.Content == ProjectDetailsView ||
                PageContainer.Content == ContextDetailsView ||
                PageContainer.Content == ContactDetailsView)
            {
                return;
            }

            PageContainer.Content = GetContentByEntityType(args.EntityType);
        }

        private UserControl GetContentByEntityType(EntityType entityType)
        {
            switch (entityType)
            {
                case EntityType.Project:
                    return ProjectDetailsView;
                case EntityType.Context:
                    return ContextDetailsView;
                case EntityType.Contact:
                    return ContactDetailsView;
                default:
                    return DetailsView;
            }
        }

        void OnViewList(ViewListArgs args)
        {
            if (PageContainer.Content == ListView)
            {
                return;
            }
            PageContainer.Content = ListView;
        }

        void OnViewProjectForm(object sender, RoutedEventArgs e)
        {
            PageContainer.Content = ProjectDetailsView;
        }

        void OnViewContextForm(object sender, RoutedEventArgs e)
        {
            PageContainer.Content = ContextDetailsView;
        }

        void OnViewContactForm(object sender, RoutedEventArgs e)
        {
            PageContainer.Content = ContactDetailsView;
        }

        public void OnConfirmDeletion(object sender, RoutedEventArgs e)
        {
            //Messenger.Default.Send(new ConfirmDeletionArgs());
        }

        private List ListView { get; set; }
        private Details DetailsView { get; set; }
        private ProjectDetails ProjectDetailsView { get; set; }
        private ContextDetails ContextDetailsView { get; set; }
        private ContactDetails ContactDetailsView { get; set; }

        public bool IsInitialized { get; set; }

        private static void AllowAllDragsEventHandler(object sender, DragDropQueryEventArgs e)
        {
            var payload = e.Options.Payload as DragDropViewModel;
            if (e.Options.Status == DragStatus.DragQuery && e.QueryResult != false)
            {
                if (payload != null)
                {
                    e.QueryResult = true;
                    e.Handled = true;

                    e.Options.ArrowCue = RadDragAndDropManager.GenerateArrowCue();

                    e.Options.DragCue = new ContentControl
                    {
                        ContentTemplate = DataTemplateProvider.Instance.DragCueTemplate,
                        Content = payload
                    };
                }
            }

            if (e.Options.Payload == null || e.Options.Status != DragStatus.DropSourceQuery)
            {
                return;
            }

            if (e.QueryResult == null)
            {
                e.QueryResult = true;
            }
        }

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

        [Import]
        public ApplicationDragDrop ApplicationDragDrop { get; set; }
    }
}