using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Media;

using BeWo.Scheduler.Resources;
using BeWo.Scheduler.View;

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

using DevExpress.Xpf.Scheduler;
using DevExpress.XtraScheduler;

namespace BeWo.Scheduler.ViewModel
{
	public class CommonCalendarListVM : ISchedulerViewModel
	{
		public SchedulerSettings GetDefaultSchedulerSettings()
		{
			var settings = new SchedulerSettings(AppointmentKind.CommonCalendar, AppointmentViewType.Week);
			return settings;
		}

		private BindingList<IBeWoAppointment> mAppointments;

		public BindingList<IBeWoAppointment> Appointments
		{
			get
			{
				if (mAppointments == null)
				{
					mAppointments = new BindingList<IBeWoAppointment>(VMList.ToList<IBeWoAppointment>());
				}

				return mAppointments;
			}
		}
		public IList<BeWoAppointmentCategory> Categories { get; private set; }
		public IList<IBeWoAppointmentResource> Resources { get; private set; }
		public Dictionary<Guid, Dictionary<string, Dictionary<string, object>>> ChangedOccurencyCustomFields { get; private set; }
		public object SelectedFilterObject { get; set; }
		public List<CommonCalendarVM> VMList { get; set; }


		public CommonCalendarListVM(IEnumerable<CompactSupportConceptDC> allSupportConcepts, IEnumerable<CompactCustomerDC> allCustomers)
		{
			VMList = new List<CommonCalendarVM>();

			Categories = new List<BeWoAppointmentCategory>{
				new BeWoAppointmentCategory
				{
					CategoryObject = new object(),
					Color = Color.FromRgb(153,59,59),
					DisplayName = "Hilfeplan",
					MenuCaption = "Hilfeplan"
				},
				new BeWoAppointmentCategory
				{
					CategoryObject = new object(),
					Color = Color.FromRgb(59,119,153),
					DisplayName = "Geburtstag",
					MenuCaption = "Geburtstag"
				},
				new BeWoAppointmentCategory
				{
					CategoryObject = new object(),
					Color = Color.FromRgb(119,221,119),
					DisplayName = "Abwesenheit",
					MenuCaption = "Abwesenheit"
				}
			};
			
			foreach (var customer in allCustomers)
			{
				if (customer.DateOfBirth == null) continue;

				var vm = new CommonCalendarVM(customer, null, null);
				VMList.Add(vm);

				if (customer.AbsenceTimes == null) continue;
				foreach (var absenceTime in customer.AbsenceTimes)
				{
					var avm = new CommonCalendarVM(customer, null, absenceTime);
					VMList.Add(avm);
				}
			}

			foreach (var supportConcept in allSupportConcepts)
			{
				var x = new CommonCalendarVM(supportConcept.Customer, supportConcept, null);
				VMList.Add(x);
			}

			Resources = new List<IBeWoAppointmentResource>();
			foreach (var item in VMList)
			{
				IBeWoAppointmentResource res = new CommonCalendarResource(item.Customer, item.SupportConcept);
				Resources.Add(res);
			}

			foreach (var item in VMList)
			{
				switch (item.Typ)
				{
					case CommonCalendarVMType.Abwesenheit:
						item.LabelId = 2;
						break;
					case CommonCalendarVMType.Geburtstag:
						item.LabelId = 1;
						break;
					case CommonCalendarVMType.Hilfeplan:
						item.LabelId = 0;
						break;
					default:
						item.LabelId = 0;
						break;
				}

				item.CustomFields.Add("Customer", item.Customer);
			}
		}


		public void SaveAppointments(SchedulerControl control, IEnumerable<Appointment> list) { }

		public void DeleteAppointments(SchedulerControl control, IEnumerable<Appointment> list) {}

		public void InsertNewAppointments(SchedulerControl control, IEnumerable<Appointment> list) {}

		public AbstractAppointmentEditForm GetEditAppointmentForm(SchedulerControl control, Appointment appointment)
		{
			return new CommonCalendarEditForm(control, appointment);
		}

		public void AddCustomFieldsMapping(SchedulerStorage schedulerStorage)
		{
			var customerMapping = new SchedulerCustomFieldMapping("Customer", "Customer");
			var isBirthdayMapping = new SchedulerCustomFieldMapping("IsBirthday", "IsBirthday");
			var kalenderTypMapping = new SchedulerCustomFieldMapping("KalenderTyp", "KalenderTyp");
			var supportConceptMapping = new SchedulerCustomFieldMapping("SupportConcept", "SupportConecpt");
			
			schedulerStorage.AppointmentStorage.CustomFieldMappings.Add(customerMapping);
			schedulerStorage.AppointmentStorage.CustomFieldMappings.Add(isBirthdayMapping);
			schedulerStorage.AppointmentStorage.CustomFieldMappings.Add(kalenderTypMapping);
			schedulerStorage.AppointmentStorage.CustomFieldMappings.Add(supportConceptMapping);
		}

		public void InitNewAppointment(Appointment appointment)
		{
			appointment.CustomFields["Customer"] = null;
			appointment.CustomFields["IsBirthday"] = false;
			appointment.CustomFields["KalenderTyp"] = null;
			appointment.CustomFields["SupportConcept"] = null;
			
			appointment.StatusId = 0;
		}

		public bool FilterAppointment(string filterCategory, object selectedObject, Appointment appointment, TimeInterval interval)
		{
			var isBirthday = (bool) appointment.CustomFields["IsBirthday"];
			var customer = (CompactCustomerDC) appointment.CustomFields["Customer"];
			var typ = (CommonCalendarVMType)appointment.CustomFields["KalenderTyp"];

			switch (filterCategory)
			{
				case "Abwesenheiten":
					return !typ.Equals(CommonCalendarVMType.Abwesenheit);
				case "Geburtstage":
					return !isBirthday;
				case "Hilfepläne":
					return isBirthday || typ.Equals(CommonCalendarVMType.Abwesenheit);
				case "Klienten":
					return !customer.Equals(selectedObject);
			}

			return false;
		}

		public void InitChangedOccurrencyCustomFields<T>(IEnumerable<T> appList) where T : IDataContract {}
	}
}
