// 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.sps.classes.KeywordCampaign;
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.datastore.Query.SortDirection;
import java.io.IOException;
import java.io.PrintWriter;
import com.google.gson.Gson;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

// gets all the keyword campaigns from datastore
// posts new keyword campaigns to datastore
@WebServlet("/keyword-campaigns")
public class KeywordCampaignsServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Query query = new Query("keywordCampaign").addSort("keywordCampaignId", SortDirection.ASCENDING);
    	PreparedQuery results = datastore.prepare(query);

        ArrayList<KeywordCampaign> keywordCampaigns = new ArrayList<KeywordCampaign>();
        for (Entity entity : results.asIterable()) {
            keywordCampaigns.add(createKeywordCampaignFromEntity(entity));
        }

        Gson gson = new Gson();
        String json = gson.toJson(keywordCampaigns);
        response.setContentType("application/json;");
        response.getWriter().println(json);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String keywordCampaignId = request.getParameter("keywordCampaignId");
        String userId = request.getParameter("userId");

        String[] DSACampaignIdsArray = request.getParameter("DSACampaignIds").split(" ");
        ArrayList<String> DSACampaignIds = new ArrayList<String>();
        for (String id : DSACampaignIdsArray) {
            DSACampaignIds.add(id);
        }

        String name = request.getParameter("name");

        int impressions = Integer.parseInt(request.getParameter("impressions"));
        int clicks = Integer.parseInt(request.getParameter("clicks"));
        double cost = Double.parseDouble(request.getParameter("cost"));

        Entity keywordCampaignEntity = new Entity("keywordCampaign");
        keywordCampaignEntity.setProperty("keywordCampaignId", keywordCampaignId);
        keywordCampaignEntity.setProperty("userId", userId);
        keywordCampaignEntity.setProperty("DSACampaignIds", DSACampaignIds);
        keywordCampaignEntity.setProperty("name", name);
       
        keywordCampaignEntity.setProperty("impressions", impressions);
        keywordCampaignEntity.setProperty("clicks", clicks);
        keywordCampaignEntity.setProperty("cost", cost);
	
    	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(keywordCampaignEntity);
            
        response.sendRedirect("/Compare/compare.html");
    }

    public static KeywordCampaign createKeywordCampaignFromEntity(Entity entity) {
        String keywordCampaignId = (String) entity.getProperty("keywordCampaignId");
        String userId = (String) entity.getProperty("userId");
        ArrayList<String> DSACampaignIds = (ArrayList<String>) entity.getProperty("DSACampaignIds");
        String name = (String) entity.getProperty("name");

        int impressions = (int) ((long) entity.getProperty("impressions"));
        int clicks = (int) ((long) entity.getProperty("clicks"));
        double cost = (double) entity.getProperty("cost");

        return new KeywordCampaign(keywordCampaignId, userId, DSACampaignIds, name, impressions, clicks, cost);
    }
}
@@ -2380,38 +2380,38 @@
 ption %7B%0A        
-String
+Entity
  keywordCampaign
@@ -2414,33 +2414,27 @@
 aign
-Id = request.getParameter
+Entity = new Entity
 (%22ke
@@ -2438,34 +2438,32 @@
 %22keywordCampaign
-Id
 %22);%0A        Stri
@@ -2462,782 +2462,179 @@
     
-String userId = request.getParameter(%22userId%22);%0A%0A        String%5B%5D DSACampaignIdsArray = request.getParameter(%22DSACampaignIds%22).split(%22 %22);%0A        ArrayList%3CString%3E DSACampaignIds = new ArrayList%3CString%3E();%0A        for (String id : DSACampaignIdsArray) %7B%0A            DSACampaignIds.add(id);%0A        %7D%0A%0A        String name = request.getParameter(%22name%22);%0A%0A        int impressions = Integer.parseInt(request.getParameter(%22impressions%22));%0A        int clicks = Integer.parseInt(request.getParameter(%22clicks%22));%0A        double cost = Double.parseDouble(request.getParameter(%22cost%22));%0A%0A        Entity keywordCampaignEntity = new Entity(%22keywordCampaign%22);%0A        keywordCampaignEntity.setProperty(%22keywordCampaignId%22, keywordCampaignId);%0A        keywordCampaignEntity.setProperty
+keywordCampaignEntity.setProperty(%22keywordCampaignId%22, request.getParameter(%22keywordCampaignId%22));%0A        keywordCampaignEntity.setProperty(%22userId%22, request.getParameter
 (%22us
@@ -2638,24 +2638,17 @@
 %22userId%22
-, userId
+)
 );%0A     
@@ -2702,16 +2702,38 @@
 gnIds%22, 
+request.getParameter(%22
 DSACampa
@@ -2738,16 +2738,29 @@
 paignIds
+%22).split(%22 %22)
 );%0A     
@@ -2804,20 +2804,44 @@
 %22name%22, 
+request.getParameter(%22
 name
+%22)
 );%0A     
@@ -2900,16 +2900,55 @@
 sions%22, 
+Integer.parseInt(request.getParameter(%22
 impressi
@@ -2950,16 +2950,19 @@
 ressions
+%22))
 );%0A     
@@ -3008,22 +3008,64 @@
 licks%22, 
+Integer.parseInt(request.getParameter(%22
 clicks
+%22))
 );%0A     
@@ -3109,20 +3109,64 @@
 %22cost%22, 
+Double.parseDouble(request.getParameter(%22
 cost
+%22))
 );%0A%09%0A   

Cut down on code.