using System;
using System.Collections.Generic;

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

using DevExpress.XtraScheduler;

namespace BeWo.Scheduler.ViewModel
{
	public class CommonCalendarVM : IBeWoAppointment
	{
		public BeWoAppointmentCategory AppointmentCategory { get; set; }
		public object Id { get; set; }
		public DateTime Start { get; set; }
		public DateTime End { get; set; }
		public int LabelId { get; set; }
		public string Subject { get; set; }
		public string Description { get; set; }
		public int Status { get; set; }
		public string Location { get; set; }
		public bool AllDay { get; set; }
		public int EventType { get; set; }
		public string RecurrenceInfo { get; set; }
		public string ReminderInfo { get; set; }
		public object ResourceId { get; set; }
		public Dictionary<string, object> CustomFields { get; private set; }

		public CompactCustomerDC Customer { get; set; }
		private bool IsBirthday { get; set; }
		public CompactSupportConceptDC SupportConcept { get; set; }

		public CommonCalendarVMType Typ { get; set; }

		public CommonCalendarVM(CompactCustomerDC customer, CompactSupportConceptDC supportConcept, AbsenceTimeDC absenceTime)
		{
			if (supportConcept == null && absenceTime == null)
			{
				IsBirthday = true;
				AllDay = true;
				
				Subject = string.Format("Geburtstag von {0}", customer.FullName);
				Description = Subject;
				Start = customer.DateOfBirth.Value;
				End = customer.DateOfBirth.Value;
				Start = Start.AddYears(2013 - Start.Year);
				End = Start;
				EventType = 0;
				//var recInfo = new RecurrenceInfo
				//{
				//	Type = RecurrenceType.Yearly,
				//	Periodicity = 1,
				//	Start = Start,
				//	Month = Start.Month,
				//	WeekOfMonth = WeekOfMonth.None,
				//	DayNumber = Start.Day,
				//	WeekDays = GetWeekDay(Start.DayOfWeek.ToString()),
				//	Range = RecurrenceRange.NoEndDate
				//};

				//EventType = 1;
				//RecurrenceInfo = recInfo.ToXml();
				Typ = CommonCalendarVMType.Geburtstag;

				ResourceId = customer.CustomerOid;
			}
			else if(absenceTime == null)
			{
				Subject = string.Format("{0} endet am {1}", customer, supportConcept.EndDate.Value.ToShortDateString());
				Description = Subject;
				Start = supportConcept.EndDate.Value;
				End = supportConcept.EndDate.Value;
				Typ = CommonCalendarVMType.Hilfeplan;
				AllDay = true;
			}
			else
			{
				Subject = string.Format("Abwesenheit von {0}", customer.FullName);
				Description = string.Format("{0}", absenceTime.Reason);
				Start = absenceTime.Start.Value;

				if (absenceTime.End == null)
				{
					End = absenceTime.Start.Value;
					AllDay = true;
				}
				else
				{
					End = absenceTime.End.Value;
				}
				
				Typ = CommonCalendarVMType.Abwesenheit;
				AllDay = true;
			}

			Customer = customer;
			SupportConcept = supportConcept;

			CustomFields = new Dictionary<string, object> { { "IsBirthday", IsBirthday }, { "KalenderTyp", Typ } };
		}

		private static WeekDays GetWeekDay(string name)
		{
			switch (name)
			{
				case "Sunday":
					return WeekDays.Sunday;
				case "Monday":
					return WeekDays.Monday;
				case "Tuesday":
					return WeekDays.Thursday;
				case "Wednesday":
					return WeekDays.Wednesday;
				case "Thursday":
					return WeekDays.Thursday;
				case "Friday":
					return WeekDays.Friday;
				case "Saturday":
					return WeekDays.Saturday;
				default:
					return WeekDays.Sunday;
			}
		}
	}
}
