using esoft.Entities;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace esoft.Class
{
    /// <summary>
    /// Класс для заполнения и работы с Исполнителями
    /// </summary>
    public class ExecuterFill : User
    {
        string grade;          //Уровень Менеджера
        int managerID;        //Менеджер которому подчиняется Исполнитель
        string fioUser;      //ФИО Исполнителя
        string fioManager;  //ФИО Менеджера

        int numberTasks_Plan;         //Количество задач запланирована
        int numberTasks_Executed;    //Количество задач исполняется
        int numberTasks_Done;       //Количество задач выполнена
        int numberTasks_Сancelled; //Количество задач отменена

        public string Grade
        {
            get { return grade; }
            set { grade = value; }
        }
        public int ManagerID
        {
            get { return managerID; }
            set { managerID = value; }
        }
        public string FioUser
        {
            get { return fioUser; }
            set { fioUser = value; }
        }
        public string FioManager
        {
            get { return fioManager; }
            set { fioManager = value; }
        }
        public int NumberTask_Plan
        {
            get { return numberTasks_Plan; }
            set { numberTasks_Plan = value; }
        }
        public int NumberTask_Executed
        {
            get { return numberTasks_Executed; }
            set { numberTasks_Executed = value; }
        }
        public int NumberTask_Done
        {
            get { return numberTasks_Done; }
            set { numberTasks_Done = value; }
        }
        public int NumberTask_Сancelled
        {
            get { return numberTasks_Сancelled; }
            set { numberTasks_Сancelled = value; }
        }
        /// <summary>
        /// Метод формирующий удоный для работы список Исполнителей
        /// </summary>
        /// <returns>Список исполнителей</returns>
        public static List<ExecuterFill> Fill()
        {
            List<ExecuterFill> executerFills = new List<ExecuterFill>();
            try
            {
                ActualContext actualContext = new ActualContext();
                List<esoft.Entities.Executor> executors = esoftEntities.GetContext().Executor.ToList();
                List<User> users = esoftEntities.GetContext().User.ToList();
                foreach (var user in users)
                {
                    foreach (var executer in executors)
                    {
                        if (user.ID == executer.ID)
                        {
                            executerFills.Add(new ExecuterFill
                            {
                                ID = user.ID,
                                Password = user.Password,
                                FirstName = user.FirstName,
                                MiddleName = user.MiddleName,
                                LastName = user.LastName,
                                Login = user.Login,
                                IsDeleted = user.IsDeleted,
                                FioUser = UserInFIO.SoloUser(user),
                                Grade = executer.Grade,
                                managerID = executer.ManagerID,
                                FioManager = UserInFIO.SoloUser(
                                actualContext.Users.FirstOrDefault(
                                    x => x.ID == actualContext.Executor.FirstOrDefault(
                                        z => z.ID == executer.ID).ManagerID)),
                                NumberTask_Plan = GetPlanTaskCountForExecutor(executer.ID),
                                NumberTask_Executed = GetExecutedTaskCountForExecutor(executer.ID),
                                NumberTask_Done = GetDoneTaskCountForExecutor(executer.ID),
                                NumberTask_Сancelled = GetCancelledTaskCountForExecutor(executer.ID),
                            });
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("Ошибка заполнения списка Исполнителей",
                    "Ошибка",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
            return executerFills;
        }

        private static int GetPlanTaskCountForExecutor(int executorID)
        {
            ActualContext actualContext = new ActualContext();
            {
                return actualContext.Tasks.Count(task => task.ExecutorID == executorID && task.Status == "запланирована");
            }
        }

        private static int GetExecutedTaskCountForExecutor(int executorID)
        {
            ActualContext actualContext = new ActualContext();
            {
                return actualContext.Tasks.Count(task => task.ExecutorID == executorID && task.Status == "исполняется");
            }
        }

        private static int GetDoneTaskCountForExecutor(int executorID)
        {
            ActualContext actualContext = new ActualContext();
            {
                return actualContext.Tasks.Count(task => task.ExecutorID == executorID && task.Status == "выполнена");
            }
        }

        private static int GetCancelledTaskCountForExecutor(int executorID)
        {
            ActualContext actualContext = new ActualContext();
            {
                return actualContext.Tasks.Count(task => task.ExecutorID == executorID && task.Status == "отменена");
            }
        }
    }
}
