// 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.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.sps.data.Vendor;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servlet to register a new vendor in Datastore */
@WebServlet("/new-vendor")
public class NewVendor extends HttpServlet {

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    final UserService userService = UserServiceFactory.getUserService();
    
    if (!userService.isUserLoggedIn()) {
      response.setContentType("text/html");
      response.getWriter().println("Error: You're not logged in");
      return;
    }

    final Vendor newVendor = getVendorData(request, userService);
    toDatastore(newVendor);
  
    response.sendRedirect("/");
  }

  // Retrieve Vendors data from input form
  private Vendor getVendorData(HttpServletRequest request, UserService userService) throws IOException {
    final String firstName = request.getParameter("first_name");
    final String lastName = request.getParameter("last_name");
    final String phoneNumber = request.getParameter("phone_number");
    final String email = userService.getCurrentUser().getEmail();
    final String id = userService.getCurrentUser().getUserId();

    return new Vendor(id, firstName, lastName, email, phoneNumber, null, null);
  }

  private Entity createVendorEntity(Vendor newVendor) throws IOException {
    Entity vendorEntity = new Entity("Vendor", newVendor.getId());

    vendorEntity.setProperty("firstName", newVendor.getFirstName());
    vendorEntity.setProperty("lastName", newVendor.getLastName());
    vendorEntity.setProperty("email", newVendor.getEmail());
    vendorEntity.setProperty("phoneNumber", newVendor.getPhoneNumber());
    vendorEntity.setProperty("profilePic", newVendor.getProfilePic());
    vendorEntity.setProperty("saleCard", newVendor.getSaleCard());

    return vendorEntity;
  }

  // Adds a new vendor entity in Datastore
  private void toDatastore(Vendor newVendor) throws IOException {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity vendor = createVendorEntity(newVendor);

    datastore.put(vendor); 
  }
}
@@ -793,16 +793,184 @@
 Entity;%0A
+import com.google.appengine.api.datastore.EntityNotFoundException;%0Aimport com.google.appengine.api.datastore.Key;%0Aimport com.google.appengine.api.datastore.KeyFactory;%0A
 import c
@@ -1759,16 +1759,54 @@
 ();%0A    
+response.setContentType(%22text/html%22);%0A
 %0A    if 
@@ -1858,36 +1858,150 @@
 nse.
-setContentType(%22text/html%22);
+getWriter().println(%22Error: You're not logged in%22);%0A      return;%0A    %7D%0A%0A    if (isUserRegistered(userService.getCurrentUser().getUserId())) %7B
 %0A   
@@ -2037,35 +2037,34 @@
 ln(%22
-Error: You're not logged in
+You're already registered!
 %22);%0A
@@ -2150,16 +2150,156 @@
 rvice);%0A
+%0A    if (!isValidInput(newVendor)) %7B%0A      response.getWriter().println(%22Error: You can't send null or empty inputs%22);%0A      return;%0A    %7D%0A%0A
     toDa
@@ -2322,11 +2322,8 @@
 r);%0A
-  %0A
     
@@ -3771,9 +3771,970 @@
 ); %0A  %7D%0A
-%7D
+%0A  // Checks if any of the input values is empty%0A  private boolean isValidInput(Vendor newVendor) throws IOException %7B%0A    final String firstName = newVendor.getFirstName();%0A    final String lastName =  newVendor.getLastName();%0A    final String phoneNumber = newVendor.getPhoneNumber();%0A%0A    return !(%0A      (firstName == null %7C%7C firstName.isEmpty()) %7C%7C%0A      (lastName == null %7C%7C lastName.isEmpty()) %7C%7C%0A      (phoneNumber == null %7C%7C phoneNumber.isEmpty())%0A    );%0A  %7D%0A%0A  public boolean isUserRegistered(String id) throws IOException %7B%0A    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A    Key vendorKey = KeyFactory.createKey(%22Vendor%22, id);%0A    %0A    try %7B%0A      Entity vendor = datastore.get(vendorKey);%0A      String phoneNumber = (String) vendor.getProperty(%22phoneNumber%22);%0A      %0A      return (phoneNumber == null %7C%7C phoneNumber.isEmpty()) ? false : true;%0A    %7D catch (EntityNotFoundException e) %7B%0A      return false;%0A    %7D%0A  %7D%0A%7D%0A

Implement user input validation and registration status