package conferenceOrganisation.services;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.SQLException;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import conferenceOrganisation.managers.EventManager;
import conferenceOrganisation.managers.TicketManager;
import conferenceOrganisation.managers.UserManager;
import conferenceOrganisation.models.Event;
import conferenceOrganisation.models.User;
import conferenceOrganisation.utils.Utils;

@Stateless
@Path("user")
public class UserService {

	private static final Response RESPONSE_OK = Response.ok().build();

	@Inject
	private UserManager userManager;

	@Inject
	private CurrentUser currentUser;

	@Inject
	private TicketManager ticketManager;

	@Inject
	private EventManager eventManager;

	@Path("login")
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public Response loginUser(User user) throws SQLException, IOException {
		User foundUser = userManager.getUserByEmailAndPassword(user.getEmail(), user.getPassword());
		if (foundUser == null) {
			return Response.status(HttpURLConnection.HTTP_UNAUTHORIZED).build();
		}
		return RESPONSE_OK;
	}

	@Path("register")
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public Response registerUser(final User user) throws IOException {
		try {
			userManager.addUser(user);
			Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					Utils.sendWelcomeEmail(user);
				}
			});
			thread.start();
			return RESPONSE_OK;
		} catch (SQLException e) {
			System.out.println("Problem occurs while trying to add new user (User with same email already exist");
		}

		return Response.status(401).build();
	}

	@Path("current")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public User getUser() {
		if (currentUser.getCurrentUser() == null) {
			return null;
		}
		return currentUser.getCurrentUser();
	}

	@Path("logout")
	@GET
	public void logoutUser() {
		currentUser.setCurrentUser(null);
	}

	@Path("bookTicket")
	@POST
	public Response bookTicket(@QueryParam("eventId") int eventId) throws SQLException, IOException {
		if (!ticketManager.addTicketToUser(eventId)) {
			return Response.status(401).build();
		}
		return RESPONSE_OK;
	}

	@Path("createEvent")
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public Response createEvent(Event event) {
		try {
			eventManager.addEvent(event);
				return RESPONSE_OK;
			
		} catch (SQLException | IOException e) {
			e.printStackTrace();
			return Response.status(401).build();
		}
	}

}

@@ -2546,24 +2546,170 @@
 vent) %7B%0A
+%09%09if (event.getTitle() == null %7C%7C event.getTitle().trim().equals(%22%22) %7C%7C event.getHall().getCapacity() %3C= 0) %7B%0A%09%09%09return Utils.RESPONSE_ERROR;%0A%09%09%7D%0A
 %09%09try %7B%0A
 %09%09%09event
@@ -2700,16 +2700,16 @@
 %09%09try %7B%0A
-
 %09%09%09event
@@ -2736,17 +2736,16 @@
 nt);%0A%09%09%09
-%09
 return R
@@ -2756,19 +2756,16 @@
 NSE_OK;%0A
-%09%09%09
 %0A%09%09%7D cat
@@ -2838,36 +2838,28 @@
 urn 
-Response.status(401).build()
+Utils.RESPONSE_ERROR
 ;%0A%09%09

add event validation