package com.google.sps.servlets;

import java.io.IOException;
import com.google.sps.data.Lesson;
import com.google.sps.data.Room;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.EntityNotFoundException;
import java.util.List;
import java.util.ArrayList;
import com.google.gson.Gson;
import com.google.sps.data.Lesson;
import com.google.sps.data.Room;
import com.google.sps.data.RequestParser;
import com.google.sps.service.DatabaseService;
import com.google.sps.data.RequestParser;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/lesson")
public class LessonHandlerServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        PreparedQuery roomResults = datastore.prepare(new Query(Room.ROOM_ENTITY_NAME));
        Room room = null;

        for(Entity entity : roomResults.asIterable()) {
            if (new Room(entity).getRoomKey().getId() == Long.parseLong(request.getParameter("room_id"))) {
                room = new Room(entity);
            }
        }
        
        response.setContentType("application/json");
        response.getWriter().println(new Gson().toJson(getLessons(room)));
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Lesson lesson = Lesson.deserializeJson(RequestParser.parseStringFromRequest(request));
        DatabaseService.save(lesson.getLessonEntity());
    
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        PreparedQuery roomResults = datastore.prepare(new Query(Room.ROOM_ENTITY_NAME));
        Room room = null;

        for(Entity entity : roomResults.asIterable()) {
            if (new Room(entity).getRoomKey().getId() == Long.parseLong(request.getParameter("room_id"))) {
                room = new Room(entity);
                room.addLesson(lesson);
            }
        }
        
        DatabaseService.save(room.getRoomEntity()); 
        response.setStatus(HttpServletResponse.SC_OK);
    }

    public List<Lesson> getLessons(Room room) {
        List<Lesson> lessons = new ArrayList<>();
    
        try {
            for(Key key : room.getAllLessons()) {
                lessons.add(DatabaseService.getLesson(key));
            }
        }
        catch (EntityNotFoundException e) {
            System.err.println("Lesson entities don't exist.");
        }
        return lessons;
    }
}
@@ -254,114 +254,8 @@
 ry;%0A
-import com.google.appengine.api.datastore.PreparedQuery;%0Aimport com.google.appengine.api.datastore.Query;%0A
 impo
@@ -600,24 +600,69 @@
 uestParser;%0A
+import com.google.sps.service.FilterService;%0A
 import com.g
@@ -700,50 +700,8 @@
 ce;%0A
-import com.google.sps.data.RequestParser;%0A
 impo
@@ -1080,311 +1080,20 @@
     
-DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A        PreparedQuery roomResults = datastore.prepare(new Query(Room.ROOM_ENTITY_NAME));%0A        Room room = null;%0A%0A        for(Entity entity : roomResults.asIterable()) %7B%0A            if (new Room(entity).getRoomKey().getId() =
+Long roomId 
 = Lo
@@ -1133,38 +1133,34 @@
 (%22room_id%22))
-) %7B%0A  
+;%0A
         
       room =
@@ -1139,37 +1139,36 @@
 _id%22));%0A        
-     
+Room
  room = new Room
@@ -1172,49 +1172,64 @@
 oom(
-entity);%0A            %7D%0A        %7D%0A        
+FilterService.getEntity(Room.ROOM_ENTITY_NAME, roomId));
 %0A   
@@ -1627,20 +1627,16 @@
 ity());%0A
-    
 %0A       
@@ -1640,311 +1640,20 @@
     
-DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A        PreparedQuery roomResults = datastore.prepare(new Query(Room.ROOM_ENTITY_NAME));%0A        Room room = null;%0A%0A        for(Entity entity : roomResults.asIterable()) %7B%0A            if (new Room(entity).getRoomKey().getId() =
+Long roomId 
 = Lo
@@ -1697,19 +1697,17 @@
 om_id%22))
-) %7B
+;
 %0A       
@@ -1703,31 +1703,28 @@
 ));%0A        
-       
+Room
  room = new 
@@ -1732,89 +1732,97 @@
 oom(
-entity);%0A                room.addLesson(lesson);%0A            %7D%0A        %7D%0A        
+FilterService.getEntity(Room.ROOM_ENTITY_NAME, roomId));%0A        room.addLesson(lesson);%0A
 %0A   

Update lesson handler