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;
using DemoexamUser11.classes;
using DemoexamUser11.windows;

namespace DemoexamUser11.pages
{
    /// <summary>
    /// Логика взаимодействия для AgentsList.xaml
    /// </summary>
    public partial class AgentsList : Page
    {
        List<string> sortList = new List<string>() { "Наименование", "Размер скидки", "Приоритет" };
        List<Agent> agents = BaseConnector.BaseConnect.Agent.ToList();
        List<Agent> agentsForFilters = BaseConnector.BaseConnect.Agent.ToList();
        List<AgentType> agentTypes = BaseConnector.BaseConnect.AgentType.ToList();        
        public AgentsList()
        {
            InitializeComponent();
            listBoxAgents.ItemsSource = agents;

            agentTypes.Add(new AgentType { ID = 9999, Title = "Все типы" });
            comboBoxFilter.ItemsSource = agentTypes;
            comboBoxFilter.DisplayMemberPath = "Title";
            comboBoxFilter.SelectedValuePath = "ID";
            comboBoxFilter.SelectedIndex = comboBoxFilter.Items.Count - 1;

            comboBoxSort.ItemsSource = sortList;
        }

        private void Search(object sender, TextChangedEventArgs e)
        {
            if (textBoxSearch.Text.Replace(" ", "") != "")
            {
                try
                {
                    agentsForFilters = agentsForFilters.Where(x => x.Title.Contains(textBoxSearch.Text) || x.Phone.Contains(textBoxSearch.Text) || x.Email.Contains(textBoxSearch.Text)).ToList();
                    listBoxAgents.ItemsSource = agentsForFilters;
                }
                catch
                {
                    MessageBox.Show("Не удалось выполнить поиск!");
                }
            }
            if (textBoxSearch.Text.Replace(" ", "") == "" && (int)comboBoxFilter.SelectedValue == 9999)
            {
                listBoxAgents.ItemsSource = agents;
                agentsForFilters = BaseConnector.BaseConnect.Agent.ToList();
            }
            else if (textBoxSearch.Text.Replace(" ", "") == "" && (int)comboBoxFilter.SelectedValue != 9999)
            {
                listBoxAgents.ItemsSource = agents.Where(x => x.AgentTypeID == (int)comboBoxFilter.SelectedValue).ToList();
                agentsForFilters = BaseConnector.BaseConnect.Agent.ToList();
            }
        }

        private void Filter(object sender, SelectionChangedEventArgs e)
        {
            if (comboBoxFilter.SelectedItem != null && (int)comboBoxFilter.SelectedValue != 9999)
            {
                agentsForFilters = agentsForFilters.Where(x => x.AgentTypeID == (int)comboBoxFilter.SelectedValue).ToList();
            }
            if ((int)comboBoxFilter.SelectedValue == 9999)
                agentsForFilters = BaseConnector.BaseConnect.Agent.ToList();
            if (comboBoxSort.SelectedItem != null)
            {
                switch (comboBoxSort.SelectedValue)
                {
                    case "Наименование":
                        agentsForFilters.Sort(delegate(Agent a1, Agent a2) 
                        { 
                            return a1.Title.CompareTo(a2.Title); 
                        });
                        break;
                    case "Размер скидки":
                        break;
                    case "Приоритет":
                        agentsForFilters.Sort(delegate (Agent a1, Agent a2)
                        {
                            return a1.Priority.CompareTo(a2.Priority);
                        });
                        break;
                }
            }
            
            listBoxAgents.ItemsSource = agentsForFilters;
        }

        private void buttomBigger_Click(object sender, RoutedEventArgs e)
        {
            agentsForFilters.Reverse();
            listBoxAgents.ItemsSource = agentsForFilters;
            listBoxAgents.Items.Refresh();
        }

        private void buttomChangePriority_Click(object sender, RoutedEventArgs e)
        {
            if (listBoxAgents.SelectedItems.Count != 0)
            {
                List<Agent> agentsListNew = new List<Agent>();
                foreach (var item in listBoxAgents.SelectedItems)
                {
                    agentsListNew.Add((Agent)item);
                }
                WindowChangePriority changePriority = new WindowChangePriority(agentsListNew);
                changePriority.ShowDialog();
                listBoxAgents.Items.Refresh();
            }
        }
    }
}
