using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Session1
{
    /// <summary>
    /// Логика взаимодействия для AgentList.xaml
    /// </summary>
    public partial class AgentList : Page
    {
        public AgentList()
        {
            InitializeComponent();
            agentList.ItemsSource = BaseConnect.baseModel.Agent.ToList();
            filter.DisplayMemberPath = "Title";
            foreach (AgentType at in BaseConnect.baseModel.AgentType)
                filter.Items.Add(at);
            filter.SelectedIndex = 0;
        }

        private bool SearchHelper(Agent agent)
        {
            return agent.Title.Contains(search.Text) || agent.Phone.Contains(search.Text) || agent.Email.Contains(search.Text);
        }

        private void UnitedChange()
        {
            List<Agent> newAgentList = BaseConnect.baseModel.Agent.ToList();
            if (filter.SelectedIndex > 0)
                newAgentList = newAgentList.Where(x => SearchHelper(x) && x.AgentType.ID == filter.SelectedIndex).ToList();
            else
                newAgentList = newAgentList.Where(x => SearchHelper(x)).ToList();
            if (sort.SelectedIndex == 0)
                newAgentList = newAgentList.OrderBy(x => x.Title).ToList();
            else if (sort.SelectedIndex == 1)
                newAgentList = newAgentList.OrderByDescending(x => x.Title).ToList();
            else if (sort.SelectedIndex == 2)
                newAgentList = newAgentList.OrderBy(x => x.AgentSaleSize).ToList();
            else if (sort.SelectedIndex == 3)
                newAgentList = newAgentList.OrderByDescending(x => x.AgentSaleSize).ToList();
            else if (sort.SelectedIndex == 4)
                newAgentList = newAgentList.OrderBy(x => x.Priority).ToList();
            else if (sort.SelectedIndex == 5)
                newAgentList = newAgentList.OrderByDescending(x => x.Priority).ToList();
            agentList.ItemsSource = newAgentList;
        }

        private void search_TextChanged(object sender, TextChangedEventArgs e)
        {
            UnitedChange();
        }

        private void filter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UnitedChange();
        }

        private void sort_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UnitedChange();
        }

        private void agentList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(agentList.SelectedItems.Count > 1)
            {
                changePriority.Visibility = Visibility.Visible;
            }
            else
            {
                changePriority.Visibility = Visibility.Hidden;
            }
        }

        private void ChangePriority_Click(object sender, RoutedEventArgs e)
        {
            List<Agent> selectedAgents = new List<Agent>();
            foreach(Agent agent in agentList.SelectedItems)
            {
                selectedAgents.Add(agent);
            }
            ChangePriority changePriority = new ChangePriority(selectedAgents);
            changePriority.ShowDialog();
            UnitedChange();
        }

        private void agentList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            EditAgent editAgent = new EditAgent((Agent)agentList.SelectedItem);
            editAgent.ShowDialog();
            UnitedChange();
        }

        private void addAgent_Click(object sender, RoutedEventArgs e)
        {
            AddAgent addAgent = new AddAgent();
            addAgent.ShowDialog();
            UnitedChange();
        }
    }
}
