using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Media;
using BeWo.Scheduler.Resources;
using BeWo.Scheduler.View;
using BeWo.ViewModel.ListViewModel;
using BS.Shared;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using DevExpress.Xpf.Scheduler;
using DevExpress.XtraScheduler;

namespace BeWo.Scheduler.ViewModel
{
	// EditForm für normalen Termin und EditForm für Abwesenheit/Hilfeplanende/Geburtstag
	// Wie ResourceAppointmentListVM aufbauen
	public class KlientenKalenderListVM : AbstractDCListMapperVM<AppointmentDC, AppointmentVM>, ISchedulerViewModel
	{
		private List<BeWoAppointmentCategory> mAppointmentCategoryList;
		private IList<IBeWoAppointmentResource> mAppointmentResourceList;
		private readonly List<CompactCustomerDC> mAllCustomers;
		private readonly List<CompactEmployeeDC> mAllEmployees;
		private readonly List<AppointmentCategoryDC> mAllCategories;
		private BindingList<IBeWoAppointment> mAppointmentList;
		private readonly long? mCustomerOid;
		private readonly List<CompactSupportConceptDC> mAllSupportConcepts;

		public SchedulerSettings GetDefaultSchedulerSettings()
		{
			var settings = new SchedulerSettings(AppointmentKind.Customer, AppointmentViewType.WorkWeek);

			return settings;
		}

		public BindingList<IBeWoAppointment> Appointments { get; private set; }
		public IList<BeWoAppointmentCategory> Categories { get; private set; }
		public IList<IBeWoAppointmentResource> Resources { get; private set; }
		public Dictionary<Guid, Dictionary<string, Dictionary<string, object>>> changedOccurencyCustomFields;
		public object SelectedFilterObject { get; set; }

		void mAppointmentList_AddingNew(object sender, AddingNewEventArgs e)
		{
			var dc = new AppointmentDC
			{
				CustomerOid = mCustomerOid,
				EmployeeOid = BeWoApp.LoggedOnEmployee.EmployeeOid,
				Status = 0
			};

			e.NewObject = new AppointmentVM(dc);
		}
		public Dictionary<Guid, Dictionary<string, Dictionary<string, object>>> ChangedOccurencyCustomFields
		{
			get { return changedOccurencyCustomFields; }
			private set { changedOccurencyCustomFields = value; }
		}
		private Dictionary<long, BeWoAppointmentCategory> oid2Category;
		private Dictionary<long, int> catOid2Index;
		private readonly Dictionary<ValueListEntryDC, List<ResourceDC>> mCategories2Resources;
		public KlientenKalenderListVM(long? customerOid, List<AppointmentCategoryDC> allCats, List<CompactCustomerDC> allCustomers, List<CompactEmployeeDC> allemps,
														List<AppointmentDC> appList, List<CompactSupportConceptDC> allSupportConcepts)
            : base(appList)
        {
            mCustomerOid = customerOid;
            mAllCustomers = allCustomers;
            mAllEmployees = allemps;
            mAllCategories = allCats;
			mAllSupportConcepts = allSupportConcepts;

            InitViewModel();

			InitChangedOccurrencyCustomFields(appList.Where(t => t.Type == 3).ToList());
        }

		private void InitViewModel()
		{
			oid2Category = new Dictionary<long, BeWoAppointmentCategory>();
			catOid2Index = new Dictionary<long, int>();

			mAppointmentCategoryList = new List<BeWoAppointmentCategory>();
			mAppointmentResourceList = new List<IBeWoAppointmentResource>();

			int index = 0;
			foreach (AppointmentCategoryDC item in mAllCategories)
			{
				var cat = new BeWoAppointmentCategory
				{
					CategoryObject = item,
					Color = ((SolidColorBrush)new BrushConverter().ConvertFrom(item.Color)).Color,
					DisplayName = item.DisplayName,
					MenuCaption = item.DisplayName
				};
				if (!oid2Category.ContainsKey(item.AppointmentCategoryOid.Value))
					oid2Category.Add(item.AppointmentCategoryOid.Value, cat);

				mAppointmentCategoryList.Add(cat);

				if (!catOid2Index.ContainsKey(item.AppointmentCategoryOid.Value))
					catOid2Index.Add(item.AppointmentCategoryOid.Value, index);

				index++;
			}

			foreach (var item in VMList)
			{
				item.ResourceId = item.DataContract.CustomerOid;
				item.CustomFields.Add("EmployeeOid", item.EmployeeOid);
				item.CustomFields.Add("CustomerOid", item.DataContract.CustomerOid);

				if (item.AppointmentCategoryDC != null && oid2Category.ContainsKey(item.AppointmentCategoryDC.AppointmentCategoryOid.Value))
					item.AppointmentCategory = oid2Category[item.AppointmentCategoryDC.AppointmentCategoryOid.Value];

				if (item.AppointmentCategoryDC != null && catOid2Index.ContainsKey(item.AppointmentCategoryDC.AppointmentCategoryOid.Value))
					item.LabelId = catOid2Index[item.AppointmentCategoryDC.AppointmentCategoryOid.Value];
			}

			((List<IBeWoAppointmentResource>)mAppointmentResourceList).Sort((a, b) => a.Caption.CompareTo(b.Caption));
		}

		public void SaveAppointments(SchedulerControl control, IEnumerable<Appointment> list)
		{
			throw new NotImplementedException();
		}

		public void DeleteAppointments(SchedulerControl control, IEnumerable<Appointment> list)
		{
			throw new NotImplementedException();
		}

		public void InsertNewAppointments(SchedulerControl control, IEnumerable<Appointment> list)
		{
			throw new NotImplementedException();
		}

		public AbstractAppointmentEditForm GetEditAppointmentForm(SchedulerControl control, Appointment appointment)
		{
			throw new NotImplementedException();
		}

		public void AddCustomFieldsMapping(SchedulerStorage schedulerStorage)
		{
			throw new NotImplementedException();
		}

		public void InitNewAppointment(Appointment appointment)
		{
			throw new NotImplementedException();
		}

		public bool FilterAppointment(string filterCategory, object selectedObject, Appointment appointment, TimeInterval interval = null)
		{
			throw new NotImplementedException();
		}

		public void InitChangedOccurrencyCustomFields<T>(IEnumerable<T> appList) where T : IDataContract
		{
			throw new NotImplementedException();
		}
	}
}
