using System;
using System.Collections.Generic;

using BS.Shared;
using BS.Shared.Core;

using BeWo.Scheduler.View;
using BeWo.ViewModel;

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

namespace BeWo.Scheduler.ViewModel
{
    public class SchedulerViewModel
    {
        private readonly Dictionary<AppointmentKind, SchedulerSettings> mType2Settings = new Dictionary<AppointmentKind, SchedulerSettings>();
        private AppointmentKind mAppointmentKind;
        private ISchedulerViewModel mActiveAppointmentViewModel;

        public event EventHandler<EventArgs<ISchedulerViewModel>> ViewModelChanged;

        public SchedulerFilter Filter { get; set; }

        public AppointmentKind ActiveViewModelKind
        {
            get
            {
                return mAppointmentKind;
            }
            set
            {
                mAppointmentKind = value;
                UpdateAppointmentViewModel();
            }
        }

        public ISchedulerViewModel ActiveAppointmentViewModel
        {
            get { return mActiveAppointmentViewModel; }
            set 
            {
                mActiveAppointmentViewModel = value;
                if (ViewModelChanged != null)
                    ViewModelChanged(this, new EventArgs<ISchedulerViewModel>(value));
            }
        }

        private void UpdateAppointmentViewModel()
        {
            switch (ActiveViewModelKind)
            {
                case AppointmentKind.General:
                    break;
                case AppointmentKind.Resource:
                    UpdateResourceViewModel();
                    break;
                case AppointmentKind.Task:
                    break;
                case AppointmentKind.Customer:
                    UpdateCustomerAppointmentViewModel();
                    break;
                case AppointmentKind.Employee:
                    UpdateEmployeeAbsenceTimeViewModel();
                    break;
                case AppointmentKind.SupportConcept:
                    UpdateSupportConceptViewModel();
                    break;
				case AppointmentKind.CommonCalendar:
					UpdateCommonCalendarViewModel();
		            break;
            }
        }

        private void UpdateSupportConceptViewModel()
        {
            VMFactory.CreateSupportConceptAppointmentListVM(cb => { ActiveAppointmentViewModel = cb; });
        }

        private void UpdateCustomerAppointmentViewModel()
        {
            long? customerOid = null;
            if (Filter != null && Filter.CustomerOid.HasValue)
                customerOid = Filter.CustomerOid;

            if (customerOid.HasValue)
            {
                //VMFactory.CreateCustomerAppointmentListVM(customerOid, cb => { ActiveAppointmentViewModel = cb; });
				VMFactory.CreateMergedCustomerAppointmentListVM(customerOid, cb => { ActiveAppointmentViewModel = cb; });
            }
            else
            {
                if (BeWoApp.LoggedOnEmployee.EmployeeOid != null)
                {
					//VMFactory.CreateaAllCustomerAppointmentListVM(BeWoApp.LoggedOnEmployee.EmployeeOid.Value, cb => { ActiveAppointmentViewModel = cb; });
	                VMFactory.CreateMergedAllCustomerAppointmentListVM(BeWoApp.LoggedOnEmployee.EmployeeOid.Value, cb => { ActiveAppointmentViewModel = cb; });
                }
            }
        }

        private void UpdateResourceViewModel()
        {
	        VMFactory.CreateResourceAppointmentListVM(cb => { ActiveAppointmentViewModel = cb; });
        }

        private void UpdateEmployeeAbsenceTimeViewModel()
        {
            VMFactory.CreateEmployeeAbsenceTimeListVM(cb => { ActiveAppointmentViewModel = cb; });
        }

	    private void UpdateCommonCalendarViewModel()
	    {
		    VMFactory.CreateCommonCalendarListVM(cb => { ActiveAppointmentViewModel = cb; });
	    }

        public SchedulerSettings GetActiveSettings()
        {

            if (mType2Settings.ContainsKey(ActiveViewModelKind))
                return mType2Settings[ActiveViewModelKind];

            if (ActiveAppointmentViewModel != null)
            {
                SchedulerSettings settings = ActiveAppointmentViewModel.GetDefaultSchedulerSettings();
                mType2Settings.Add(ActiveViewModelKind, settings);

                return settings;
            }

            return null;
        }

        public void SetActiveViewType(SchedulerViewType newViewType)
        {
            var s = mType2Settings[ActiveViewModelKind];
            if (s != null)
                s.ViewType = (AppointmentViewType) ((int) newViewType);
        }

        public AbstractAppointmentEditForm GetEditAppointmentForm(SchedulerControl control, Appointment appointment)
        {
            if (ActiveAppointmentViewModel != null)
                return ActiveAppointmentViewModel.GetEditAppointmentForm(control, appointment);

            return null;
        }

        public void AddCustomFieldsMapping(SchedulerStorage schedulerStorage) 
        {
            if (ActiveAppointmentViewModel != null)
                ActiveAppointmentViewModel.AddCustomFieldsMapping(schedulerStorage);
        }

        internal void InitNewAppointment(Appointment appointment)
        {
            if (ActiveAppointmentViewModel != null)
                ActiveAppointmentViewModel.InitNewAppointment(appointment);
        }
    }
}
