using System;
using System.Collections;
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 DemoTest_Boldin_41P
{
    /// <summary>
    /// Логика взаимодействия для pgAgentList.xaml
    /// </summary>
    public partial class pgAgentList : Page
    {
        VMMAgent VMMA;
        List<Agent> order;
        public pgAgentList()
        {
            InitializeComponent();    
            VMMA = new VMMAgent();
            order = VMMA.createList();
            lbAgents.ItemsSource =VMMA.createList();
            List<AgentType> at = BaseConnect.BaseModel.AgentType.ToList();
            cbFilter.Items.Add("Все типы");
            List<string> typesA = new List<string>();
            foreach(AgentType a in at)
            {
                typesA.Add(a.Title);
            }
            foreach (string s in typesA)
            {
                cbFilter.Items.Add(s);
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Filter();
        }

        void Filter()
        {
            order = VMMA.createList();
            if(tbFind.Text !="" && tbFind.Text != "Введите для поиска")
            {
                order = order.Where(x => x.Title.Contains(tbFind.Text) || x.Phone.Contains(tbFind.Text) || x.Email.Contains(tbFind.Text)).ToList();
            }
            if(cbFilter.SelectedIndex !=-1)
            {
                if(cbFilter.SelectedValue != "Все типы")
                {
                    order = order.Where(x => x.NameTypeAgent == cbFilter.SelectedValue).ToList();
                }    
            }
            if (cbOrder.SelectedIndex == 0)
            {
                order = order.OrderBy(x => x.Title).ToList();
            }
            if (cbOrder.SelectedIndex == 1)
            {
                order = order.OrderBy(x => x.Title).ToList();
                order.Reverse();
            }
            if (cbOrder.SelectedIndex == 2)
            {
                order = order.OrderBy(x => x.Sale).ToList();
            }
            if (cbOrder.SelectedIndex == 3)
            {
                order = order.OrderBy(x => x.Sale).ToList();
                order.Reverse();
            }
            if (cbOrder.SelectedIndex == 4)
            {
                order = order.OrderBy(x => x.Priority).ToList();
            }
            if (cbOrder.SelectedIndex == 5)
            {
                order = order.OrderBy(x => x.Priority).ToList();
                order.Reverse();
            }
            lbAgents.ItemsSource = order;
        }

        private void TextBox_MouseEnter(object sender, MouseEventArgs e)
        {
            if (tbFind.Text == "Введите для поиска")
                tbFind.Text = "";
        }

        private void tbFind_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (tbFind.Text != "Введите для поиска")
                Filter();
        }

        private void lbAgents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(lbAgents.SelectedItems.Count>1)
            {
                btnChangePriority.Visibility = Visibility.Visible;
            }
            else
                btnChangePriority.Visibility = Visibility.Collapsed;

        }

        private void btnChangePriority_Click(object sender, RoutedEventArgs e)
        {
            IList ls = lbAgents.SelectedItems;
            new windChangePriority(ls).ShowDialog();
            Filter();
            lbAgents.ItemsSource = order;
        }

        private void lbAgents_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            IList ls = lbAgents.SelectedItems;
            new windChangeAgent(ls).ShowDialog();
            Filter();
            lbAgents.ItemsSource = order;
        }

        private void btnAddAgent_Click(object sender, RoutedEventArgs e)
        {
            new windChangeAgent().ShowDialog();
            Filter();
            lbAgents.ItemsSource = order;
        }
    }
}
