using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using BeWo.Core.Service;
using BS.Shared;
using BS.Shared.DataContracts.Compact;

namespace BeWo.View.Search
{
    public class SupportConceptSearchView : GenericSearchView<CompactSupportConceptDC>
    {
        public static readonly DependencyProperty CostBearerFilterProperty = DependencyProperty.Register(
            "CostBearerFilter", typeof (CompactOrganisationDC), typeof (SupportConceptSearchView), new UIPropertyMetadata(null, (s, e) => ((SupportConceptSearchView) s).UpdateFilteredList()));

        private CheckBox mChkBoxShowExpiredSupportConcepts;

        private CheckBox mChkBoxShowOnlyMyClients;

        private List<CompactSupportConceptDC> mHiddenSupportConcepts;

        private bool mIgnoreFilterChange;

        private bool mShowFilterPanel;

        public SupportConceptSearchView()
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                ThemeColor = FindResource("SupportConceptContentBrush") as Brush;
                ListItemStyle = FindResource("SupportConceptSearchStyle") as Style;
            }
        }

        public CompactOrganisationDC CostBearerFilter
        {
            get { return (CompactOrganisationDC) GetValue(CostBearerFilterProperty); }

            set { SetValue(CostBearerFilterProperty, value); }
        }

        public List<CompactSupportConceptDC> HiddenSupportConcepts
        {
            get { return mHiddenSupportConcepts; }

            set
            {
                mHiddenSupportConcepts = value;
                UpdateFilteredList();
            }
        }

        public List<CompactSupportConceptDC> SupportConcepts
        {
            get { return _ObjectList; }
            set { base.UpdateObjectList(value); }
        }

        public bool ShowFilterPanel
        {
            get { return mShowFilterPanel; }

            set
            {
                mShowFilterPanel = value;
                if (value)
                {
                    CreateFilterPanel();
                }
            }
        }

        public event EventHandler ControlInitialized;

        protected override void UpdateObjectList(List<CompactSupportConceptDC> list)
        {
            base.UpdateObjectList(list);
            if (ControlInitialized != null)
                ControlInitialized(this, new EventArgs());
        }

        protected void CreateFilterPanel()
        {
            if (BeWoApp.LoggedOnUser != null && mChkBoxShowOnlyMyClients == null)
            {
                mIgnoreFilterChange = true;
                var sp = new StackPanel {Margin = new Thickness(3, 3, 3, 3), Orientation = Orientation.Horizontal};

                mChkBoxShowOnlyMyClients = new CheckBox {Content = "Nur eigene Klienten anzeigen"};
                mChkBoxShowOnlyMyClients.Checked += FilterCheckBoxChanged;
                mChkBoxShowOnlyMyClients.Unchecked += FilterCheckBoxChanged;
                mChkBoxShowOnlyMyClients.FontSize = 12;
                mChkBoxShowOnlyMyClients.VerticalAlignment = VerticalAlignment.Center;
                var b = FindResource("MainGroupBoxForegroundBrush") as Brush;
                if (b != null)
                {
                    mChkBoxShowOnlyMyClients.Foreground = b;
                }

                sp.Children.Add(mChkBoxShowOnlyMyClients);

                mChkBoxShowExpiredSupportConcepts = new CheckBox {Content = "Abgelaufene Hilfepläne anzeigen", Margin = new Thickness(5, 0, 0, 0)};
                mChkBoxShowExpiredSupportConcepts.Checked += FilterCheckBoxChanged;
                mChkBoxShowExpiredSupportConcepts.Unchecked += FilterCheckBoxChanged;
                mChkBoxShowExpiredSupportConcepts.FontSize = 12;
                mChkBoxShowExpiredSupportConcepts.VerticalAlignment = VerticalAlignment.Center;

                if (b != null)
                {
                    mChkBoxShowExpiredSupportConcepts.Foreground = b;
                }

                if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.CustomerView_View) && !BeWoApp.LoggedOnUser.HasRight(UserRightType.ViewAll))
                {
                    mChkBoxShowOnlyMyClients.IsChecked = true;
                    mChkBoxShowOnlyMyClients.IsEnabled = false;
                }
                else
                {
                    mChkBoxShowOnlyMyClients.IsChecked = !BeWoApp.AppSettings.ShowAllClients;
                }

                mChkBoxShowExpiredSupportConcepts.IsChecked = BeWoApp.AppSettings.ShowExpiredSupportConcepts;

                sp.Children.Add(mChkBoxShowExpiredSupportConcepts);

                AddCustomContentHeaderElement(sp);
                mIgnoreFilterChange = false;
            }
        }

        protected override void GetAll(Action<List<CompactSupportConceptDC>> pCallBack)
        {
            if (SearchMode == SearchMode.ActiveApprovedLVRSupportConcepts)
            {
                Cache.GetInstance().GetAllActiveAndApprovedSupportConceptCostbearer(pCallBack);
            }
            else if (SearchMode == SearchMode.ActiveCostbearer2SupportConcepts)
            {
                Cache.GetInstance().GetActiveSupportConceptCostbearer(pCallBack);
            }
            else
            {
                Cache.GetInstance().GetAllActiveSupportConceptsCompact(pCallBack);
            }
        }

        protected override bool ShowDCInList(object dc)
        {
            bool show = true;

            var scDC = dc as CompactSupportConceptDC;

            if (scDC != null)
            {
                if (CostBearerFilter != null && !scDC.CostBearerList.Exists(i => i.CostBearerOid.Equals(CostBearerFilter.CostBearerOid)))
                {
                    return false;
                }

                if (ShowFilterPanel)
                {
                    if (!mChkBoxShowExpiredSupportConcepts.IsChecked.Value)
                    {
                        DateTime? endDate = scDC.EndDate;
                        if (endDate != null)
                        {
                            show = endDate >= DateTime.Now.Date;
                        }
                    }

                    if (show && mChkBoxShowOnlyMyClients.IsChecked.Value)
                    {
                        show = scDC.IsRelatedToEmployee;
                    }
                }

                if (show && HiddenSupportConcepts != null)
                {
                    foreach (CompactSupportConceptDC item in HiddenSupportConcepts)
                    {
                        if (item.Customer.Equals(scDC.Customer))
                        {
                            return false;
                        }
                    }
                }
            }

            return show;
        }

        private void FilterCheckBoxChanged(object sender, RoutedEventArgs e)
        {
            if (!mIgnoreFilterChange)
            {
                BeWoApp.AppSettings.ShowAllClients = !mChkBoxShowOnlyMyClients.IsChecked.Value;
                BeWoApp.AppSettings.ShowExpiredSupportConcepts = mChkBoxShowExpiredSupportConcepts.IsChecked.Value;
                BeWoApp.SaveAppSettings();
                UpdateFilteredList();
            }
        }
    }
}