using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Rkis29.Model;
using Rkis29.View;
using System.Windows.Controls;
using System.Windows;

namespace Rkis29.ViewModel
{
    internal class VWTask : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string prop = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
        }

        private RelayCommand _data; //добавление значений в грид
        public RelayCommand Data
        {
            get
            {
                return _data ?? new RelayCommand(obj =>
                {
                    DataGrid dataGrid = (DataGrid)obj;
                    DAtaGo(dataGrid);
                    DataCombo();
                    OnPropertyChanged();
                });
            }
        }

        private RelayCommand _chandgeBtn; //добавление значений в грид
        public RelayCommand ChandgeBtn
        {
            get
            {
                return _chandgeBtn ?? new RelayCommand(obj =>
                {
                    DataGrid dataGrid = (DataGrid)obj; //объект грида
                    if (dataGrid.SelectedItem is Model.Task t) //поиск выбранного элемента
                    {
                        if (t.StatusTaskId == 1)
                        {
                            ChangeStatus(t.Id, 2, Users.Id);  //смена статуса
                            MessageBox.Show("Вы выполняете задачу");
                            Method(dataGrid);
                            OnPropertyChanged();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Задача не выбрана");
                    }

                });
            }
        }


        public static void ChangeStatus(int IdTask, int Status, int IdPerson)
        {
            var t = ModelPublic.GetContext().Task.FirstOrDefault(w => w.Id == IdTask);
            t.StatusTaskId = Status;
            t.AcceptedTaskId = IdPerson;
            ModelPublic.GetContext().SaveChanges();
        }

        public void Method(DataGrid data)
        {
            List<Model.Task> people = ModelPublic.GetContext().Task.Where(w => w.StatusTaskId == 1 && w.AcceptedTaskId == w.CreaterTaskId).ToList();
            data.ItemsSource = people;
        }

        private RelayCommand _searchBtn;
        public RelayCommand SearchBtn //кнопка поиска
        {
            get
            {
                return _searchBtn ?? new RelayCommand(obj =>
                {
                    Window window = (Window)obj;
                    DataGrid dataGrid = window.FindName("PoiskGrid") as DataGrid;
                    //параметры для поиска
                    dataGrid.ItemsSource = ModelPublic.GetContext().Task.Where(w => w.Person.Logins == _login.Logins && w.StatusTaskId == 1 && w.Person1.Logins == _login.Logins).ToList();

                });
            }
        }

        private RelayCommand _backBtn;
        public RelayCommand BackBtn
        {
            get
            {
                return _backBtn ?? new RelayCommand(obj =>
                {
                    Registration main = new Registration();
                    main.Show();
                    Window window = (Window)obj;
                    window.Close();
                });
            }
        }
        private Person _login;
        public Person Login { get { return _login; } set { _login = value; OnPropertyChanged(); } }


        public void DAtaGo(DataGrid data)
        {
            List<Model.Task> people = ModelPublic.GetContext().Task.Where(w => w.StatusTaskId == 1 && w.AcceptedTaskId == w.CreaterTaskId).ToList();
            data.ItemsSource = people;
        }
        public void DataCombo() //реализация поиска через комбобокс
        {
            var p = ModelPublic.GetContext().Person.ToList();
            DataComboGo = p;
        }
        private List<Person> _dataComboGo;
        public List<Person> DataComboGo
        { get { return _dataComboGo; } set { _dataComboGo = value; OnPropertyChanged(); } }
    }
}
