using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WPControls;
using Microsoft.Phone.Controls;
using System.Linq;
using HolidayManager.Db;

namespace HolidayManager
{
    public class ColorConverter : IDateToBrushConverter
    {
        MainPage _mainPage;
        MainPage mainPage
        {
            get
            {
                try
                {
                    if (_mainPage == null)
                    {
                        if (Application.Current == null || ((App)Application.Current).RootFrame == null)
                        {
                            return null;
                        }

                        _mainPage = (((App)(Application.Current)).RootFrame.Content as MainPage);
                    }

                    return _mainPage;
                }
                catch
                {
                    return null;
                }
            }
        }

        public Brush Convert(DateTime dateTime, bool isSelected, Brush defaultValue, BrushType brushType)
        {
            if (brushType == BrushType.Background)
            {
                HolidayType holidayType = HolidayType.None;

                if (IsMyHoliday(dateTime, out holidayType) == true)
                {
                    //if (IsToday(dateTime) == true)
                    //{
                    //    return new SolidColorBrush(Colors.Magenta);
                    //}

                    if (holidayType == HolidayType.Full)
                    {
                        return new SolidColorBrush(Colors.DarkGray);
                    }
                    else
                    {
                        return new SolidColorBrush(Colors.LightGray);
                    }
                }

                return defaultValue;
            }
            else
            {
                if (dateTime.DayOfWeek == DayOfWeek.Saturday)
                {
                    return new SolidColorBrush(Colors.Purple);
                }
                else if (dateTime.DayOfWeek == DayOfWeek.Sunday)
                {
                    return new SolidColorBrush(Colors.Red);
                }
                else
                {
                    return defaultValue;
                }
            }
        }

        private bool IsMyHoliday(DateTime dateTime, out HolidayType holidayType)
        {
            holidayType = HolidayType.None;

            if (mainPage == null || mainPage.MyHolidays == null)
            {
                return false;
            }

            if (mainPage.CacheDate.ContainsKey(dateTime) == false)
            {
                return false;
            }

            return true;
        }

        private bool IsToday(DateTime dateTime)
        {
            DateTime now = DateTime.Now;
            return (dateTime.Year == now.Year &&
                dateTime.Month == now.Month &&
                dateTime.Day == now.Day);
        }
    }
}
