// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.sps.servlets;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.EmbeddedEntity;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.sps.data.HttpServletUtils;
import com.google.sps.data.Vendor;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * When the user submits the form, Blobstore processes the file upload and then forwards the request
 * to this servlet to update a vendor information
 */
@WebServlet("/update-vendor")
public class UpdateVendorServlet extends HttpServlet {

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UserService userService = UserServiceFactory.getUserService();
    
    if (!userService.isUserLoggedIn()) {
      System.out.println("User is not logged in.");
      response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "User is not logged in.");
      return;
    }

    String firstName = HttpServletUtils.getParameter(request, "firstName", "");
    String lastName = HttpServletUtils.getParameter(request, "lastName", "");
    String phoneNumber = HttpServletUtils.getParameter(request, "phoneNumber", "");
      
    // Get values to add/update vendor's profile picture
    String currentBlobKey = HttpServletUtils.getParameter(request, "blobKey", "");
    BlobKey imageBlobKey = HttpServletUtils.getUploadedFileBlobKey(request, "imageFile");
    String altText = HttpServletUtils.getParameter(request, "altText", "");
    // If nothing was uploaded and there is a current picture, keep current
    if (!currentBlobKey.isEmpty() && imageBlobKey == null) {
      imageBlobKey = new BlobKey(currentBlobKey);
    }

    // Check values are not empty and valid
    if (!HttpServletUtils.hasOnlyLetters(firstName) || !HttpServletUtils.hasOnlyLetters(lastName) || 
        !HttpServletUtils.hasOnlyNumbers(phoneNumber) || altText.isEmpty() || imageBlobKey == null) {
      // Delete from blobstore if the uploaded file was a new one
      if (imageBlobKey != null && !currentBlobKey.equals(imageBlobKey.toString())) {
        BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
        blobstoreService.delete(imageBlobKey);
      }
      System.out.println("The values do not exist and/or the format is incorrect.");
      response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Inputs do not exist and/or the format is incorrect.");
      return;
    }

    String vendorId = userService.getCurrentUser().getUserId();
    Key vendorKey = KeyFactory.createKey("Vendor", vendorId);
    Entity vendorEntity = HttpServletUtils.getVendorEntity(vendorId);
    if (vendorEntity == null) {
      System.out.println("Vendor Account does not exist");
      response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Vendor Account does not exist");
      return;
    }
    Vendor vendorObject = new Vendor(vendorEntity);

    // If the vendor doesn't have a picture, create completely new entity
    // Else, use the already existing entity
    Entity picture = (vendorObject.getProfilePic() == null)
        ? new Entity("Picture", vendorKey)
        : new Entity("Picture", vendorObject.getProfilePic().getId(), vendorKey);

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    // If not the same, delete previous blob from blobstore
    if (vendorObject.getProfilePic() != null && 
        imageBlobKey.compareTo(vendorObject.getProfilePic().getBlobKey()) != 0) {
      BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
      blobstoreService.delete(vendorObject.getProfilePic().getBlobKey());
    }
    picture.setProperty("blobKey", imageBlobKey);
    picture.setProperty("altText", altText);
    datastore.put(picture);

    EmbeddedEntity picInfo = new EmbeddedEntity();
    picInfo.setKey(picture.getKey());
    picInfo.setPropertiesFrom(picture);

    vendorEntity.setProperty("firstName", firstName);
    vendorEntity.setProperty("lastName", lastName);
    vendorEntity.setProperty("phoneNumber", phoneNumber);
    vendorEntity.setProperty("profilePic", picInfo);
    datastore.put(vendorEntity);
      
    response.sendRedirect("/");
  }
}

@@ -3108,53 +3108,8 @@
 ber)
- %7C%7C altText.isEmpty() %7C%7C imageBlobKey == null
 ) %7B%0A
@@ -4577,16 +4577,39 @@
 null && 
+imageBlobKey != null &&
 %0A       
@@ -4848,24 +4848,186 @@
 y());%0A    %7D%0A
+%0A    // If there is a picture with alt text, set properties; Else if only picture, delete from blobstore %0A    if (imageBlobKey != null && !altText.isEmpty()) %7B%0A  
     picture.
@@ -5064,24 +5064,26 @@
 obKey);%0A    
+  
 picture.setP
@@ -5111,16 +5111,18 @@
 tText);%0A
+  
     data
@@ -5146,16 +5146,18 @@
 );%0A%0A    
+  
 Embedded
@@ -5195,24 +5195,26 @@
 tity();%0A    
+ 
+ 
 picInfo.setK
@@ -5231,24 +5231,26 @@
 .getKey());%0A
+  
     picInfo.
@@ -5274,24 +5274,259 @@
 (picture);%0A%0A
+      vendorEntity.setProperty(%22profilePic%22, picInfo);%0A    %7D else if (imageBlobKey != null) %7B%0A      BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();%0A      blobstoreService.delete(imageBlobKey);%0A    %7D%0A%0A
     vendorEn
@@ -5681,61 +5681,8 @@
 r);%0A
-    vendorEntity.setProperty(%22profilePic%22, picInfo);%0A
     

Make profile pic optional