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

using BeWo.ServiceProxy;

using BS.Shared;
using BS.Shared.DataContracts;
using BS.Shared.Extensions;

namespace BeWo.View.Search
{
    public class TextbausteineSearchView : GenericSearchView<TextbausteinDC>
    {
        private long serviceCategoryOid;

        public long ServiceCategoryOid
        {
            get { return serviceCategoryOid; }
            set
            {
                serviceCategoryOid = value;

                ServiceFacade.DoOperationsServiceAsync(s => s.GetTextbausteineByServiceCategory(ServiceCategoryOid), r =>
                {
                    r = r.OrderBy(tb => tb.ServiceCategory != null).ThenBy(tb => tb.Position).ThenBy(tb => tb.Name).ToList();
                    this.Dispatch(() => UpdateObjectList(r));
                });
            }
        }

        public TextbausteineSearchView()
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                ThemeColor = FindResource("TextbausteinBrush") as Brush;
            }
        }

        protected override void GetAll(Action<List<TextbausteinDC>> pCallBack)
        {
            ServiceFacade.DoOperationsServiceAsync(s => s.GetTextbausteineByServiceCategory(ServiceCategoryOid), r =>
            {
                r = r.OrderBy(tb => tb.ServiceCategory == null).ThenBy(tb => tb.Position).ThenBy(tb => tb.Name).ToList();

                r = !BeWoApp.LoggedOnUser.HasRight(UserRightType.TextbausteineAlleAnsehen) ? 
                    r.Where(w => w.Employee.EmployeeOid.Equals(BeWoApp.LoggedOnEmployee.EmployeeOid)).ToList() :
                    r.Where(w => !(w.IsOnlyForEmployee && !w.Employee.EmployeeOid.Equals(BeWoApp.LoggedOnEmployee.EmployeeOid))).ToList();

                pCallBack(r);
            });
        }
    }
}
