using System.Windows;
using System.ComponentModel;
using System.Collections.Generic;
using WpfApp1.SQL;
using System;
using System.Linq;
using System.Windows.Input;
using System.Windows.Controls;
using WpfApp1.AddEdit;

namespace WpfApp1
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public List<string> sortList
        {
            get => Items.sortList.ToList();
        }

        private List<Agent> _AgentList;
        public List<Agent> AgentList
        {
            get
            {
                List<Agent> agentList = _AgentList;

                if (SearchText != null)
                    agentList = agentList.Where(item => item.Title.IndexOf(SearchText, StringComparison.OrdinalIgnoreCase) != -1).ToList();

                if (SortList && ChooseSortItem == 0)
                    return agentList.OrderBy(items => items.Title).ToList();
                else if (SortList && ChooseSortItem == 1)
                    return agentList.OrderBy(items => items.Title).ToList();
                else if (SortList && ChooseSortItem == 2)
                    return agentList.OrderBy(items => items.Priority).ToList();
                else if (!SortList && ChooseSortItem == 0)
                    return agentList.OrderBy(items => items.Title).ToList();
                else if (!SortList && ChooseSortItem == 1)
                    return agentList.OrderBy(items => items.Title).ToList();
                else
                    return agentList.OrderByDescending(items => items.Priority).ToList();
            }
            set
            {
                _AgentList = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("AgentList"));
                    PropertyChanged(this, new PropertyChangedEventArgs("SortList"));
                }
            }
        }

        private string _SearchText;
        public string SearchText
        {
            get => _SearchText;
            set
            {
                _SearchText = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("AgentList"));
                    PropertyChanged(this, new PropertyChangedEventArgs("SearchText"));
                }
            }
        }

        private byte _ChooseSortItem;
        public byte ChooseSortItem
        {
            get => _ChooseSortItem;
            set
            {
                _ChooseSortItem = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ChooseSortItem"));
                    PropertyChanged(this, new PropertyChangedEventArgs("AgentList"));
                }
            }
        }


        private bool _SortList = false;
        public bool SortList
        {
            get => _SortList;
            set
            {
                _SortList = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("SortList"));
                    PropertyChanged(this, new PropertyChangedEventArgs("AgentList"));
                    PropertyChanged(this, new PropertyChangedEventArgs("GetContentButtonSort"));
                }
            }
        }

        public string GetContentButtonSort
        {
            get => SortList ? "По возрастанию" : "По убыванию";
        }


        public MainWindow()
        {
            InitializeComponent();

            AgentList = Base.EM.Agent.ToList();

            DataContext = this;
        }

        private void TextBox_Name_KeyUp(object sender, KeyEventArgs e)
        {
            SearchText = TextBox_Name.Text;
        }

        private void Button_Sort_Click(object sender, RoutedEventArgs e)
        {
            if (SortList)
                SortList = false;
            else SortList = true;
        }

        private void ComboBox_Sort_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ChooseSortItem = (byte)ComboBox_Sort.SelectedIndex;
        }

        private void DataView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Agent item = ListView_Agent.SelectedItem as Agent;
            AddEditItems aei = new AddEditItems(item);

            if (aei.ShowDialog() == true)
            {
                AgentList = Base.EM.Agent.ToList();
                PropertyChanged(this, new PropertyChangedEventArgs("AgentList"));
            }
        }

        private void Button_Add_Click(object sender, RoutedEventArgs e)
        {
            Agent item = new Agent();
            AddEditItems aei = new AddEditItems(item);

            if (aei.ShowDialog() == true)
            {
                AgentList = Base.EM.Agent.ToList();
                PropertyChanged(this, new PropertyChangedEventArgs("AgentList"));
            }
        }

        private void Button_Exit_Click(object sender, RoutedEventArgs e) => Application.Current.Shutdown();
    }
}