// Copyright 2020 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 servlets;

import data.JsonConverter;
import data.Vendor;
import data.Account;
import data.SheetsConverter;

import java.util.*;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.security.GeneralSecurityException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.google.gson.Gson;

@WebServlet("/VendorServlet")
public class VendorServlet extends HttpServlet {
  private final String CONTENT_TYPE = "text/html;";
  private final String DELETE_PAGE_REDIRECT = "/deletefile.html";
  private final String VENDOR_ID_PARAM = "vendorID";

  @Override
  /**
   * Prints a hashmap to the response object
   * STRING (Vendor ID) : ARRAY OF STRINGS (Account IDs)
   * e.g., {"vend_1":["account1","account2","account3","account4"],
   *            "vend_2":["account5","account6","account7"]}
   */
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    JsonConverter converter = new JsonConverter();
    String configMap = converter.getConfigMap();
    response.setContentType(CONTENT_TYPE);
    response.getWriter().println(configMap);
  }

  /** Delete the desired Vendor from the fileset and spreadsheets if posible.*/
  public void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {  
    String vendorID = request.getParameter(VENDOR_ID_PARAM);
    String deleteStatus = "success";

    // Delete the file from the filesystem and update the sheets.
    try {
      deleteVendorFile(vendorID);
      updateSheets(request);    
    } catch (FileNotFoundException e) {
      deleteStatus = "Could not execute, an FileNotFoundException was thrown.";
    } catch (GeneralSecurityException e) {
      deleteStatus = "Could not execute, a GeneralSecurityException was thrown.";
    } 

    // Convert the String message into a JSON String.
    String jsonMessage = messageAsJson(deleteStatus);
    
    response.setContentType(CONTENT_TYPE);
    response.getWriter().println(jsonMessage);
  }

  private void deleteVendorFile(String vendorID) throws FileNotFoundException {
    JsonConverter converter = new JsonConverter();
    ArrayList<String> vendorIDs = converter.getVendorIDs();

    // Validate that the vendorID exists.
    if(!vendorIDs.contains(vendorID)) {
        throw new FileNotFoundException();
    }
    converter.deleteFile(vendorID);
  }

  /** Rebuild the sheets without the deleted Vendor's data. */
  private void updateSheets(HttpServletRequest request) 
      throws IOException, GeneralSecurityException {
    JsonConverter converter = new JsonConverter();
    ArrayList<String> vendorIDs = converter.getVendorIDs();
    ArrayList<Vendor> vendors = getVendors(vendorIDs, converter); 

    // Only retrieve the session if one exists.
    HttpSession session = request.getSession(false);
    String accessToken = session.getAttribute("accessToken").toString();

    SheetsConverter sheets = new SheetsConverter();
    sheets.updateSheets(vendors, accessToken);
  }

  public ArrayList<Vendor> getVendors(ArrayList<String> vendorIDs, 
    JsonConverter converter) throws IOException {

    ArrayList<Vendor> vendors = new ArrayList<Vendor>(); 

    for(int i = 0; i < vendorIDs.size(); i++) {
      String vendorJson = converter.getConfig(vendorIDs.get(i));
      vendors.add(new Vendor(vendorJson, vendorIDs.get(i)));
    }

    return vendors;
  } 

  /** Converts a String into a JSON string using Gson. */
  private String messageAsJson(String deleteMessage) {
    Gson gson = new Gson();
    String jsonMessage = gson.toJson(deleteMessage);
    return jsonMessage;
  }
}
@@ -1943,19 +1943,8 @@
 eets
- if posible
 .*/%0A
@@ -2119,28 +2119,30 @@
  String 
-deleteStatus
+responseString
  = %22succ
@@ -2341,203 +2341,139 @@
     
-deleteStatus = %22Could not execute, an FileNotFoundException was thrown.%22;%0A    %7D catch (GeneralSecurityException e) %7B%0A      deleteStatus = %22Could not execute, a GeneralSecurityException was thrown
+responseString = %22Failed to Delete File.%22;%0A    %7D catch (GeneralSecurityException e) %7B%0A      responseString = %22Failed to Delete File
 .%22;%0A
@@ -2577,20 +2577,22 @@
 son(
-deleteStatus
+responseString
 );%0A 
@@ -4191,108 +4191,47 @@
     
-Gson gson = new Gson();%0A    String jsonMessage = gson.toJson(deleteMessage);%0A    return jsonMessage;
+return new Gson().toJson(deleteMessage)
 %0A  %7D

change variable name and comment in VendorServelt