using Reconstruction;
using System.Collections.ObjectModel;

namespace Reconstruction.Functions
{ 
    public static class Display
    {

        public static void ShowCompletedTask()
        {
            Console.WriteLine("Tasks completed by " + Service.LoggedUser.Login);
            ObservableCollection<Task> TaskCollection = new ObservableCollection<Task>(Service.db.Tasks.Where(x => x.AccUserId == Service.LoggedUser.Id && x.StatusId = 3).ToList());

            foreach (Task t in TaskCollection)
            {
                User user = (User)Service.db.Users.Where(u => u.Id == t.CreatorUserId).SingleOrDefault();
                Status status = (Status)Service.db.Statuses.Where(s => s.Id == t.StatusId).SingleOrDefault();

                Console.WriteLine("Title: " + t.Title +
                                  "\nDescription: " + t.Description +
                                  "\nCreated by " + user.Login +
                                  "\nStatus: " + status.Title + "\n");
            }

            Console.ReadKey();

        }

        public static void ShowUsersTasks()
        {
            ObservableCollection<Task> TaskCollection = new ObservableCollection<Task>(Service.db.Tasks.Where(x => x.AccUserId == Service.LoggedUser.Id).ToList());

            foreach (Task t in TaskCollection)
            {
                User user = (User)Service.db.Users.Where(u => u.Id == t.CreatorUserId).SingleOrDefault();
                Status status = (Status)Service.db.Statuses.Where(s => s.Id == t.StatusId).SingleOrDefault();

                Console.WriteLine("Title: " + t.Title +
                                  "\nDescription: " + t.Description +
                                  "\nCreated by " + user.Login +
                                  "\nStatus: " + status.Title +
                                  "\nEnter " + t.Id + " to change its status\n");
            }

            int id = 0;
            Task task;

            while (true)
            {
                try
                {
                    id = int.Parse(Console.ReadLine());
                    task = (Task)Service.db.Tasks.Where(x => x.Id == id).SingleOrDefault();
                    break;
                }
                catch
                {
                    Console.WriteLine("You entered a wrong value");
                }
            }
            if(task != null)
            {
                Console.WriteLine("Enter:\n1 - to set task 'incompleted'\n2 - to set task 'in progress'\n3 - to set task 'completed'\n0 - to abolish this operation\n");
                int statusID;

                while(true)
                {
                    try
                    {
                        statusID = int.Parse(Console.ReadLine());
                        break;
                    }
                    catch
                    {
                        Console.WriteLine("You entered a wrong value");
                    }
                }
                if(statusID != 0 && statusID < 4)
                {
                    task.StatusId = statusID;
                    Service.db.Tasks.Update(task);
                    Service.db.SaveChanges();
                    Console.WriteLine("Operation performed successfully");
                }
                else
                {
                    Console.WriteLine("Operation abolished");
                }

            }

            Console.ReadKey();

        }


        public static void ShowUserList()
        {
            Console.WriteLine("Login  |" + "  Lastname  |" + "  Firstname  |" + "  Middlename ");
            foreach (User s in Service.UserCollection)
            {
                Console.WriteLine(s.Login + ' ' + s.Lastname + ' ' + s.Firstname + ' ' + s.Middlename);
            }

            Console.ReadKey();  

        }



        public static void ShowProfileData()
        {
            Console.WriteLine("User Name: "     + Service.LoggedUser.Login 
                          + "\nFirst name: "    + Service.LoggedUser.Firstname 
                          + "\nSurname: "       + Service.LoggedUser.Lastname 
                          + "\nPatronimic: "    + Service.LoggedUser.Middlename 
                          + "\nPhone: "         + Service.LoggedUser.Phone 
                          + "\nTasks: ");
            Service.db.Tasks.Where(t => t.AccUserId == Service.LoggedUser.Id).ToList().ForEach(t => global::System.Console.Write(t.Title + " "));
        
            Console.ReadKey();
        }


        public static void ShowAvailableTask()
        {
            ObservableCollection<Task> TaskCollection = new ObservableCollection<Task>(Service.db.Tasks.ToList());

            foreach (Task t in TaskCollection)
            {
                User user = (User)Service.db.Users.Where(u => u.Id == t.CreatorUserId).SingleOrDefault();

                if(t.StatusId != 1 && t.AccUserId == null)
                {
                    Console.WriteLine("Title: " + t.Title +
                                      "\nDescription: " + t.Description +
                                      "\nCreated by " + user.Login + 
                                      "\nEnter " + t.Id + " to take this task\n");
                }
            }

            int id = 0;
            Task task;

            while (true)
            {
                try
                {
                    id = int.Parse(Console.ReadLine());
                    task = (Task)Service.db.Tasks.Where(x => x.Id == id).SingleOrDefault();
                    break;
                }
                catch
                {
                    Console.WriteLine("You entered a wrong value");
                }
                
            }

            task.AccUserId = Service.LoggedUser.Id;

            Service.db.Tasks.Update(task);
            Service.db.SaveChanges();

            Console.ReadKey();
        }

    }
}
