using System;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows;

using BeWo.ViewModel.ListViewModel;

using DevExpress.Xpf.Grid;

using System.Windows.Input;

namespace BeWo.Converter
{
    public class AssessmentSheetHeaderToForegroundConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values != null && values.Length == 3)
            {
                TextBlock tb = values[2] as TextBlock;

                var header = values[0] as GridColumnHeader;
                if (header != null)
                {
                    GridColumn column = header.DataContext as GridColumn;

                    if (column != null && column.Name.StartsWith("colDay"))
                    {
                        int day = int.Parse(column.HeaderCaption.ToString().Replace(".", string.Empty));

                        AssessmentSheetEntryListVM vm = values[1] as AssessmentSheetEntryListVM;
                        if (vm == null)
                        {
                            var col = values[1] as GridColumn;
                            if (col != null)
                                vm = col.DataContext as AssessmentSheetEntryListVM;
                        }
                        if (vm != null)
                        {
                            DateTime dt;
                            try
                            {
                                dt = new DateTime(vm.StartDate.Year, vm.StartDate.Month, day);
                            }
                            catch (Exception)
                            {
                                dt = vm.StartDate;
                            }
                            
                            if (dt < vm.StartDate)
                                dt = dt.AddMonths(1);

                            var srList = vm.GetServiceRecordsForDate(dt);

                            if (srList != null && srList.Count > 0)
                            {
                                if (tb != null)
                                {
                                    tb.Cursor = Cursors.Hand;
                                    tb.FontSize = 12;
                                    tb.FontWeight = FontWeights.Bold;
                                }
                                return Brushes.DeepSkyBlue;
                            }
                        }
                    }
                }
                if (tb != null)
                {
                    tb.Cursor = null;
                    tb.FontSize = 12;
                    tb.FontWeight = FontWeights.Normal;
                }
            }
            
            return Brushes.White;
        }

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