using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Input;
using WPF.Framework.Commands.Base;
using WPF.Framework.Models;
using WPF.Framework.Models.Repository;
using WPF.Framework.Models.Repository.Base;

namespace WPF.Framework.ViewModels
{
    class AgentsViewModel : ViewModel
    {
        public AgentsViewModel()
        {
            agentRepository = new AgentRepository(new AgentsDbContext());
            agentTypeRepository = new Repository<AgentType>(new AgentsDbContext());
            agentList = agentRepository.Items.ToList();
            agentsCollectionViewSource = new CollectionViewSource()
            {
                Source = agentList
            };
            SortingDirectionsDictionary = new Dictionary<ListSortDirection, string>()
            {
                { ListSortDirection.Ascending, "По возрастанию" },
                { ListSortDirection.Descending, "По убыванию" }
            };
            AgentTypesFilterDictionary = InitializeAgentTypesFilterDictionary();
            AgentsSortingDictionary = InitializeAgentsSortingDictionary();

            CurrentListSortDirection = ListSortDirection.Ascending;
            CurrentSortingAgentProperty = AgentsSortingDictionary.First().Key;
            CurrentAgentTypeFilter = AgentTypesFilterDictionary.First().Key;

            agentsCollectionViewSource.Filter += OnSearchFilter;
            agentsCollectionViewSource.Filter += OnAgentTypesFilter;

            ListDirectionCommand = new LambdaCommad(OnListDirectionCommandExecuted);
        }

        private IRepository<Agent> agentRepository;
        private IRepository<AgentType> agentTypeRepository;
        private List<Agent> agentList;
        private CollectionViewSource agentsCollectionViewSource;

        private string searchText = "";
        public string SearchText
        {
            get => searchText;
            set => Set(ref searchText, ref value);
        }

        private ListSortDirection currentListSortDirection;
        public ListSortDirection CurrentListSortDirection
        {
            get => currentListSortDirection;
            set
            {
                if (Set(ref currentListSortDirection, ref value))
                {
                    agentsCollectionViewSource.SortDescriptions.Clear();
                    agentsCollectionViewSource.SortDescriptions.Add(new SortDescription(CurrentSortingAgentProperty, value));
                    AgentsCollectionView.Refresh();
                }
            }
        }

        private AgentType currentAgentTypeFilter;
        public AgentType CurrentAgentTypeFilter
        {
            get => currentAgentTypeFilter;
            set
            {
                if (Set(ref currentAgentTypeFilter, ref value))
                {
                    AgentsCollectionView.Refresh();
                }
            }
        }

        private string currentSortingAgentProperty;
        public string CurrentSortingAgentProperty
        {
            get => currentSortingAgentProperty;
            set
            {
                if (Set(ref currentSortingAgentProperty, ref value))
                {
                    agentsCollectionViewSource.SortDescriptions.Clear();
                    agentsCollectionViewSource.SortDescriptions.Add(new SortDescription(value, CurrentListSortDirection));
                    AgentsCollectionView.Refresh();
                }
            }
        }

        public Dictionary<ListSortDirection, string> SortingDirectionsDictionary { get; }
        public Dictionary<AgentType, string> AgentTypesFilterDictionary { get; }
        public Dictionary<string, string> AgentsSortingDictionary { get; }
        public ICollectionView AgentsCollectionView => agentsCollectionViewSource.View;

        private void OnAgentTypesFilter(object sender, FilterEventArgs e)
        {
            if (e.Item == null)
            {
                return;
            }

            Agent agent = (Agent)e.Item;
            if (agent.AgentType.ID != CurrentAgentTypeFilter.ID)
            {
                e.Accepted = false;
            }
        }

        private void OnSearchFilter(object sender, FilterEventArgs e)
        {
            if(e.Item == null)
            {
                return;
            }

            Agent agent = (Agent)e.Item;
            if(!agent.Title.Contains(SearchText) || !agent.Email.Contains(SearchText) || !agent.Phone.Contains(SearchText))
            {
                e.Accepted = false;
            }
        }

        public ICommand ListDirectionCommand { get; }
        private void OnListDirectionCommandExecuted(object obj)
        {
            if (CurrentListSortDirection == ListSortDirection.Descending)
            {
                CurrentListSortDirection = ListSortDirection.Ascending;
            }
            else
            {
                CurrentListSortDirection = ListSortDirection.Descending;
            }
        }



        private Dictionary<AgentType, string> InitializeAgentTypesFilterDictionary()
        {
            Dictionary<AgentType, string> dictionaty = new Dictionary<AgentType, string>();
            foreach (var item in agentTypeRepository.Items)
            {
                dictionaty.Add(item, item.Title);
            }
            return dictionaty;
        }
        private Dictionary<string, string> InitializeAgentsSortingDictionary()
        {
            Dictionary<string, string> dictionaty = new Dictionary<string, string>()
            {
                { nameof(Agent.Title), "Название" },
                { nameof(Agent.Discount), "Размер скидки" },
                { nameof(Agent.Priority), "Приоритет" },
            };
            return dictionaty;
        }
    }
}
