package nuclibook.server;

import nuclibook.constants.C;
import nuclibook.constants.RequestType;
import nuclibook.entity_utils.SecurityUtils;
import nuclibook.routes.*;
import org.apache.commons.configuration.ConfigurationException;
import spark.Spark;

public class LocalServer {

	public static void main(String... args) {
		/*
		SERVER SETTINGS
		 */

		//initialise constants
		try {
			C.initConstants();
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}

		// static files folder
		Spark.staticFileLocation("/static");

		// page security
		Spark.before((request, response) -> {
			// get path
			String path = request.pathInfo();

			// check if they are accessing non-page
			if (path.startsWith("/css")
					|| path.startsWith("/images")
					|| path.startsWith("/js")
					|| path.startsWith("/font-awesome")) {
				// nothing more to do - everything is fine
				return;
			}

			// check for a password force-change
			if (SecurityUtils.checkLoggedIn()
					&& SecurityUtils.getCurrentUser() != null
					&& SecurityUtils.getCurrentUser().getDaysRemainingToPasswordChange() < 1
					&& !path.startsWith("/profile")) {
				response.redirect("/profile?changepw=1&force=1");
			}

			// check if they are accessing a non-secure page
			if (path.startsWith("/login")) {
				// nothing more to do - everything is fine
				return;
			}

			// not authenticated?
			if (!SecurityUtils.checkLoggedIn()) {
				// send them back to the login page
				response.redirect("/login");
			}
		});

		// prevent viewing pages after logout
		Spark.after((request, response) -> {
			// get path
			String path = request.pathInfo();

			// don't apply this for resources
			if (path.startsWith("/css")
					|| path.startsWith("/images")
					|| path.startsWith("/js")
					|| path.startsWith("/font-awesome")) {
				// nothing more to do - everything is fine
				return;
			}

			response.header("Cache-Control", "no-cache, no-store, must-revalidate");
			response.header("Pragma", "no-cache");
			response.header("Expires", "0");
		});

		/*
		ROUTES
		 */

		// basic pages
		Spark.get("/", new DashboardRoute());

        // day summary
        Spark.get("/day-summary", new DaySummaryRoute());

		// security
		Spark.get("/access-denied", new AccessDeniedRoute());
		Spark.get("/login", new LoginRoute(RequestType.GET));
		Spark.post("/login", new LoginRoute(RequestType.POST));
		Spark.get("/logout", new LogoutRoute());
		Spark.get("/profile", new ProfileRoute());

		// action logs
		Spark.get("/action-log", new ActionLogRoute());

		// entity CRUD action routes
		Spark.post("/entity-update", new CrudCreateUpdateRoute());
		Spark.post("/entity-delete", new CrudDeleteRoute());

		// basic CRUD pages
		Spark.get("/cameras", new CamerasRoute());
		Spark.get("/camera-types", new CameraTypesRoute());
		Spark.get("/patients", new PatientsRoute());
		Spark.get("/staff", new StaffRoute());
		Spark.get("/staff-roles", new StaffRolesRoute());
		Spark.get("/therapies", new TherapiesRoute());
		Spark.get("/tracers", new TracersRoute());

		// staff absences and availabilities
		Spark.get("/select-staff/:target:", new SelectStaffRoute());
		Spark.get("/staff-absences/:staffid:", new StaffAbsencesRoute());
		Spark.get("/staff-availabilities/:staffid:", new StaffAvailabilitiesRoute());

		// bookings
		Spark.get("/new-booking-1", new NewBookingRouteStage1());
		Spark.post("/new-booking-2", new NewBookingRouteStage2());
		Spark.post("/new-booking-3", new NewBookingRouteStage3());
		Spark.get("/bookings", new BookingsRoute());
		Spark.get("/booking-details/:bookingid:", new BookingDetailsRoute());
		Spark.get("/booking-details/:bookingid:/:newstatus:", new BookingDetailsRoute());

		// calendar data
		Spark.get("/calendar-data", new CalendarDataRoute());

		// tracer orders
		Spark.get("/tracer-orders", new TracerOrdersRoute());
		Spark.get("/tracer-order-details/:tracerorderid:", new TracerOrderDetailsRoute());
		Spark.get("/tracer-order-details/:tracerorderid:/:newstatus:", new TracerOrderDetailsRoute());

		// patients
		Spark.get("/patient-details/:patientid:", new PatientDetailsRoute());

		// export
		Spark.get("/export/:file:", new ExportRoute());

		// import
		Spark.post("/import", new ImportRoute());
	}

}

@@ -135,16 +135,47 @@
 yUtils;%0A
+import nuclibook.models.Staff;%0A
 import n
@@ -257,16 +257,38 @@
 eption;%0A
+import spark.Session;%0A
 import s
@@ -942,32 +942,163 @@
 %09%09return;%0A%09%09%09%7D%0A%0A
+%09%09%09// get current session and user%0A%09%09%09Session session = request.session();%0A%09%09%09Staff user = SecurityUtils.getCurrentUser(session);%0A%0A
 %09%09%09// check for 
@@ -1156,16 +1156,23 @@
 oggedIn(
+session
 )%0A%09%09%09%09%09&
@@ -1177,85 +1177,33 @@
 %09&& 
-SecurityUtils.getCurrentUser() != null%0A%09%09%09%09%09&& SecurityUtils.getCurrentU
+user != null%0A%09%09%09%09%09&& u
 ser
-()
 .get
@@ -1552,16 +1552,23 @@
 oggedIn(
+session
 )) %7B%0A%09%09%09

Add sessions to LocalServer