//Used for calculation of the rating of a business based on reviews
        public void calculateRating(Review review)
        {
            int totalNumberOfReviews = 0;
            int totalRating = 0;
            List<Review> reviews = db.Reviews.ToList();

            //Recalculate the combined review ratings
            foreach (var item in reviews)
            {
                //If item is the same as the business currently being reviewed
                if (item.BusinessID == review.BusinessID)
                {
                    totalNumberOfReviews++;
                    totalRating += item.Rating;
                }
            }

            Business b1 = new Business();
            b1 = db.Businesses.Find(review.BusinessID);

            //Check if there are no reviews to ensure there is no divide by 0 exception
            if (totalNumberOfReviews == 0)
            {
                b1.CombinedReviewRating = 0;//Set to 0 if no reviews
            }
            else
            {
                b1.CombinedReviewRating = totalRating / totalNumberOfReviews;//Calculate average rating
            }

            db.Entry(b1).State = EntityState.Modified;
            db.SaveChanges();
        }
        //Called when editing blobs
        public void UpdateBLOBs(Business business, HttpPostedFileBase[] files)
        {
            if (files[0] != null && files[0].ContentLength > 0)
            {
                try
                {
                   CloudBlockBlob blockBlob = container.GetBlockBlobReference(business.Name + business.Street + business.County + "Photo1.jpg");
                   blockBlob.UploadFromStream(files[0].InputStream);
                   business.URLPhoto1 = "http://lookitup.blob.core.windows.net/photos/" + business.Name + business.Street + business.County + "Photo1.jpg";
                }
                catch(Exception ex)
                {
                    
                }
            }
            if (files[1] != null && files[1].ContentLength > 0)
            {
                try
                {
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(business.Name + business.Street + business.County + "Photo2.jpg");
                    blockBlob.UploadFromStream(files[1].InputStream);
                    business.URLPhoto2 = "http://lookitup.blob.core.windows.net/photos/" + business.Name + business.Street + business.County + "Photo2.jpg";
                }
                catch (Exception ex)
                {

                }
            }
            if (files[2] != null && files[2].ContentLength > 0)
            {
                try
                {
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(business.Name + business.Street + business.County + "Photo3.jpg");
                    blockBlob.UploadFromStream(files[2].InputStream);
                    business.URLPhoto3 = "http://lookitup.blob.core.windows.net/photos/" + business.Name + business.Street + business.County + "Photo3.jpg";
                }
                catch (Exception ex)
                {

                }
            }
            if (files[3] != null && files[3].ContentLength > 0)
            {
                try
                {
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(business.Name + business.Street + business.County + "Photo4.jpg");
                    blockBlob.UploadFromStream(files[3].InputStream);
                    business.URLPhoto4 = "http://lookitup.blob.core.windows.net/photos/" + business.Name + business.Street + business.County + "Photo4.jpg";
                }
                catch (Exception ex)
                {

                }
            }
            if (files[4] != null && files[4].ContentLength > 0)
            {
                try
                {
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(business.Name + business.Street + business.County + "Photo5.jpg");
                    blockBlob.UploadFromStream(files[4].InputStream);
                    business.URLPhoto5 = "http://lookitup.blob.core.windows.net/photos/" + business.Name + business.Street + business.County + "Photo5.jpg";
                }
                catch (Exception ex)
                {

                }
            }

            /*for (int i = 0; i < files.Length; i++)
            {
                if (files[i] != null && files[i].ContentLength > 0)
                {
                    try
                    {
                        CloudBlockBlob blockBlob = container.GetBlockBlobReference(business.Name + business.Street + business.County + "Photo" + (i + 1) + ".jpg");
                        blockBlob.UploadFromStream(files[i].InputStream);
                        //Go through each photo to see what url should be updated
                        if (i == 0)
                        {
                            business.URLPhoto1 = "http://lookitup.blob.core.windows.net/photos/" + business.Name + business.Street + business.County + "Photo" + (i + 1) + ".jpg";
                        }
                        else if (i == 1)
                        {
                            business.URLPhoto2 = "http://lookitup.blob.core.windows.net/photos/" + business.Name + business.Street + business.County + "Photo" + (i + 1) + ".jpg";
                        }
                        else if (i == 2)
                        {
                            business.URLPhoto3 = "http://lookitup.blob.core.windows.net/photos/" + business.Name + business.Street + business.County + "Photo" + (i + 1) + ".jpg";
                        }
                        else if (i == 3)
                        {
                            business.URLPhoto4 = "http://lookitup.blob.core.windows.net/photos/" + business.Name + business.Street + business.County + "Photo" + (i + 1) + ".jpg";
                        }
                        else if (i == 4)
                        {
                            business.URLPhoto5 = "http://lookitup.blob.core.windows.net/photos/" + business.Name + business.Street + business.County + "Photo" + (i + 1) + ".jpg";
                        }

                    }
                    catch (Exception ex)
                    {

                    }
                }
                else if(business)
                {

                }
            }*/
        }
        //Called when all blobs for a business need to be deleted
        public void DeleteBLOBs(Business business)
        {
            CloudBlockBlob blockBlob;
            if (business.URLPhoto1 != null)
            {
                blockBlob = container.GetBlockBlobReference(business.Name + business.Street + business.County + "Photo1.jpg");
                // Delete the blob.
                blockBlob.Delete();
            }
            if (business.URLPhoto2 != null)
            {
                blockBlob = container.GetBlockBlobReference(business.Name + business.Street + business.County + "Photo2.jpg");
                // Delete the blob.
                blockBlob.Delete();
            }
            if (business.URLPhoto3 != null)
            {
                blockBlob = container.GetBlockBlobReference(business.Name + business.Street + business.County + "Photo3.jpg");
                // Delete the blob.
                blockBlob.Delete();
            }
            if (business.URLPhoto4 != null)
            {
                blockBlob = container.GetBlockBlobReference(business.Name + business.Street + business.County + "Photo4.jpg");
                // Delete the blob.
                blockBlob.Delete();
            }
            if (business.URLPhoto5 != null)
            {
                blockBlob = container.GetBlockBlobReference(business.Name + business.Street + business.County + "Photo5.jpg");
                // Delete the blob.
                blockBlob.Delete();
            }

        }
        //Used for calculation of the rating of a business based on reviews
        public int calculateRating(Business business)
        {
            int totalNumberOfReviews = 0;
            int totalRating = 0;
            int averageRating = 0;
            List<Review> reviews = db.Reviews.ToList();

            //Recalculate the combined review ratings
            foreach (var item in reviews)
            {
                //If item is the same as the business currently being reviewed
                if (item.BusinessID == business.BusinessID)
                {
                    totalNumberOfReviews++;
                    totalRating += item.Rating;
                }
            }

            //Check if there are no reviews to ensure there is no divide by 0 exception
            if (totalNumberOfReviews == 0)
            {
                averageRating = 0;//Set to 0 if no reviews
            }
            else
            {
                averageRating = totalRating / totalNumberOfReviews;//Calculate average rating
            }

            return averageRating;
        }
        public ActionResult Edit(Business business, HttpPostedFileBase[] files)
        {
            if (ModelState.IsValid)
            {
                //Update datetime edited
                business.DateTime = System.DateTime.Now;
                //Update the Rating
                business.CombinedReviewRating = calculateRating(business);
                //Update the BLOBs
                UpdateBLOBs(business, files);

                db.Entry(business).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(business);
        }
        public async Task<ActionResult> Create(Business business)
        {
            RecaptchaVerificationHelper recaptchaHelper = this.GetRecaptchaVerificationHelper();

            //Check if Captcha is empty
            if (String.IsNullOrEmpty(recaptchaHelper.Response))
            {
                ModelState.AddModelError("", "Captcha answer cannot be empty.");
                return View(business);
            }

            RecaptchaVerificationResult recaptchaResult = await recaptchaHelper.VerifyRecaptchaResponseTaskAsync();

            //Check if captcha is not a success
            if (recaptchaResult != RecaptchaVerificationResult.Success)
            {
                //Return user to business page with an error
                ModelState.AddModelError("", "Incorrect captcha answer.");
                return View(business);
            }
            else
            {
                if (ModelState.IsValid)
                {
                    //Add the current datetime value
                    business.DateTime = System.DateTime.Now;

                    //Add business if model and captcha are valid 
                    db.Businesses.Add(business);

                    db.SaveChanges();
                    
                    return RedirectToAction("Index");
                }
                else
                {
                    return View(business);
                }
            }
        }