package com.github.vovas11.courses.command;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.github.vovas11.courses.dao.*;

/**
 * {@code LoginCommand} handles access for the existing user
 * 
 * @author vovas11
 * @see DaoFactory
 * @see UserDao
 * @see CourseDao
 * 
 */
public class LoginCommand implements Command {
    /**
     * Defines if the name and password of the user exists in the database and if yes,
     * returns the page for the user. If not, returns the home page
     *
     * @param   request   HTTP request
     * @return the the name of the page the server returns to the client
     */
    @Override
    public String execute(HttpServletRequest request) {
	
	/* gets parameters from the HTTP request */
	String login = request.getParameter("login");
	String password = request.getParameter("password");
	
	/* gets the link to the DaoFactory and UserDao and CourseDao */
	DaoFactory daoFactory = DaoFactory.getInstance();
	UserDao users = daoFactory.getUserDao();
	CourseDao courses = daoFactory.getCourseDao();
	
	/* gets the link to the current session or creates new one */
	HttpSession session = request.getSession();
	
	/* gets the link to the current user and if one does not exists yet
	 * creates new user instance and updates all fields */
	User user = (User) session.getAttribute(session.getId());
	
	// FIXME check the user credential after checking existence of the login
	
	if (user == null) {
	    user = new User();
	    user.setLogin(login);
	    user.setPassword(password);
	    
	    // gets all fields for the object from the database
	    users.getFieldsForUser(user);
	    session.setAttribute(session.getId(), user);
	}
	
	/* Checks if the user already Prepares the data for the JSP page:
	 * current user, subscribed courses and available courses */
	if (users.isExist(user)) {
	    // Information about the logged user
	    request.setAttribute("user", user);
	    // Getting subscribed courses
	    List<Course> subscribedCourses = courses.getSubscribedCourses(user);
	    request.setAttribute("subscrcourses", subscribedCourses);
	    
	    // Getting all courses from the DB
	    List<Course> courseList = courses.getAvailableCourses(user);
	    request.setAttribute("courses", courseList);
	    return "/courses.jsp";
	} else {
	    return "/index.html";
	}
    }
}

@@ -564,12 +564,16 @@
 the 
-home
+starting
  pag
@@ -1153,490 +1153,451 @@
 %09/* 
-gets the link to the current session or creates new one */%0D%0A%09HttpSession session = request.getSession();%0D%0A%09%0D%0A%09/* gets the link to the current user and if one does not exists yet%0D%0A%09 * creates new user instance and updates all fields */%0D%0A%09User user = (User) session.getAttribute(session.getId());%0D%0A%09%0D%0A%09// FIXME check the user credential after checking existence of the login%0D%0A%09%0D%0A%09if (user == null) %7B%0D%0A%09    user = new User();%0D%0A%09    user.setLogin(login);%0D%0A%09    user.setPassword(password
+creates new user and sets the fields received from the user form via HTTP request */%0D%0A%09User user = new User();%0D%0A%09user.setLogin(login);%0D%0A%09user.setPassword(password);%0D%0A%09%0D%0A%09/* checks if the user with entered credentials exists in the database:%0D%0A%09 * yes - proceed, no - forwards to the starting page */%0D%0A%09if (users.isExist(user)) %7B%0D%0A%09    /* gets the link to the current session or creates new one */%0D%0A%09    HttpSession session = request.getSession(
 );%0D%0A
@@ -1609,17 +1609,17 @@
 %0D%0A%09    /
-/
+*
  gets al
@@ -1639,14 +1639,12 @@
 the 
-object
+user
  fro
@@ -1657,16 +1657,51 @@
 database
+ and set the link to the session */
 %0D%0A%09    u
@@ -1786,46 +1786,23 @@
 ;%0D%0A%09
-%7D%0D%0A%09%0D%0A%09/* Checks if the user already P
+    %0D%0A%09    /* p
 repa
@@ -1835,16 +1835,20 @@
 age:%0D%0A%09 
+    
 * curren
@@ -1904,80 +1904,8 @@
 */%0D%0A
-%09if (users.isExist(user)) %7B%0D%0A%09    // Information about the logged user%0D%0A
 %09   
@@ -1947,26 +1947,30 @@
 ;%0D%0A%09    
-// Getting
+%0D%0A%09    /* gets
  subscri
@@ -1980,16 +1980,74 @@
  courses
+ from the database and set the link to the HTTP request */
 %0D%0A%09    L
@@ -2196,21 +2196,24 @@
    /
-/ Getting all
+* gets available
  cou
@@ -2230,10 +2230,56 @@
 the 
-DB
+database and set the link to the HTTP request */
 %0D%0A%09 
@@ -2423,16 +2423,23 @@
 .jsp%22;%0D%0A
+%09    %0D%0A
 %09%7D else 

fix signing in with wrong credentials