// GET: Shares
        public async Task<ActionResult> Index()
        {
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                string userId = User.Identity.GetUserId();
                AspNetUser user = await db.AspNetUsers.FindAsync(userId);

                List<Share> shares = user.Shares.ToList();

                List<Directory> shareDirectories =
                    await
                        db.Directories.Where(
                            x =>
                                shares.Where(shr => shr.Directory_nodeId != 0)
                                    .Select(shr => shr.Directory_nodeId)
                                    .ToList()
                                    .Contains(x.dirId)).ToListAsync();

                List<Document> shareDocuments =
                    await
                        db.Documents.Where(
                            doc =>
                                shares.Where(shr => shr.Document_docId != 0)
                                    .Select(x => x.Document_docId)
                                    .ToList()
                                    .Contains(doc.docId)).ToListAsync();

                ViewBag.Directories = shareDirectories;

                ViewBag.Documents = shareDocuments;

                return PartialView();
            }
        }
        public async Task<ActionResult> ShareDirectory(int dirId, string userName)
        {
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                AspNetUser otherUser = await db.AspNetUsers.Where(usr => usr.UserName == userName).FirstOrDefaultAsync();

                if (otherUser == null) return Json(new { status = "error", message = "<strong>Error!</strong> The specified Username couldn't be found." });

                Share newShare = new Share()
                {
                    shrCreatedDate = DateTime.Now,
                    Directory_nodeId = dirId
                };
                newShare.AspNetUsers.Add(otherUser);

                db.Shares.Add(newShare);

                try
                {
                    await db.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    return Json(new { status = "error", message = "<strong>Error!</strong> Failed to add the new Share: " + ex.Message });
                }

                return Json(new { status = "success", message = "<strong>Success!</strong> This Directory was successfully shared with " + otherUser.UserName + "." });
            }
        }
        public ActionResult Upload(int id)
        {
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                Directory dir = db.Directories.Find(id);

                return dir == null ? PartialView("Error") : PartialView(dir);
            }
        }
        public async Task<ActionResult> FileDetails(int id)
        {
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                Document doc = await db.Documents.FirstOrDefaultAsync(x => x.docId == id);

                ViewBag.Shares = await db.Shares.Where(shr => shr.Document_docId == doc.docId).ToListAsync();

                return doc == null ? PartialView("Error") : PartialView(doc);
            }
        }
        /// <summary>
        /// Asynchronously retrieve the Library with the specified Library ID.
        /// </summary>
        /// <param name="identity">The inferring IIdentity of the User.</param>
        /// <returns>The Library matching the specified Library ID.</returns>
        public static async Task<Library> GetLibraryAsync(this IIdentity identity, int libraryId)
        {
            Library userLibrary = null;
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                string userId = identity.GetUserId();
                userLibrary = await db.Libraries.Where(x => x.libId == libraryId && x.AspNetUser_Id == userId).FirstOrDefaultAsync();
            }

            if (userLibrary.AspNetUser_Id != identity.GetUserId())
            {
                return null;
            }

            return userLibrary;
        }
        public async Task<ActionResult> ShareDirectory(int id)
        {
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                Directory dir = await db.Directories.FindAsync(id);

                List<Share> dirShares =
                    await
                        db.Shares.Include(shr => shr.AspNetUsers)
                            .Where(shr => shr.Directory_nodeId == dir.dirId)
                            .ToListAsync();

                ViewBag.DirShares = dirShares;

                return PartialView(dir);
            }
        }
        /// <summary>
        /// Asynchronously retrieve the newest Library for the User.
        /// </summary>
        /// <param name="identity">The inferring IIdentity of the User.</param>
        /// <returns>The User's newest Library.</returns>
        public static async Task<Library> GetLibraryAsync(this IIdentity identity)
        {
            Library userLibrary = null;
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                string userId = identity.GetUserId();
                IReadOnlyList<Library> userLibraries = await db.Libraries.Where(x => x.AspNetUser_Id == userId).OrderByDescending(x => x.libCreatedDate).Include(x => x.Directories.Select(dir => dir.Documents)).ToListAsync();
                userLibrary = userLibraries.FirstOrDefault();

                if (userLibrary == null)
                {
                    // Create new Library for User
                    userLibrary = await CreateLibrary(userId);
                }
            }

            return userLibrary;
        }
        public ActionResult Upload(int dirId, string fileName, HttpPostedFileBase file)
        {
            if (string.IsNullOrEmpty(fileName)) return Json(new { status = "error", message = "A file name is required to upload." });
            if (file == null || file.ContentLength == 0) return Json(new { status = "error", message = "A file is required." });
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                Directory thisDirectory = db.Directories.Find(dirId);

                string extension = Path.GetExtension(file.FileName);

                Document newDoc = new Document()
                {
                    dirId = dirId,
                    docCreatedDate = DateTime.Now,
                    docName = fileName,
                    docExtension = extension,
                    docContentType = file.ContentType,
                    User_userId = User.Identity.GetUserId()
                };

                db.Documents.Add(newDoc);

                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    return Json(new { status = "error", message = "Error uploading new Document: " + ex.Message });
                }

                string basePath = Path.Combine(Server.MapPath("~"), thisDirectory.Path);
                string fullFileName = newDoc.docId + newDoc.docExtension;
                if (!System.IO.Directory.Exists(basePath))
                {
                    System.IO.Directory.CreateDirectory(basePath);
                }
                file.SaveAs(Path.Combine(basePath, fullFileName));
            }

            return Json(new { status = "success", message = "The new Document was uploaded successfully." });
        }
        /// <summary>
        /// Load a Partial View with the contents of the specified Directory.
        /// </summary>
        /// <param name="dirId">ID of the Directory.</param>
        /// <param name="libraryId">ID of the Library of which this Directory is a member.</param>
        /// <returns>Partial View.</returns>
        /// <remarks>NOTE: This action is a Child action, meaning it is loaded as part of another (parent) action. Therefore (unfortunately), it must be synchronous.</remarks>
        public ActionResult LoadDirectory(int? dirId, int? libraryId)
        {
            if (!dirId.HasValue || dirId == 0)
            {
                ViewBag.errorMessage = "A Directory ID is required.";
                return PartialView("Error");
            }

            if (!libraryId.HasValue || libraryId == 0)
            {
                ViewBag.errorMessage = "A Library ID is required.";
                return PartialView("Error");
            }

            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                Directory thisDirectory = db.Directories.Include(dir => dir.Documents).FirstOrDefault(dir => dir.dirId == dirId && dir.Library_libId == libraryId);
                if (thisDirectory == null) return View("Error", new HandleErrorInfo(new Exception("Directory not found."), "Library", "LoadDirectory"));
                if (thisDirectory.Library_libId != libraryId) return View("Error", new HandleErrorInfo(new Exception("Specified Directory is not a part of this your Library."), "Library", "LoadDirectory"));

                return PartialView(thisDirectory);
            }
        }
        public ActionResult Delete(int id)
        {
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                Document doc = db.Documents.Find(id);

                string path = doc.Path;

                db.Entry(doc).State = EntityState.Deleted;

                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    return Json(new { status = "error", message = "Failed to delete file: " + ex.Message }, JsonRequestBehavior.AllowGet);
                }

                System.IO.File.Delete(path);
            }

            return Json(new { status = "success", message = "File successfully deleted!" }, JsonRequestBehavior.AllowGet);
        }
        public async Task<ActionResult> SearchUsers(string q)
        {
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                string[] results =
                    await
                        db.AspNetUsers.Where(usr => usr.UserName.Contains(q)).Select(usr => usr.UserName).ToArrayAsync();

                return Json(results, JsonRequestBehavior.AllowGet);
            }
        }
        public async Task<ActionResult> UnShareDocument(int docId, string userName)
        {
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                AspNetUser otherUser = await db.AspNetUsers.Where(usr => usr.UserName == userName).FirstOrDefaultAsync();

                if (otherUser == null) return Json(new { status = "error", message = "<strong>Error!</strong> The specified Username couldn't be found." });

                List<Share> docShares = await db.Shares.Where(share => share.Document_docId == docId && share.AspNetUsers.Select(usr => usr.UserName).Contains(userName)).ToListAsync();

                foreach (Share shr in docShares)
                {
                    List<AspNetUser> updatedUsers = shr.AspNetUsers.ToList();
                    updatedUsers.RemoveAll(usr => usr.UserName == userName);
                    shr.AspNetUsers = updatedUsers;
                    db.Entry(shr).State = EntityState.Modified;
                }

                try
                {
                    await db.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    return Json(new { status = "error", message = "<strong>Error!</strong> Failed to modify the  Share: " + ex.Message });
                }

                return Json(new { status = "success", message = "<strong>Success!</strong> This document was un-successfully shared with " + otherUser.UserName + "." });
            }
        }
        public ActionResult Get(int id)
        {
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                Document doc = db.Documents.Find(id);

                if (doc == null)
                {
                    return HttpNotFound("The requested Document could not be found.");
                }

                byte[] fileBytes = System.IO.File.ReadAllBytes(doc.Path);

                return File(new MemoryStream(fileBytes), doc.docContentType, doc.FullFilename);
            }
        }
        public async Task<ActionResult> ShareDocument(int id)
        {
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                Document doc = await db.Documents.FindAsync(id);

                List<Share> docShares =
                    await
                        db.Shares.Include(shr => shr.AspNetUsers)
                            .Where(shr => shr.Document_docId == doc.docId)
                            .ToListAsync();

                ViewBag.DocShares = docShares;

                return PartialView(doc);
            }
        }
        /// <summary>
        /// Asynchronously creates a new Library entity for the specified User.
        /// </summary>
        /// <param name="userId">The User ID of the User for which to create a new Library.</param>
        /// <returns>The newly created Library entity.</returns>
        public static async Task<Library> CreateLibrary(string userId)
        {
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                Library newLibrary = new Library()
                {
                    AspNetUser_Id = userId,
                    libCreatedDate = DateTime.Now
                };

                db.Libraries.Add(newLibrary);

                try
                {
                    await db.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return newLibrary;
            }
        }
        public ActionResult DeleteDirectory(int id)
        {
            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                Directory dir = db.Directories.Find(id);

                if (dir == null) return Json(new { status = "error", message = "Couldn't find the specified Directory." });

                if (dir.dirParentId == null)
                {
                    return Json(new { status = "error", message = "You can't delete your Root directory." });
                }

                string path = dir.Path;

                db.Entry(dir).State = EntityState.Deleted;

                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    return Json(new { status = "error", message = "Failed to delete folder: " + ex.Message });
                }

                path = Path.Combine(Server.MapPath("~"), path);
                System.IO.Directory.Delete(path, true);
            }

            return Json(new { status = "success", message = "Folder successfully deleted!" });
        }
        /// <summary>
        /// Creates a new Directory record for the current User's Library with the specified Parent Directory and Name.
        /// </summary>
        /// <param name="parentDirId">The id of the new Directory's Parent.</param>
        /// <param name="dirName">The name of the new Directory.</param>
        /// <param name="libraryId">The ID of a specific Library to use if desired.</param>
        /// <returns>JSON specifying status and the ID of the new Directory if successful.</returns>
        public async Task<ActionResult> CreateFolder(int? parentDirId, string dirName, int? libraryId = null)
        {
            if (parentDirId == null)
            {
                return Json(new { status = "error", message = "A Directory Parent ID is required." });
            }
            if (parentDirId == 0)
            {
                parentDirId = null;
            }

            if (string.IsNullOrEmpty(dirName))
            {
                return Json(new { status = "error", message = "A Directory name is required." });
            }

            string userId = User.Identity.GetUserId();

            Directory newDir = new Directory()
            {
                dirCreateDate = DateTime.Now,
                dirName = dirName,
                dirParentId = parentDirId
            };

            using (IntelliDocsEntities db = new IntelliDocsEntities())
            {
                Library userLibrary;
                if (libraryId == null || libraryId == 0)
                {
                    userLibrary = await User.Identity.GetLibraryAsync();
                }
                else
                {
                    userLibrary = await db.Libraries.FindAsync(libraryId);
                    if (userLibrary.AspNetUser_Id != userId)
                    {
                        return Json(new { status = "error", message = "The specified Library is not owned by the current User." });
                    }
                }

                newDir.Library_libId = userLibrary.libId;

                if (parentDirId == null)
                {
                    newDir.dirParentId = userLibrary.RootDirectory.dirId;
                }

                db.Directories.Add(newDir);

                try
                {
                    await db.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    // TODO: Exception handling
                    return Json(new { status = "error", message = "Error adding new Directory to DB: " + ex.Message });
                }

                System.IO.Directory.CreateDirectory(Path.Combine(Server.MapPath("~"), newDir.Path));
            }

            return Json(new { status = "success", message = "New folder, " + newDir.dirName + ", was created <strong>successfully</strong>!" });
        }