using MVVM;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Work29.ViewModels
{
    public class ViewTasksWindowViewModel : BaseViewModel
    {
        private Task _task;

        private ObservableCollection<Task> _tasks;

        private ObservableCollection<StatusTask> _statusTasks;

        private StatusTask _statusTask;

        private RelayCommand _initialWindow;

        private RelayCommand _alterStatus;

        private RelayCommand _availableTasks;

        private RelayCommand _completedTasks;

        private RelayCommand _acceptTask;

        private RelayCommand _findUserTask;

        private string _userCreatedLogin;

        public RelayCommand FindUserTask
        {
            get
            {
                return _findUserTask ??
                    (_findUserTask = new RelayCommand(x =>
                    {
                        Work29Context context = new Work29Context();
                        var find = context.Users.FirstOrDefault(p => p.Login == UserCreatedLogin);

                        if (find != null)
                        {
                            Tasks = new ObservableCollection<Task>(context.Tasks.Where(p => p.UserCreated.Login == UserCreatedLogin));
                        }
                        else
                        {
                            MessageBox.Show("Такого пользователя не существует!!!");
                        }
                    }
                    ));
            }
        }

        public RelayCommand AcceptTask
        {
            get
            {
                return _acceptTask ??
                    (_acceptTask = new RelayCommand(x =>
                    {
                        if (x is Task task)
                        {
                            Work29Context context = new Work29Context();
                            if ((task.UserCreatedId == task.UserAcceptedId) && (task.UserCreatedId != User.a_user.Id))
                            {
                                task.UserAcceptedId = User.a_user.Id;
                                context.Entry(task).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                                task.StatusTaskId = 2;
                                task.StatusTask = context.StatusTasks.Find(2);
                            }
                            else
                            {
                                MessageBox.Show("Вы не можете принять свою задачу \nили эта задача занята!");
                            }
                            context.SaveChanges();
                            ItemsSourseUpdate();
                        }
                    }
                    ));
            }
        }

        public RelayCommand CompletedTasks
        {
            get
            {
                return _completedTasks ??
                    (_completedTasks = new RelayCommand(x =>
                    {
                        Work29Context context = new Work29Context();
                        Tasks = new ObservableCollection<Task>(context.Tasks.Where(p => p.StatusTaskId == 1 && p.UserAcceptedId == User.a_user.Id));
                    }
                    ));
            }
        }

        public RelayCommand AvailableTasks
        {
            get
            {
                return _availableTasks ??
                    (_availableTasks = new RelayCommand(x =>
                    {
                        Work29Context context = new Work29Context();
                        Tasks = new ObservableCollection<Task>(context.Tasks.Where(p => p.StatusTaskId == 3));
                    }
                    ));
            }
        }

        public RelayCommand AlterStatus
        {
            get
            {
                return _alterStatus ??
                    (_alterStatus = new RelayCommand(x =>
                    {
                        if (Task.UserCreatedId == User.a_user.Id)
                        {
                            Work29Context context = new Work29Context();
                            var check = context.Tasks.FirstOrDefault(p => p.Id == Task.Id);
                            check.StatusTaskId = Task.StatusTask.Id;
                            context.Tasks.Update(check);
                            context.SaveChanges();
                        }
                        else
                        {
                            MessageBox.Show("Менять стутус можно \nлишь у своих задач!");
                            Work29Context context = new Work29Context();
                            Tasks = new ObservableCollection<Task>(context.Tasks);
                            StatusTasks = new ObservableCollection<StatusTask>(context.StatusTasks);
                        }
                    }
                    ));
            }
        }

        public RelayCommand InitialWindow
        {
            get
            {
                return _initialWindow ??
                    (_initialWindow = new RelayCommand(x =>
                    {
                        Work29Context context = new Work29Context();
                        Tasks = new ObservableCollection<Task>(context.Tasks);
                    }
                    ));
            }
        }

        public string UserCreatedLogin
        {
            get => _userCreatedLogin;
            set
            {
                _userCreatedLogin = value;
                OnPropertyChanged();
            }
        }

        public void ItemsSourseUpdate()
        {
            Work29Context context = new Work29Context();
            Tasks = new ObservableCollection<Task>(context.Tasks);
        }

        public StatusTask StatusTask
        {
            get => _statusTask;
            set
            {
                _statusTask = value;
                OnPropertyChanged();
            }
        }

        public Task Task
        {
            get => _task;
            set
            {
                _task = value;
                OnPropertyChanged();
            }
        }

        public ObservableCollection<Task> Tasks
        {
            get => _tasks;
            set
            {
                _tasks = value;
                OnPropertyChanged();
            }
        }

        public ObservableCollection<StatusTask> StatusTasks
        {
            get => _statusTasks;
            set
            {
                _statusTasks = value;
                OnPropertyChanged();
            }
        }

        public ViewTasksWindowViewModel()
        {
            Work29Context context = new Work29Context();
            Tasks = new ObservableCollection<Task>(context.Tasks);
            StatusTasks = new ObservableCollection<StatusTask>(context.StatusTasks);
        }
    }
}
