package com.blade.mvc.handler;

import com.blade.Blade;
import com.blade.kit.ReflectKit;
import com.blade.kit.UUID;
import com.blade.mvc.http.Request;
import com.blade.mvc.http.Session;
import com.blade.mvc.http.session.SessionManager;
import com.blade.server.netty.HttpConst;

import java.time.Instant;

import static com.blade.mvc.Const.ENV_KEY_SESSION_KEY;
import static com.blade.mvc.Const.ENV_KEY_SESSION_TIMEOUT;

/**
 * session handler
 *
 * @author biezhi
 * 2017/6/3
 */
public class SessionHandler {

    private final Blade          blade;
    private final SessionManager sessionManager;
    private final String         sessionKey;
    private final int            timeout;

    public SessionHandler(Blade blade) {
        this.blade = blade;
        this.sessionManager = blade.sessionManager();
        this.sessionKey = blade.environment().get(ENV_KEY_SESSION_KEY, HttpConst.DEFAULT_SESSION_KEY);
        this.timeout = blade.environment().getInt(ENV_KEY_SESSION_TIMEOUT, 1800);
    }

    public Session createSession(Request request) {
        Session session = getSession(request);
        if (null == session) {
            long now     = Instant.now().getEpochSecond();
            long expired = now + timeout;

            String sessionId = UUID.UU32();

            session = ReflectKit.newInstance(blade.sessionType());
            session.id(sessionId);
            session.created(now);
            session.expired(expired);
            sessionManager.addSession(session);
            return session;
        } else {
            long now = Instant.now().getEpochSecond();
            if (session.expired() < now) {
                sessionManager.remove(session);
            } else {
                // renewal
                long expired = now + timeout;
                session.expired(expired);
            }
        }
        return session;
    }

    private Session getSession(Request request) {
        String cookieHeader = request.cookie(sessionKey);
        if (null == cookieHeader) {
            return null;
        }
        return sessionManager.getSession(cookieHeader);
    }

}

@@ -82,16 +82,48 @@
 ectKit;%0A
+import com.blade.kit.StringKit;%0A
 import c
@@ -1131,43 +1131,9 @@
 t);%0A
-        if (null == session) %7B%0A    
+%0A
     
@@ -1145,20 +1145,16 @@
 ong now 
-    
 = Instan
@@ -1171,32 +1171,63 @@
 tEpochSecond();%0A
+        if (null == session) %7B%0A
             long
@@ -1256,54 +1256,8 @@
 ut;%0A
-%0A            String sessionId = UUID.UU32();%0A%0A
     
@@ -1346,17 +1346,19 @@
 .id(
-sessionId
+UUID.UU32()
 );%0A 
@@ -1525,63 +1525,8 @@
 e %7B%0A
-            long now = Instant.now().getEpochSecond();%0A
     
@@ -1923,24 +1923,34 @@
     if (
-null == 
+StringKit.isEmpty(
 cookieHe
@@ -1954,16 +1954,17 @@
 eHeader)
+)
  %7B%0A     

add session cleaner