package controllers;

import dao.IUserDao;
import dao.impl.BlogPostDao;
import dao.IBlogPostDao;
import dao.impl.OrientBlogPostDao;
import dao.impl.OrientUserDao;
import dao.impl.UserDao;
import models.*;
import models.user.User;
import play.data.Form;
import play.mvc.*;
import play.Routes;

import views.html.*;

public class Application extends Controller {

   //TODO DI ?
   public static IBlogPostDao postDao = new OrientBlogPostDao();
   public static IUserDao userDao = new OrientUserDao();

   public static Result index(int page) {
      if (page < 1)
         return redirect(routes.Application.index(1));
      return ok(index.render(Post.page(page, getLocalUser(session()) != null), postDao.findAll()));
   }


   public static Result login() {
      return ok(
              login.render(Form.form(Login.class))
      );
   }

   public static Result logout() {
      session().clear();
      flash("success", "You've been logged out");
      return redirect(
              routes.Application.login()
      );
   }

   public static Result authenticate() {
      Form<Login> loginForm = Form.form(Login.class).bindFromRequest();
      if (loginForm.hasErrors()) {
         return badRequest(login.render(loginForm));
      } else {
         session().clear();
         session("email", loginForm.get().email);
         return redirect(
                 routes.Application.admin()
         );
      }
   }

   /**
    * Display admin page
    *
    * @return
    */
   @Security.Authenticated(Secured.class)
   public static Result admin() {
      return ok(admin.render(postDao.findAll()));
   }

   /**
    * Need for referencing routes from js;
    *
    * @return
    */
   public static Result javascriptRoutes() {
      response().setContentType("text/javascript");
      return ok(
              Routes.javascriptRouter("jsRoutes",
                      controllers.routes.javascript.Posts.delete(),
                      controllers.routes.javascript.Posts.doEdit()
              )
      );
   }


   public static class Login {

      public String email;
      public String password;

      public String validate() {
         if (!userDao.exist(email, password)) {
            return "Invalid user or password";
         }
         return null;
      }

   }


   public static User getLocalUser(Http.Session session) {
      if (session.get("email") == null)
         return null;
      else
         return userDao.findByEmail(session.get("email"));
   }


}

@@ -2055,57 +2055,369 @@
    p
-ublic String email;%0A      public String password;
+rivate String email;%0A      private String password;%0A%0A      public String getEmail() %7B%0A         return email;%0A      %7D%0A%0A      public void setEmail(String email) %7B%0A         this.email = email;%0A      %7D%0A%0A      public String getPassword() %7B%0A         return password;%0A      %7D%0A%0A      public void setPassword(String password) %7B%0A         this.password = password;%0A      %7D
 %0A%0A  

add getter and setter manually; fix login issue