using esoft.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace esoft.Class
{
    /// <summary>
    /// Составления списка для отображения пользователю в зависимости от роли
    /// </summary>
    public class TaskFill : esoft.Entities.Task
    {        
        string fioManager;
        string fioUser;

        public string FioManager
        {
            get { return fioManager; } 
            private set { fioManager = value; }
        }
        public string FioUser
        {
            get { return fioUser; }
            private set { fioUser = value; }
        }

        /// <summary>
        /// Собирает коллекцию для вывода задач пользователя
        /// </summary>
        /// <param name="actualContext">Контекст базы даных</param>
        /// <param name="user">Пользователь</param>
        /// <returns>Список отобранный для пользователя</returns>
        public static List<TaskFill> TaskFills(User user)
        {
            try
            {
                ActualContext actualContext = new ActualContext();
                List<Task> tasks = esoftEntities.GetContext().Task.ToList();
                List<TaskFill> taskFills = new List<TaskFill>();
                for (int i = 0; i < tasks.Count; i++)
                {
                    taskFills.Add(new TaskFill
                    {
                        ID = tasks[i].ID,
                        ExecutorID = tasks[i].ExecutorID,
                        Executor = tasks[i].Executor,
                        Title = tasks[i].Title,
                        Description = tasks[i].Description,
                        CreateDateTime = tasks[i].CreateDateTime,
                        Deadline = tasks[i].Deadline,
                        Difficulty = tasks[i].Difficulty,
                        Time = tasks[i].Time,
                        Status = tasks[i].Status,
                        WorkType = tasks[i].WorkType,
                        CompletedDateTime = tasks[i].CompletedDateTime,
                        IsDeleted = tasks[i].IsDeleted,
                        FioUser = UserInFIO.SoloUser(
                            actualContext.Users.FirstOrDefault(
                                x => x.ID == tasks[i].ExecutorID)),
                        FioManager = UserInFIO.SoloUser(
                            actualContext.Users.FirstOrDefault(
                                x => x.ID == actualContext.Executor.FirstOrDefault(
                                    z => z.ID == tasks[i].ExecutorID).ManagerID))           
                    });
                }
                if (actualContext.Manager.FirstOrDefault(x => x.ID == user.ID) == null) 
                {
                    return taskFills.OrderByDescending(
                    x => x.CreateDateTime).Where(x => x.FioUser == UserInFIO.SoloUser(user)).ToList();
                }
                else
                {
                    return taskFills.OrderByDescending(
                    x => x.CreateDateTime).Where(x => x.FioManager == UserInFIO.SoloUser(user)).ToList();
                }
                
            }
            catch
            {
                MessageBox.Show("Ошибка подключчения",
                    "Проверте подключение к интернету",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
                return null;
            }
        }
        /// <summary>
        /// Вывод всех задач (без удалённых)
        /// </summary>
        /// <param name="actualContext">Контекст базы данных</param>
        /// <returns>Возвращает список</returns>
        public static List<TaskFill> TaskFills()
        {
            try
            {
                ActualContext actualContext = new ActualContext();
                List<Task> tasks = esoftEntities.GetContext().Task.ToList().Where(x => x.IsDeleted == false).ToList();
                List<TaskFill> taskFills = new List<TaskFill>();
                for (int i = 0; i < tasks.Count; i++)
                {
                    taskFills.Add(new TaskFill
                    {
                        ID = tasks[i].ID,
                        ExecutorID = tasks[i].ExecutorID,
                        Executor = tasks[i].Executor,
                        Title = tasks[i].Title,
                        Description = tasks[i].Description,
                        CreateDateTime = tasks[i].CreateDateTime,
                        Deadline = tasks[i].Deadline,
                        Difficulty = tasks[i].Difficulty,
                        Time = tasks[i].Time,
                        Status = tasks[i].Status,
                        WorkType = tasks[i].WorkType,
                        CompletedDateTime = tasks[i].CompletedDateTime,
                        IsDeleted = tasks[i].IsDeleted,
                        FioUser = UserInFIO.SoloUser(
                            actualContext.Users.FirstOrDefault(
                                x => x.ID == tasks[i].ExecutorID)),
                        FioManager = UserInFIO.SoloUser(
                            actualContext.Users.FirstOrDefault(
                                x => x.ID == actualContext.Executor.FirstOrDefault(
                                    z => z.ID == tasks[i].ExecutorID).ManagerID))
                    });
                }
                return taskFills;
            }
            catch
            {
                MessageBox.Show("Ошибка подключчения",
                    "Проверте подключение к интернету",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
                return null;
            }
        }
    }
}
