using System;
using System.Collections.Generic;
using System.ComponentModel;

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

using BeWo.ViewModel;
using BeWo.ViewModel.ListViewModel;
using BeWo.Scheduler.Resources;
using BeWo.Scheduler.View;

using DevExpress.XtraScheduler;

namespace BeWo.Scheduler.ViewModel
{
    public class SupportConceptAppointmentListVM : AbstractDCListMapperVM<SupportConceptDC, SupportConceptVM>, ISchedulerViewModel
    {
        private List<BeWoAppointmentCategory> mAppointmentCategoryList;
        private IList<IBeWoAppointmentResource> mAppointmentResourceList;
        private readonly BindingList<IBeWoAppointment> mAppointmentList = null;

        public SupportConceptAppointmentListVM(IEnumerable<SupportConceptDC> pDCList)
            : base(pDCList)
        {
            InitViewModel();
        }

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

            var oid2Category = new Dictionary<long, BeWoAppointmentCategory>();
            var oid2Resource = new Dictionary<long, IBeWoAppointmentResource>();
            var oid2Customer = new Dictionary<long, CompactCustomerDC>();
            var reasonOid2Index = new Dictionary<long, int>();

            //foreach (var item in mAllEmployees)
            //{
            //     if (!oid2Employee.ContainsKey(item.EmployeeOid))
            //     {
            //         oid2Employee.Add(item.EmployeeOid, item);
            //     }
            //}

            //int index = 0;
            //foreach (var item in mAllReasons)
            //{
            //    BeWoAppointmentCategory cat = new BeWoAppointmentCategory();
            //    cat.CategoryObject = item;
            //    cat.Color = ((SolidColorBrush)new BrushConverter().ConvertFrom(item.Color)).Color;
            //    cat.DisplayName = item.Description;
            //    cat.MenuCaption = item.Description;
            //    if (!oid2Category.ContainsKey(item.AbsenceReasonOid.Value))
            //        oid2Category.Add(item.AbsenceReasonOid.Value, cat);
            //    mAppointmentCategoryList.Add(cat);

            //    if (!reasonOid2Index.ContainsKey(item.AbsenceReasonOid.Value))
            //        reasonOid2Index.Add(item.AbsenceReasonOid.Value, index);

            //    index++;
            //}

            foreach (var item in VMList)
            {
                //    if (item.DataContract.EmployeeOid.HasValue)
                //    {
                //        IBeWoAppointmentResource res = null;

                //        if (oid2Resource.ContainsKey(item.DataContract.EmployeeOid.Value))
                //            res = oid2Resource[item.DataContract.EmployeeOid.Value];
                //        else
                //        {
                //            if (oid2Employee.ContainsKey(item.DataContract.EmployeeOid.Value))
                //            {
                //                res = new EmployeeResource(oid2Employee[item.DataContract.EmployeeOid.Value]);
                //                oid2Resource.Add(item.DataContract.EmployeeOid.Value, res);
                //                mAppointmentResourceList.Add(res);
                //            }
                //        }
                //        if (res != null)
                //        {
                //            item.ResourceId = item.DataContract.EmployeeOid.Value;
                //        }

                //    }

                //    if (item.Reason != null && oid2Category.ContainsKey(item.Reason.AbsenceReasonOid.Value))
                //        item.AppointmentCategory = oid2Category[item.Reason.AbsenceReasonOid.Value];

                //    if (reasonOid2Index.ContainsKey(item.Reason.AbsenceReasonOid.Value))
                //        item.LabelId = reasonOid2Index[item.Reason.AbsenceReasonOid.Value];
            }

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



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

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

	    public object SelectedFilterObject { get; set; }

	    public BindingList<IBeWoAppointment> Appointments
        {
            get
            {
                return mAppointmentList;
            }
        }

        public SchedulerSettings GetDefaultSchedulerSettings()
        {
            var settings = new SchedulerSettings(AppointmentKind.SupportConcept, AppointmentViewType.Month)
            {
                StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1),
                AllowChangeResource = false,
                AllowEdit = false,
                AllowDelete = false,
                AllowCreateNew = false
            };

            return settings;
        }


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

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

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

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

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

        public void InitNewAppointment(Appointment appointment)
        {
            //
        }

        public bool FilterAppointment(string filterCategory, object selectedObject, Appointment appointment, TimeInterval interval)
        {

            return false;
        }
    }
}