package com.wizzardo.servlet;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Collections;
import java.util.Enumeration;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: wizzardo
 * Date: 17.10.14
 */
public class Session implements HttpSession {

    private com.wizzardo.http.Session session;
    private ConcurrentHashMap<String, Object> map;
    private boolean newSession = false;
    private long creationTime = System.currentTimeMillis();
    private long lastAccessedTime;

    Session(com.wizzardo.http.Session session) {
        this.session = session;
        map = (ConcurrentHashMap<String, Object>) session.get("data");
        if (map == null) {
            map = new ConcurrentHashMap<String, Object>();
            newSession = true;
            session.put("data", map);
        }
    }

    @Override
    public long getCreationTime() {
        return creationTime;
    }

    @Override
    public String getId() {
        return session.getId();
    }

    @Override
    public long getLastAccessedTime() {
        return lastAccessedTime;
    }

    @Override
    public ServletContext getServletContext() {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void setMaxInactiveInterval(int interval) {
        session.setTTL(interval * 1000l);
    }

    @Override
    public int getMaxInactiveInterval() {
        return (int) (session.getTTL() / 1000);
    }

    @Override
    public HttpSessionContext getSessionContext() {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public Object getAttribute(String name) {
        return map.get(name);
    }

    @Override
    public Object getValue(String name) {
        return getAttribute(name);
    }

    @Override
    public Enumeration<String> getAttributeNames() {
        return Collections.enumeration(map.keySet());
    }

    @Override
    public String[] getValueNames() {
        return map.keySet().toArray(new String[map.size()]);
    }

    @Override
    public void setAttribute(String name, Object value) {
        map.put(name, value);
    }

    @Override
    public void putValue(String name, Object value) {
        setAttribute(name, value);
    }

    @Override
    public void removeAttribute(String name) {
        map.remove(name);
    }

    @Override
    public void removeValue(String name) {
        removeAttribute(name);
    }

    @Override
    public void invalidate() {
        map.clear();
    }

    @Override
    public boolean isNew() {
        return newSession;
    }
}

@@ -444,16 +444,58 @@
 ect%3E map
+ = new ConcurrentHashMap%3CString, Object%3E()
 ;%0A    pr
@@ -512,25 +512,19 @@
 ean 
-newSession = fals
+isNew = tru
 e;%0A 
@@ -619,14 +619,37 @@
 Time
-;%0A%0A   
+ = creationTime;%0A%0A    private
  Ses
@@ -733,50 +733,8 @@
     
- map = (ConcurrentHashMap%3CString, Object%3E)
  ses
@@ -742,18 +742,18 @@
 ion.
-ge
+pu
 t(%22data%22
 );%0A 
@@ -752,175 +752,403 @@
 ata%22
+, this
 );%0A    
+%7D%0A%0A
     
-if (map == null) %7B%0A            map = new ConcurrentHashMap%3CString, Object%3E();%0A            newSession = true;%0A            session.put(%22data%22, map);%0A        %7D
+static Session create(com.wizzardo.http.Session session) %7B%0A        return new Session(session);%0A    %7D%0A%0A    static Session get(com.wizzardo.http.Session session) %7B%0A        return (Session) session.get(%22session%22);%0A    %7D%0A%0A    void setIsNew(boolean isNew) %7B%0A        this.isNew = isNew;%0A    %7D%0A%0A    void updateAccessedTime() %7B%0A        lastAccessedTime = System.currentTimeMillis();
 %0A   
@@ -2915,18 +2915,13 @@
 urn 
-newSession
+isNew
 ;%0A  

improve session