using System;
using System.Linq;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Media;
using GalaSoft.MvvmLight.Messaging;
using TaskIt.Infrastructure;
using TaskIt.Web;
using TaskIt.Web.Services;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls.Scheduler;

namespace TaskIt.Core
{
    public static class Utils
    {
        public static App CurrentApplication
        {
            get { return Application.Current as App; }
        }

        public static DataContext DataContext
        {
            get { return CurrentApplication.DataContext; }
        }

        public static long CurrentUserID
        {
            get { return CurrentUser.ID; }
        }

        public static TaskItUser CurrentUser
        {
            // TODO: Ross - Hard-coded to 3 for now. When auth is implemented, set this to the id of the current user
            get { return DataContext.TaskItUsers.Where(p => p.ID == 3).FirstOrDefault(); }
        }

        public static void GridContextMenuOpened(object sender, RoutedEventArgs e)
        {
            var menu = (RadContextMenu)sender;
            var row = menu.GetClickedElement<GridViewRow>();

            if (row == null)
            {
                menu.IsOpen = false;
                return;
            }

            row.IsSelected = row.IsCurrent = true;
            var cell = menu.GetClickedElement<GridViewCell>();
            if (cell != null)
            {
                cell.IsCurrent = true;
            }
        }

        public static void CalendarContextMenuOpened(object sender, RoutedEventArgs e)
        {
            var menu = (RadContextMenu)sender;
            var appointmentItem = menu.GetClickedElement<AppointmentItem>();

            if (appointmentItem == null)
            {
                menu.IsOpen = false;
                return;
            }

            appointmentItem.IsSelected = true;
        }

        public static Category GetAppointmentCategory(long taskCategoryID)
        {
            switch ((TaskCategoryEnum)taskCategoryID)
            {
                case TaskCategoryEnum.Inbox:
                    return Categories.GreenCategory;
                case TaskCategoryEnum.Actions:
                    return Categories.BlueCategory;
                case TaskCategoryEnum.Waiting:
                    return Categories.PurpleCategory;
                case TaskCategoryEnum.Someday:
                    return Categories.YellowCategory;
                case TaskCategoryEnum.Ticklers:
                    return Categories.RedCategory;
                case TaskCategoryEnum.Reference:
                    return Categories.OrangeCategory;
                default:
                    return Categories.OliveCategory;
            }
        }

        public static void SetFocus(Control control)
        {
            if (!CurrentApplication.IsRunningOutOfBrowser)
            {
                HtmlPage.Plugin.Focus();
            }
            control.Focus();
        }

        public static void SetFocusAndSelect(TextBox textBox)
        {
            SetFocus(textBox);
            textBox.SelectAll();
        }

        public static string GetIconPath(string iconName)
        {
            return Constants.ICON_PATH + iconName + Constants.ICON_EXTENSION;
        }

        public static Uri GetIconUri(string iconName)
        {
            return new Uri(GetIconPath(iconName), UriKind.Relative);
        }

        public static Uri GetLargeIconUri(string iconName)
        {
            return new Uri(Constants.LARGE_ICON_PATH + iconName + Constants.ICON_EXTENSION, UriKind.Relative);
        }

        public static void ShowBusyIndicator()
        {
            Messenger.Default.Send(new OperationInProgressArgs());
        }

        public static void HideBusyIndicator()
        {
            Messenger.Default.Send(new OperationCompletedArgs());
        }

        public static void ShowErrorMessage(string errorMessage, string operationErrorMessage)
        {
            var args = new OperationErrorArgs(errorMessage, operationErrorMessage);
            Messenger.Default.Send(args); 
        }

        public static string GetErrorMessage(OperationErrorArgs args)
        {
            return string.Format(args.ErrorMessage, args.OperationErrorMessage);
        }

        public static ChartLegendItem GetChartLegendItem(string label, Brush brush)
        {
            return new ChartLegendItem
            {
                Label = label,
                Background = brush,
                BorderThickness = new Thickness(0)
            };
        }

        public static bool IsStringEmpty(string text)
        {
            return text.Trim() == string.Empty;
        }
    }
}