using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
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 PracticaDemoexam.Model;
using PracticaDemoexam.Model.ItemModels;
using PracticaDemoexam.Windows;

namespace PracticaDemoexam.Pages
{
    /// <summary>
    /// Логика взаимодействия для Agents.xaml
    /// </summary>
    public partial class Agents : Page
    {
        user34Entities entities = new user34Entities();

        bool _sortDir = true;
        int _pageIndex = 0;
        List<AgentModel> _unchangedAgents = new List<AgentModel>();
        List<AgentModel> _changedAgents = new List<AgentModel>();
        List<List<AgentModel>> Pages = new List<List<AgentModel>> { new List<AgentModel>() };
        List<AgentModel> Page
        {
            get
            {
                return Pages[_pageIndex];
            }
        }
        List<string> PageNumbers = new List<string>();

        List<string> sortingParameter = new List<string>
        {
            "без сортировки",
            "наименование",
            "размер скидки",
            "приоритет"
        };

        List<string> filteringParameter = new List<string>
        {
            "Все типы"
        };

        public Agents()
        {
            InitializeComponent();

            LoadThings();

            lbAgents.ItemsSource = Page;
            lbPogination.ItemsSource = PageNumbers;
            cbSort.ItemsSource = sortingParameter;
            cbFilter.ItemsSource = filteringParameter;
        }

        private void LoadThings()
        {
            AgentModel agentModel;
            int i = 0, j = 0;
            foreach (Agent agent in entities.Agent.ToList())
            {
                agentModel = new AgentModel { Agent = agent };
                _unchangedAgents.Add(agentModel);
                _changedAgents.Add(agentModel);
                Pages[i].Add(agentModel);
                j++;
                if (j == 10)
                {
                    PageNumbers.Add((i + 1).ToString());
                    i++;
                    j = 0;
                    Pages.Add(new List<AgentModel>());
                }
            }
            if (Pages.Count > PageNumbers.Count)
            {
                PageNumbers.Add((i + 1).ToString());
            }

            foreach (AgentType type in entities.AgentType.ToList())
            {
                filteringParameter.Add(type.Title);
            }
        }

        private void tbSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (string.IsNullOrEmpty(tbSearch.Text) || string.IsNullOrWhiteSpace(tbSearch.Text))
            {
                _changedAgents = _unchangedAgents;
            }
            else
            {
                _changedAgents = _changedAgents.Where(x => (x.Agent.Title + " " + x.Agent.Email + " " + x.Agent.Phone).Contains(tbSearch.Text)).ToList();
            }
            int i = 0, j = 0;
            Pages.Clear();
            foreach (AgentModel agent in _changedAgents)
            {
                Pages[i].Add(agent);
                j++;
                if (j == 10)
                {
                    i++;
                    j = 0;
                    Pages.Add(new List<AgentModel>());
                }
            }

        }

        private void cbSort_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Sort();
        }

        private void Sort()
        {
            switch (cbFilter.SelectedIndex)
            {
                case 0:
                    if (_sortDir)
                    {
                        _changedAgents.OrderBy(x => x.Agent.ID);
                    }
                    else
                    {
                        _changedAgents.OrderByDescending(x => x.Agent.ID);
                    }
                    break;
                case 1:
                    if (_sortDir)
                    {
                        _changedAgents.OrderBy(x => x.Agent.Title);
                    }
                    else
                    {
                        _changedAgents.OrderByDescending(x => x.Agent.Title);
                    }
                    break;
                case 2:
                    if (_sortDir)
                    {
                        _changedAgents.OrderBy(x => x.Discount);
                    }
                    else
                    {
                        _changedAgents.OrderByDescending(x => x.Discount);
                    }
                    break;
                case 3:
                    if (_sortDir)
                    {
                        _changedAgents.OrderBy(x => x.Agent.Priority);
                    }
                    else
                    {
                        _changedAgents.OrderByDescending(x => x.Agent.Priority);
                    }
                    break;

            }
            int i = 0, j = 0;
            Pages.Clear();
            foreach (AgentModel agent in _changedAgents)
            {
                Pages[i].Add(agent);
                j++;
                if (j == 10)
                {
                    i++;
                    j = 0;
                    Pages.Add(new List<AgentModel>());
                }
            }
        }

        private void cbFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((cbFilter.SelectedValue as string) == "Все типы")
            {
                _changedAgents = _unchangedAgents;
            }
            else
            {
                _changedAgents = _changedAgents.Where(x => x.Agent.AgentType.Title == (cbFilter.SelectedValue as string)).ToList();
            }
            int i = 0, j = 0;
            Pages.Clear();
            foreach (AgentModel agent in _changedAgents)
            {
                Pages[i].Add(agent);
                j++;
                if (j == 10)
                {
                    i++;
                    j = 0;
                    Pages.Add(new List<AgentModel>());
                }
            }
        }

        private void bLeft_Click(object sender, RoutedEventArgs e)
        {
            _pageIndex++;
            if (_pageIndex > Convert.ToInt32(PageNumbers.Last()) - 1)
            {
                _pageIndex = 0;
            }
            lbAgents.ItemsSource = Page;
        }

        private void lbPogination_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _pageIndex = Convert.ToInt32((lbPogination.SelectedValue as string)) - 1;
            lbAgents.ItemsSource = Page;
        }

        private void bRight_Click(object sender, RoutedEventArgs e)
        {
            _pageIndex--;
            if (_pageIndex < Convert.ToInt32(PageNumbers.First()) - 1)
            {
                _pageIndex = 0;
            }
            lbAgents.ItemsSource = Page;
        }

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            if ((sender as RadioButton).Name == "rbUb")
            {
                _sortDir = false;
            }
            else
            {
                _sortDir = true;
            }
            Sort();
        }

        ChangePriority cp;

        private void lbAgents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cp == null)
            {
                cp = new ChangePriority();
                cp.Button_click = Button_Click;
            }
            cp.Show();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<AgentModel> ls = new List<AgentModel>();

            foreach (AgentModel am in lbAgents.SelectedItems)
            {
                ls.Add(am);
            }

            pc = new PriorityChanger(ls);

            pc.Button_click = ChangePriority;

            pc.ShowDialog();
        }

        PriorityChanger pc;

        private void ChangePriority(List<AgentModel> ls, int priority)
        {
            foreach(Agent agent in entities.Agent.ToList())
            {
                if(ls.FirstOrDefault(x=> x.Agent.ID == agent.ID) != null)
                {
                    agent.Priority = priority;
                }
            }

            pc.Close();

            entities.SaveChanges();
            LoadThings();

            lbAgents.ItemsSource = null;
            lbPogination.ItemsSource = null;
            cbSort.ItemsSource = null;
            cbFilter.ItemsSource = null;

            lbAgents.ItemsSource = Page;
            lbPogination.ItemsSource = PageNumbers;
            cbSort.ItemsSource = sortingParameter;
            cbFilter.ItemsSource = filteringParameter;
        }
    }
}
