using Microsoft.EntityFrameworkCore;
using Project.Data.Project;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;

namespace Project.Data.Project
{
    public class ProjectService
    {

        private readonly ProjectContext _context;
        public ProjectService(ProjectContext context)
        {
            _context = context;
        }

        public async Task<Project> GetSelectProjectAsync(int projectId)
        {
            Project colProject = new Project();
            colProject.Title = "PROJECT NOT FOUND";
            //gets all project if user is admin
            string strCurrentUser = "";
            if (projectId == null)
            {
                
                return await Task.FromResult(colProject); 
            }
            else //get projects for current user
            {
                colProject =
                (from project in _context.Project
                 where project.Id == projectId
                 select project).FirstOrDefault();
                return await Task.FromResult(colProject);
            }
        }
        public async Task <List<Project>> GetProjectAsync()
        {
            List<Project> colProject = new List<Project>();
            //gets all project if user is admin
            string strCurrentUser = "";
            if (strCurrentUser == "Admin@BlazorHelpWebsite.com")
            {
                colProject =
                (from project in _context.Project
                 
                 select project).ToList();
                return await Task.FromResult(colProject);
            }
            else //get projects for current user
            {
                //colProject = _context.Project
                   // .Include(c => c.Members)
                  // .AsNoTracking()
                   // .ToList();
                colProject =
                (from project in _context.Project
                     // only get entries for the current logged in user
                 //where project.UserName == strCurrentUser
                 select project).ToList();
            return await Task.FromResult(colProject);
            }
        }

        public Task<Project> CreateProjectAsync(Project objProject)
        {
            _context.Project.Add(objProject);
            _context.SaveChanges();
            return Task.FromResult(objProject);
        }


        public Task<bool> UpdateProjectAsync(Project objProject)
        {
            var ExistingProject =
                _context.Project
                .Where(x => x.Id == objProject.Id)
                .FirstOrDefault();
            if (ExistingProject != null)
            {
                ExistingProject.Title =
                    objProject.Title;
                ExistingProject.Details =
                    objProject.Details;
                ExistingProject.MaxStudents =
                    objProject.MaxStudents;
                ExistingProject.IsActive =
                    objProject.IsActive;
                ExistingProject.isLock =
                    objProject.isLock;
                ExistingProject.UserLockID =
                    objProject.UserLockID;
                //ExistingProject.Members =
                   // objProject.Members;

                _context.SaveChanges();
            }
            else
            {
                return Task.FromResult(false);
            }
            return Task.FromResult(true);
        }

    }
}
