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.Compact;

namespace BeWo.View.Search
{
    public class CustomerSearchView : GenericSearchView<CompactCustomerDC>
    {
        public List<CompactCustomerDC> Customers
        {
            get { return _ObjectList; }
            set { base.UpdateObjectList(value); }
        }

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

        public bool FetchReferenceNumbers { get; set; }

        protected override void GetAll(Action<List<CompactCustomerDC>> pCallBack)
        {
            // if (SearchMode == SearchMode.CostBearer)
            // ServiceFacade.DoCustomerServiceAsync(s => s.GetAllCostBearerCompact(), pCallBack);
            // else
            // base.GetAll(pCallBack);
            long? oid = null;

            if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.ViewAll) && !BeWoApp.LoggedOnUser.HasRight(UserRightType.CustomerView_View))
            {
                oid = BeWoApp.LoggedOnUser.Employee.EmployeeOid;
            }

            ServiceFacade.DoCustomerServiceAsync(
                s => s.GetAllActiveCustomersCompact(this.FetchReferenceNumbers, oid), 
                r =>
                    {
                        List<CompactCustomerDC> list = r;

                        if (oid != null)
                        {
                            list = list.Where(c => c.IsRelatedToEmployee).ToList();
                        }

                        list.Sort((x, y) => (x.LastName + ", " + x.FirstName).CompareTo(y.LastName + ", " + y.FirstName)); // (Comparison<CompactCustomerDC>)new CustomerSorter().GetComparison("LastName", BeWo.Core.AbstractSorter.SortDirection.Ascending));
                        pCallBack(list);
                    });
        }
    }
}