package halleck.webserver.routs;

import com.google.inject.Inject;
import halleck.api.Settings;
import halleck.webserver.*;
import spark.ModelAndView;
import spark.Request;
import spark.Response;

import java.util.Map;
import java.util.Objects;

import static halleck.webserver.MapMaker.map;

public class AuthenticationRouts extends SparkRoutCollector {

    private final Settings settings;
    private final Authenticator auth;


    @Inject
    public AuthenticationRouts(Settings settings,
                               Authenticator auth) {

        this.settings = settings;
        this.auth = auth;
    }

    public void init(){
        get(new FullPage("/login") {
            @Override
            public ModelMapView action(Request request, Response response) {
                return new ModelMapView(getSettings(),"login.mustache");
            }

            private Map getSettings() {
                return map("showpass", !Objects.equals(settings.getAuthenticationType(), "fake"));
            }
        });

        post(new FullPage("/login") {
            @Override
            public ModelMapView action(Request request, Response response) {
                String username = request.queryParams("username");
                String password = request.queryParams("password");

                if(auth.authenticate(username, password)){
                    response.cookie(SecurityFilter.USERNAME_COOKIE, username);
                    return new ModelMapView(map("url", "/"), "redirect.mustache");
                }
                return new ModelMapView(map("wrong", true), "login.mustache");
            }
        });

        get(new FullPage("/logout") {
            @Override
            public ModelMapView action(Request request, Response response) {
                response.removeCookie(SecurityFilter.USERNAME_COOKIE);
                response.redirect("/login");
                return null;
            }
        });
    }
}

@@ -649,154 +649,27 @@
 get(
-new FullPage(%22/login%22) %7B%0A            @Override%0A            public ModelMapView action(Request request, Response response) %7B%0A                return
+ %22/login%22, (q,p) -%3E
  new
@@ -705,32 +705,33 @@
 login.mustache%22)
+)
 ;%0A            %7D%0A
@@ -728,158 +728,106 @@
     
-    %7D%0A%0A            private Map getSettings() %7B%0A                return map(%22showpass%22, !Objects.equals(settings.getAuthenticationType(), %22fake%22
+post(%22/login%22, (q,p) -%3E loginAction(q, p));%0A        get(%22/logout%22, (q,p) -%3E logoutAction(p
 ));%0A    
     
@@ -826,39 +826,141 @@
     
+%7D%0A%0A
     
-    %7D%0A        %7D
+private ModelMapView logoutAction(Response response) %7B%0A        response.removeCookie(SecurityFilter.USERNAME_COOKIE
 );%0A
-%0A
         
 post
@@ -959,25 +959,25 @@
     
-post(new FullPage
+response.redirect
 (%22/l
@@ -982,30 +982,26 @@
 /login%22)
- %7B%0A   
+;%0A
         
  @Overri
@@ -996,37 +996,39 @@
     
- @Override
+return null;
 %0A    
-    
+%7D%0A%0A
     p
-ublic
+rivate
  Mod
@@ -1029,33 +1029,38 @@
 te ModelMapView 
-a
+loginA
 ction(Request re
@@ -1083,32 +1083,24 @@
 response) %7B%0A
-        
         Stri
@@ -1154,24 +1154,16 @@
         
-     
-   
 String p
@@ -1206,24 +1206,16 @@
 ord%22);%0A%0A
-        
         
@@ -1265,32 +1265,24 @@
             
-   
-     
 response.coo
@@ -1328,24 +1328,16 @@
 rname);%0A
-        
         
@@ -1411,34 +1411,18 @@
         
-        %7D%0A        
+%7D%0A
         
@@ -1492,336 +1492,131 @@
     
-        %7D%0A        %7D);%0A%0A        get(new FullPage(%22/logout%22) %7B%0A            @Override%0A            public ModelMapView action(Request request, Response response) %7B%0A                response.removeCookie(SecurityFilter.USERNAME_COOKIE);%0A                response.redirect(%22/login%22);%0A                return null;%0A            %7D%0A        %7D
+%7D%0A%0A    private Map getSettings() %7B%0A        return map(%22showpass%22, !Objects.equals(settings.getAuthenticationType(), %22fake%22)
 );%0A 

add lambdas for authentication