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

namespace Rkis29.ViewModel
{
    public class VWMain : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string prop = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
        }
        public void DataGoM(DataGrid d)
        {
            List<Model.Task> t = ModelPublic.GetContext().Task.Where(w => w.AcceptedTaskId == Users.Id && w.StatusTaskId != 3).ToList();
            d.ItemsSource = t;
        }

        private string _Profil;
        public string LbProfil { get { return _Profil; } set { _Profil = value; OnPropertyChanged(); } }

        public void SaveFIO()
        {
            var p = ModelPublic.GetContext().Person.FirstOrDefault(f => f.Id == Users.Id);
            _Profil = p.SurnameP + " " + p.NameP + " " + p.PatronomycP;
        }

        private RelayCommand _dataGo;
        public RelayCommand DataGo
        {
            get
            {
                return _dataGo ?? new RelayCommand(obj =>
                {
                    var Grid = (DataGrid)obj;
                    DataGoM(Grid);
                    SaveFIO();
                    OnPropertyChanged();
                });
            }
        }
        public static void Status(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();
        }

        private RelayCommand _tbGo_Status;
        public RelayCommand TbGo_Status
        {
            get
            {
                return _tbGo_Status ?? new RelayCommand(obj =>
                {
                    DataGrid dataGrid = (DataGrid)obj;
                    if (dataGrid.SelectedItem is Model.Task t) //поиск выбранного элемента
                    {
                        if (t.StatusTaskId == 1)
                        {
                            Status(t.Id, 2, Users.Id);
                            DataGoM(dataGrid);
                            OnPropertyChanged();
                        }
                        else if (t.StatusTaskId == 2)
                        {
                            Status(t.Id, 3, Users.Id);
                            DataGoM(dataGrid);
                            OnPropertyChanged();
                        }
                    }
                });
            }
        }

        private RelayCommand _autho_ready;
        public RelayCommand Autho_ready
        {
            get
            {
                return _autho_ready ?? new RelayCommand(obj =>
                {
                    Tasks task = new Tasks();
                    task.Show();
                    Window window = (Window)obj;
                    window.Close();
                });
            }
        }
    }
}
