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 Demoexample
{
    /// <summary>
    /// Логика взаимодействия для Agents.xaml
    /// </summary>
    public partial class Agents : Page
    {
        ViewModel viewModel = new ViewModel();
        List<Agent> change;
        public Agents()
        {
            InitializeComponent();
            lbAllAgent.ItemsSource = viewModel.agents.ToList();
            lbAllAgent.Items.Refresh();
            change = viewModel.agents;
            Filter.ItemsSource = DataBase.BaseModel.AgentType.ToList();
            Filter.SelectedValuePath = "ID";
            Filter.DisplayMemberPath = "Title";
            Filter.SelectedIndex = 0;
        }
        
        public void ChangeList(object sender, RoutedEventArgs e)
        {
            change = viewModel.agents;
            if (Filter.SelectedIndex == 0)
            {
                change = viewModel.agents;
            }
            else
            {
                change = change.Where(x => x.AgentTypeID == Filter.SelectedIndex).ToList();
            }
            lbAllAgent.ItemsSource = change;

            switch (Sort.SelectedIndex)
            {
                case 0:
                    change = change.OrderBy(x => x.Title).ToList();
                    break;
                case 1:
                    change = change.OrderBy(x => x.Title).ToList();
                    change.Reverse();
                    break;
                case 2:
                    change = change.OrderBy(x => x.Sale).ToList();
                    break;
                case 3:
                    change = change.OrderBy(x => x.Sale).ToList();
                    change.Reverse();
                    break;
                case 4:
                    change = change.OrderBy(x => x.Priority).ToList();
                    break;
                case 5:
                    change = change.OrderBy(x => x.Priority).ToList();
                    change.Reverse();
                    break;
            }
            lbAllAgent.ItemsSource = change;


            if(Scan.Text != "")
            {
                change = change.Where(x => x.Title.Contains(Scan.Text)).ToList();
            }
            lbAllAgent.ItemsSource = change;
        }

        private void newAgent_Click(object sender, RoutedEventArgs e)
        {
            new NewAgent().ShowDialog();
        }

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