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 BuzuevaDemo.Classes;

namespace BuzuevaDemo.Pages
{
    /// <summary>
    /// Логика взаимодействия для Agents.xaml
    /// </summary>
    public partial class Agents : Page
    {
        ViewModel view = new ViewModel();
        List<Agent> filter;
        List<Agent> sortirov;
        List<Agent> filtrasia;
        List<Agent> agents = new List<Agent>();
        public string[] typelist = new string[6];
        public Agents()
        {
            InitializeComponent();
            AllAgent.ItemsSource = view.agent.ToList();
            AllAgent.Items.Refresh();
            filter = view.agent.ToList();
            sortirov = view.agent.ToList();
            filtrasia = view.agent.ToList();
            List<AgentType> type = DataBase.BaseModel.AgentType.ToList();
            int i = 0;
            foreach (AgentType tps in type)
            {
                typelist[i] = tps.Title;
                i++;
            }
            tb2.Text = typelist[0];
            tb3.Text = typelist[1];
            tb4.Text = typelist[2];
            tb5.Text = typelist[3];
            tb6.Text = typelist[4];
            tb7.Text = typelist[5];
        }

        private void Changeg(object sender, TextChangedEventArgs e)
        {
            filter = view.agent;
            if (poisk.Text != "")
            {
                filter = filter.Where(x => x.Title.Contains(poisk.Text) || x.Email.Contains(poisk.Text) || x.Phone.Contains(poisk.Text)).ToList();

            }
            AllAgent.ItemsSource = filter.ToList();

            
        }

        private void PerexodCreate(object sender, MouseButtonEventArgs e)
        {
            StackPanel zapis = (StackPanel)sender;
            int id = Convert.ToInt32(zapis.Uid);
            Agent agent = DataBase.BaseModel.Agent.FirstOrDefault(x => x.ID == id);
            new CreateChange(agent, 0).ShowDialog();
        }

        private void PerexodСhange(object sender, RoutedEventArgs e)
        { 
            new CreateChange(null, 1).ShowDialog();
        }

        private void sortSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (sort.SelectedIndex)
            {
                case 0:
                    sortirov = sortirov.OrderBy(x => x.Title).ToList();
                    AllAgent.ItemsSource = sortirov.ToList();
                    break;
                case 1:
                    sortirov = sortirov.OrderBy(x => x.Title).ToList();
                    sortirov.Reverse();
                    AllAgent.ItemsSource = sortirov.ToList();
                    break;
                case 2:
                    sortirov = sortirov.ToList();
                    break;
                case 3:
                    sortirov = sortirov.ToList();
                    break;
                case 4:
                    sortirov = sortirov.OrderBy(x => x.Priority).ToList();
                    AllAgent.ItemsSource = sortirov.ToList();
                    break;
                case 5:
                    sortirov = sortirov.OrderBy(x => x.Priority).ToList();
                    sortirov.Reverse();
                    AllAgent.ItemsSource = sortirov.ToList();
                    break;
                default:
                    sortirov = sortirov.ToList();
                    break;
                   
            } 
            AllAgent.ItemsSource = sortirov.ToList();
        }


        private void filtSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            filtrasia = view.agent;
            switch (filt.SelectedIndex)
            {
                case 0: 
                    break;
                case 1:
                    filtrasia = filtrasia.Where(x => x.TitleType == typelist[0]).ToList();
                    break;
                case 2:
                    filtrasia = filtrasia.Where(x => x.TitleType == typelist[1]).ToList();
                    break;
                case 3:
                    filtrasia = filtrasia.Where(x => x.TitleType == typelist[2]).ToList();
                    break;
                case 4:
                    filtrasia = filtrasia.Where(x => x.TitleType == typelist[3]).ToList();
                    break;
                case 5:
                    filtrasia = filtrasia.Where(x => x.TitleType == typelist[4]).ToList();
                    break;
                case 6:
                    filtrasia = filtrasia.Where(x => x.TitleType == typelist[5]).ToList();
                    break;
                    
            }
            AllAgent.ItemsSource = filtrasia.ToList();
        }
    }
}
