using System;
using System.Collections.Generic;
using System.Linq;

using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.Extensions;
using BeWo.ViewModel;
using BeWo.ViewModel.ListViewModel;
using BeWo.ServiceProxy;
using System.Windows.Threading;
using System.Windows.Media;
using BS.Shared.DataContracts.Compact;
using BeWo.Scheduler.Resources;
using System.ComponentModel;
using BeWo.Scheduler.View;
using DevExpress.XtraScheduler;

namespace BeWo.Scheduler.ViewModel
{
    public class ResourceBookingListVM : AbstractDCListMapperVM<BookingSequenceDC, ResourceBookingVM>, ISchedulerViewModel
    {
        private IList<BeWoAppointmentCategory> mAppointmentCategoryList = null;
        private IList<IBeWoAppointmentResource> mAppointmentResourceList = null;
        private Dictionary<ValueListEntryDC, List<ResourceDC>> mCategoriesToResources;
        private List<CompactEmployeeDC> mAllEmployees;


        public ResourceBookingListVM(List<BookingSequenceDC> pDCList)
            : base(pDCList)
        {
        }

        public ResourceBookingListVM(Dictionary<ValueListEntryDC, List<ResourceDC>> lCategoriesToResources, List<CompactEmployeeDC> allemps, List<BookingSequenceDC> bookings)
            : base(bookings)
        {
            this.mCategoriesToResources = lCategoriesToResources;
            this.mAllEmployees = allemps;

            InitViewModel();
        }

        

        private void InitViewModel()
        {
            mAppointmentCategoryList = new List<BeWoAppointmentCategory>();
            mAppointmentResourceList = new List<IBeWoAppointmentResource>();

            Dictionary<long, BeWoAppointmentCategory> oid2Category = new Dictionary<long, BeWoAppointmentCategory>();
            Dictionary<long, IBeWoAppointmentResource> oid2Resource = new Dictionary<long, IBeWoAppointmentResource>();

            foreach (var item in mCategoriesToResources.Keys)
            {
                foreach (var res in mCategoriesToResources[item])
                {
                    BeWoAppointmentCategory cat = new BeWoAppointmentCategory();
                    cat.CategoryObject = res;
                    cat.Color = ((SolidColorBrush)new BrushConverter().ConvertFrom(res.Color)).Color;
                    cat.DisplayName = res.Name;
                    cat.MenuCaption = res.Name;
                    if (!oid2Category.ContainsKey(res.ResourceOid.Value))
                        oid2Category.Add(res.ResourceOid.Value, cat);
                    mAppointmentCategoryList.Add(cat);
                }

            }

            foreach (var item in VMList)
            {
                
                if (item.Resource != null && oid2Category.ContainsKey(item.Resource.ResourceOid.Value))
                    item.AppointmentCategory = oid2Category[item.Resource.ResourceOid.Value];
            }

        
        
        }

        
        protected override ResourceBookingVM CreateVM(BookingSequenceDC pDC)
        {
            if (pDC.SequenceOid == null)
            {
                pDC.RootBooking = new ResourceBookingDC();
                pDC.RootBooking.Duration = new DateTimeSpan
                    {
                        StartDateTime = DateTime.Now.AddMinutes(15 - DateTime.Now.Minute % 15)
                    };
                pDC.RootBooking.Duration.EndDateTime = pDC.RootBooking.Duration.StartDateTime.AddHours(1);
                pDC.RootBooking.SequencePosition = 0;
                pDC.Frequency = ResourceBookingFrequencyType.Once;
                pDC.RepeatInterval = 1;
                pDC.SequenceEnd = pDC.RootBooking.Duration.StartDateTime.AddMonths(12).Date;
            }

            return new ResourceBookingVM(pDC);
        }

        #region ISchedulerViewModel Member

        public IList<BeWoAppointmentCategory> Categories
        {
            get
            {
                return mAppointmentCategoryList;
            }
        }

        public IList<IBeWoAppointmentResource> Resources
        {
            get
            {
                return mAppointmentResourceList;
            }
            
        }

        public BindingList<IBeWoAppointment> Appointments
        {
            get
            {
                return new BindingList<IBeWoAppointment>(VMList.ToList<IBeWoAppointment>());
            }
            
        }

        public SchedulerSettings GetDefaultSchedulerSettings()
        {
            SchedulerSettings settings = new SchedulerSettings(AppointmentKind.Employee, AppointmentViewType.Timeline);

            return settings;
        }

        public void SaveAppointments(DevExpress.Xpf.Scheduler.SchedulerControl control, IList<Appointment> list)
        {
            foreach (var item in list)
            {
                AppointmentVM vm = item.GetSourceObject(control.GetCoreStorage()) as AppointmentVM;
                if (vm != null)
                {
                    AppointmentDC dc = vm.CommitToDataContract();


                }
            }

        }

        public void DeleteAppointments(DevExpress.Xpf.Scheduler.SchedulerControl control, IList<Appointment> list)
        {
            throw new NotImplementedException();
        }

        public void InsertNewAppointments(DevExpress.Xpf.Scheduler.SchedulerControl control, IList<Appointment> list)
        {
            throw new NotImplementedException();
        }

        public AbstractAppointmentEditForm GetEditAppointmentForm(DevExpress.Xpf.Scheduler.SchedulerControl control, DevExpress.XtraScheduler.Appointment appointment)
        {
            return null; // new ResourceAppointmentEditForm(control, appointment);
        }

        public void AddCustomFieldsMapping(DevExpress.Xpf.Scheduler.SchedulerStorage schedulerStorage)
        {
            //
        }

        public void InitNewAppointment(Appointment appointment)
        {
            //
        }
        #endregion

    }
}