// 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.SheetsConverter;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
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.api.client.googleapis.auth.oauth2.GoogleCredential;

@WebServlet("/BillingConfig")
public class BillingConfig extends HttpServlet {
  private static final String CONTENT_TYPE_TEXT_HTML = "text/html;";
  private static final String REDIRECT_READFILE = "/index.html";

  public static final String VENDOR_ID = "vendor-id";
  public static final String ACCOUNT_ID = "account-id";
  public static final String LEGACY_CUSTOMER_ID = "legacy-customer-id";
  public static final String NEXT_GEN_CUSTOMER_ID = "next-gen-customer-id";
  public static final String LEGACY_ACCOUNT_ID = "legacy-account-id";
  public static final String NEXT_GEN_ACCOUNT_ID = "next-gen-account-id";
  public static final String CURRENCY_CODE = "currency-code";
  public static final String DIRECTION = "direction";
  public static final String ENTITY = "entity";
  public static final String MATCHING_MODE = "matching-mode";
  public static final String AGGREGATION_MODE = "aggregation-mode";

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
    // GET method --> returns entire billing config as JSON
    // return null if vendor ID doesn't exist
    String vendorID = request.getParameter(VENDOR_ID);
    String accountID = request.getParameter(ACCOUNT_ID);
    JsonConverter json = new JsonConverter();
    String configText = "";

    if (json.getConfig(vendorID) != null) {
      configText = json.getConfig(vendorID);
    } else {
      configText = "Error finding " + vendorID + "'s configuration";
    }

    /** 
     * TODO: @cade, eventually you will want to pass this access token into the 
     * updateSheets() method in the SheetsConverter class.
     */
    // Only retrieves the session if one exists.
    HttpSession session = request.getSession(false);
    String accessToken = session.getAttribute("accessToken").toString();

    SheetsConverter sheet = new SheetsConverter(); 
    try {
      sheet.writeToSheet(accessToken);
    } catch (GeneralSecurityException e) {
      // TODO: @cade Figure out how you want to handle this error.
    }

    response.setContentType(CONTENT_TYPE_TEXT_HTML);
    response.getWriter().println(configText);
    response.sendRedirect(REDIRECT_READFILE);
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // POST method --> overwrites the existing configuration or creates a new one
    // currently assuming there is only one account --> add this to the form @Vincent?
    // TODO: can the vendor ID be appended to the request as a query string @vincent?
    String tempVendorID = "ALPHA";
    Vendor newVendor = new Vendor(request, tempVendorID, 1);
    JsonConverter jsonConverter = new JsonConverter();
    response.setContentType(CONTENT_TYPE_TEXT_HTML);
    response.sendRedirect("/index.html");
  }
}
@@ -647,16 +647,37 @@
 Vendor;%0A
+import data.Account;%0A
 import d
@@ -697,16 +697,16 @@
 verter;%0A
-
 %0Aimport 
@@ -2076,16 +2076,69 @@
 mode%22;%0A%0A
+  private Vendor vendor;%0A  private Account account;%0A%0A
   @Overr
@@ -2794,16 +2794,40 @@
 ken 
+and %0A     * Vendor List 
 into the
  %0A  
@@ -2822,24 +2822,16 @@
 into the
- %0A     *
  updateS
@@ -3121,16 +3121,378 @@
   try %7B%0A
+      // Need to add the vendorList in the param.%0A      ArrayList%3CVendor%3E vendors = new ArrayList%3CVendor%3E();%0A      vendor = new Vendor(%22vend_1%22, %22legVend_27%22, 17);%0A      account = new Account(%22acc_12%22, %22vend_1%22, %22shopper%22, %22USD%22, %22disbursement%22,%0A            %22legAcc_53%22, 17, %22straight%22, %22totalAgg%22);%0A      vendor.addAccount(account);%0A      vendors.add(vendor);%0A%0A
       sh
@@ -3508,16 +3508,25 @@
 ToSheet(
+vendors, 
 accessTo

use hard-coded values in BillingConfig to test that SheetsConverter works