using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;

using DevExpress.XtraScheduler;

namespace BeWo.Scheduler.View
{
    public class EmployeeBrushConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var farbe = Color.FromRgb(192, 255, 208);
            var gradientCollection = new GradientStopCollection { new GradientStop(farbe, 1) };

            if (values[2] is bool && ((bool) values[2]) == false)
            {
                return gradientCollection;
            }

            try
            {
                var customViewInfo = values[1] as CustomFieldCollection;
                if (customViewInfo == null)
                {
                    return new LinearGradientBrush(new GradientStopCollection { new GradientStop(farbe, 1) }, new Point(.5, 0), new Point(.5, 1));
                }

                var aptEmployees = (List<Employee2SchedulerAppointmentDC>) customViewInfo["EmployeeList"];
                var ersteller = (CompactEmployeeDC) customViewInfo["Originator"];

                if (aptEmployees.Count > 0)
                {
                    var firstEmployeeInList = aptEmployees.First(f => f.Employee.EmployeeColor != null);
                    gradientCollection = new GradientStopCollection { new GradientStop(firstEmployeeInList.Employee.FilterableBrush.Color, 1) };
                }
                else if (!String.IsNullOrEmpty(ersteller.EmployeeColor))
                {
                    gradientCollection = new GradientStopCollection {new GradientStop(ersteller.FilterableBrush.Color, 1)};
                }

                return new LinearGradientBrush(gradientCollection, new Point(.5, 0), new Point(.5, 1));
            }
            catch (Exception)
            {
                return new LinearGradientBrush(new GradientStopCollection { new GradientStop(farbe, 1) }, new Point(.5, 0), new Point(.5, 1));
            }
        }


        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}