using System.ComponentModel;
using System.Text;
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 WpfApp1.model;
using System.Threading.Tasks;
using System;
using WpfApp1.model;
using WpfApp1.model;
using WpfApp1.model;
using System.IO;
using System.Runtime.Serialization.Json;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void Invalidate()
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("WorkerList"));
        }
        public string selectedPos = "Должность";
        public WorkerAge? selectedAge = null;
        public string selectedSex = "Пол";

        
        private IEnumerable<Worker> _WorkerList;

        public IEnumerable<Worker> WorkerList
        {
            get
            {
                var res = _WorkerList;

                res = res
                    .Where(c => (c.Pos == selectedPos || selectedPos == "Должность"))
                    .Where(c => (selectedAge == null || (c.Age >= selectedAge.AgeFrom && c.Age < selectedAge.AgeTo)))
                    .Where(c => (c.Sex == selectedSex || selectedSex == "Пол"));
                if (searchFilter != "")
                    res = res.Where(c => c.name.IndexOf(
                        searchFilter,
                        StringComparison.OrdinalIgnoreCase) >= 0);
                if (sortAsc) res = res.OrderBy(c => c.Age);
                else res = res.OrderByDescending(c => c.Age);

                return res;

            }
            set
            {
                _WorkerList = value;
            }
        }
        

        public List<WorkerPos> WorkerPosList { get; set; }
        public List<WorkerAge> WorkerAgeList { get; set; }
        public List<WorkerSex> WorkerSexList { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            Globals.dataProvider = new JSONDataProvider();
            WorkerList = Globals.dataProvider.getWorker();
            WorkerPosList = Globals.dataProvider.getPos().ToList();
            WorkerPosList.Insert(0, new WorkerPos { title = "Должность" });
            WorkerAgeList = Globals.dataProvider.getAge().ToList();
            WorkerSexList = Globals.dataProvider.getSex().ToList();
            WorkerSexList.Insert(0, new WorkerSex { title = "Пол" });
            WorkerList = Globals.dataProvider.getWorker();
        }
        public class JSONDataProvider : LocalDataProvider, IDataProvider
        {
            private List<Worker> _WorkerList;

            public JSONDataProvider()
            {
                var serializer = new DataContractJsonSerializer(typeof(Worker[]));
                using (var sr = new StreamReader("./data.json"))
                {
                    _WorkerList = ((Worker[])serializer.ReadObject(sr.BaseStream)).ToList();

                }
            }
            public IEnumerable<Worker> getWorker()
            {
                return _WorkerList;
            }
        }
        private bool sortAsc = true;


        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();

        }

        private void PosFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedPos = (PosFilterComboBox.SelectedItem as WorkerPos).title;
            Invalidate();
        }
        private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedAge = (AgeFilterComboBox.SelectedItem as WorkerAge);
            Invalidate();
        }
        private void SexFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedSex = (SexFilterComboBox.SelectedItem as WorkerSex).title;
            Invalidate();
        }
        private string searchFilter = "";

        private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
        {
            searchFilter = SearchFilterTextBox.Text;
            Invalidate();
        }


        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            sortAsc = (sender as RadioButton).Tag.ToString() == "1";
            Invalidate();
        }
    }
    
    interface IDataProvider
    {
        IEnumerable<Worker> getWorker();
        IEnumerable<WorkerPos> getPos();
        IEnumerable<WorkerAge> getAge();
        IEnumerable<WorkerSex> getSex();
    }



    public class LocalDataProvider : IDataProvider
    {
        public IEnumerable<WorkerPos> getPos()
        {
            return new WorkerPos[]
            {
                new WorkerPos()
                {
                    title="Директор"

                },
                new WorkerPos()
                {
                    title="Продавец"
                },
                new WorkerPos()
                {
                    title="Механик"

                },
                new WorkerPos()
                {
                    title="Уборщик"

                },
                new WorkerPos()
                {
                    title="Стажер"

                },
                new WorkerPos()
                {
                    title="Оператор"

                },

            };
        }
        public IEnumerable<WorkerAge> getAge()
        {
            return new WorkerAge[]
            {
                new WorkerAge()
                {
                    title="Все возраста",
                    AgeFrom=0,
                    AgeTo=110

                },
                new WorkerAge()
                {
                    title="Дети",
                    AgeFrom=1,
                    AgeTo=6

                },
                new WorkerAge()
                {
                    title="Подростки",
                    AgeFrom=7,
                    AgeTo=21

                },
                new WorkerAge()
                {
                    title="Молодые",
                    AgeFrom=22,
                    AgeTo=50

                },
                new WorkerAge()
                {
                    title="Старики",
                    AgeFrom=51,
                    AgeTo=110

                }

            };

        }
        public IEnumerable<WorkerSex> getSex()
        {
            return new WorkerSex[]
            {
                new WorkerSex()
                {
                    title="Человек"
                },
                new WorkerSex()
                {
                    title="Женщина"
                }
            };
        }
        public IEnumerable<Worker> getWorker()
        {
            return new Worker[]{
            new Worker{
                Sex="Человек",
                name="Вася",
                Age=25,
                Pos="Директор"},
            new Worker{
                Sex="Человек",
                name="Петя",
                Age=45,
                Pos="Продавец"},
            new Worker{
                Sex="Человек",
                name="Степа",
                Age = 30,
                Pos="Механик"},
            new Worker{
                Sex="Человек",
                name="Миша",
                Age = 100,
                Pos="Стажер"},
            new Worker{
                Sex="Женщина",
                name="Лена",
                Age = 12,
                Pos="Оператор"}

        };


        }


    }

}
