// 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.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
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();
    response.setContentType("text/html");

    if (!userService.isUserLoggedIn()) {
      response.getWriter().println("Error: You're not logged in");
      return;
    }

    if (isUserRegistered(userService.getCurrentUser().getUserId())) {
      response.getWriter().println("You're already registered!");
      return;
    }

    final Vendor newVendor = getVendorData(request, userService);

    if (!isValidInput(newVendor)) {
      response.getWriter().println("Error: You can't send null or empty inputs");
      return;
    }

    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();
    datastore.put(createVendorEntity(newVendor)); 
  }

  // Checks if any of the input values is empty
  private boolean isValidInput(Vendor newVendor) throws IOException {
    final String firstName = newVendor.getFirstName();
    final String lastName =  newVendor.getLastName();
    final String phoneNumber = newVendor.getPhoneNumber();

    return !(
      (firstName == null || firstName.isEmpty()) ||
      (lastName == null || lastName.isEmpty()) ||
      (phoneNumber == null || phoneNumber.isEmpty())
    );
  }

  public boolean isUserRegistered(String id) throws IOException {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key vendorKey = KeyFactory.createKey("Vendor", id);
    
    try {
      Entity vendor = datastore.get(vendorKey);
      String phoneNumber = (String) vendor.getProperty("phoneNumber");
      
      return (phoneNumber == null || phoneNumber.isEmpty()) ? false : true;
    } catch (EntityNotFoundException e) {
      return false;
    }
  }
}

@@ -1267,16 +1267,80 @@
 Writer;%0A
+import java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A
 import j
@@ -1633,17 +1633,16 @@
 rvlet %7B%0A
-%0A
   @Overr
@@ -2297,42 +2297,55 @@
 or: 
-You
+Inputs
  can't 
-send null or empty input
+be empty or have special character
 s%22);
@@ -3815,24 +3815,781 @@
 or)); %0A  %7D%0A%0A
+  // Check if the vendors names contain special characters%0A  private boolean hasSpecialCharacters(String vendorName) throws IOException%7B%0A    Pattern validCharacters = Pattern.compile(%22%5Ba-zA-Z0-9%5D*%22);%0A    Matcher validInputChecker = validCharacters.matcher(vendorName);%0A%0A    if (!validInputChecker.matches()) %7B%0A      return true;%0A    %7D%0A    return false;%0A  %7D%0A%0A  // Check if the vendors phone number contain special characters or letters%0A  private boolean hasLettersOrSpecialCharacters(%0A    String phoneNumber) throws IOException %7B%0A      Pattern validCharacters = Pattern.compile(%22%5B0-9%5D*%22);%0A      Matcher validInputChecker = validCharacters.matcher(phoneNumber);%0A%0A      if (!validInputChecker.matches()) %7B%0A        return true;%0A      %7D%0A      return false;%0A  %7D%0A%0A
   // Checks 
@@ -4871,17 +4871,42 @@
     
-return !(
+final boolean notValidFirstName = 
 %0A   
@@ -4941,36 +4941,107 @@
 stName.isEmpty()
-)
  %7C%7C
+ hasSpecialCharacters(firstName));%0A    final boolean notValidLastName = 
 %0A      (lastName
@@ -5074,71 +5074,300 @@
 ty()
-)
  %7C%7C
-%0A      (phoneNumber == null %7C%7C phoneNumber.isEmpty())%0A    )
+ hasSpecialCharacters(lastName));%0A    final boolean notValidPhoneNumber = %0A      (phoneNumber == null %7C%7C phoneNumber.isEmpty() %7C%7C hasLettersOrSpecialCharacters(phoneNumber));%0A%0A    if (notValidFirstName %7C%7C notValidLastName %7C%7C notValidPhoneNumber) %7B%0A      return false;%0A    %7D%0A    return true
 ;%0A  

Add input validation on new vendor servlet