/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *  
 */
// https://issues.jboss.org/browse/TEIID-2507
// https://issues.redhat.com/browse/TEIID-2172
// https://issues.redhat.com/browse/TEIID-5246
// https://issues.redhat.com/browse/TEIID-3663
package org.teiid.adminapi.impl;

import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import org.teiid.adminapi.Session;
import org.teiid.client.security.SessionToken;
import org.wildfly.security.auth.server.SecurityIdentity;


/**
 * Add and delete properties also in the Mapper class for correct wrapping for profile service.
 * 
 */
/* TODO: it would probably be good to let ipAddress denote the connecting address 
 and add clientIpAdress as the client reported value */
public class SessionMetadata extends AdminObjectImpl implements Session, Cloneable {
	private static final long serialVersionUID = 918638989081830034L;

	private static class GlobalState implements Serializable {
		private String applicationName;
		private volatile long lastPingTime = System.currentTimeMillis();
		private long createdTime;
		private String ipAddress;
		private String clientHostName;
		private String clientHardwareAddress;
		private String vdbName;
		private int vdbVersion;
		private String sessionId;
		private OpenIDMetadata openIDMetadata;
		private String userRoleType;

		//server session state
		private transient VDBMetaData vdb;
		private transient SessionToken sessionToken;
		private String securityDomain;
		private transient boolean embedded;
		private final transient Map<String, Object> sessionVariables = Collections.synchronizedMap(new HashMap<>(2));
		private volatile boolean closed;
		private final AtomicLong bytesUsed = new AtomicLong();
		private volatile boolean active = true;
	}

	private final GlobalState globalState = new GlobalState();
	private String userName;
	private transient SecurityIdentity subject;

	@Override
	public String getApplicationName() {
		return this.globalState.applicationName;
	}
	
    public void setApplicationName(String applicationName) {
		this.globalState.applicationName = applicationName;
	}	
    
	@Override
	public long getCreatedTime() {
		return this.globalState.createdTime;
	}

	public void setCreatedTime(long createdTime) {
		this.globalState.createdTime = createdTime;
	}

	@Override
	public String getClientHostName() {
		return this.globalState.clientHostName;
	}

	public void setClientHostName(String clientHostname) {
		this.globalState.clientHostName = clientHostname;
	}

	@Override
	public String getIPAddress() {
		return this.globalState.ipAddress;
	}

	public void setIPAddress(String ipAddress) {
		this.globalState.ipAddress = ipAddress;
	}

	@Override
	public long getLastPingTime() {
		return this.globalState.lastPingTime;
	}

	public void setLastPingTime(long lastPingTime) {
		this.globalState.lastPingTime = lastPingTime;
	}

	@Override
	public String getSessionId() {
		return this.globalState.sessionId;
	}

	public void setSessionId(String sessionId) {
		this.globalState.sessionId = sessionId;
	}

	@Override
	public String getUserName() {
		return this.userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Override
	public String getVDBName() {
		return this.globalState.vdbName;
	}

	public void setVDBName(String vdbName) {
		this.globalState.vdbName = vdbName;
	}

	@Override
	public int getVDBVersion() {
		return this.globalState.vdbVersion;
	}

	public void setVDBVersion(int vdbVersion) {
		this.globalState.vdbVersion = vdbVersion;
	}

	@Override
	public String getSecurityDomain() {
		return this.globalState.securityDomain;
	}
	
	public void setSecurityDomain(String domain) {
		this.globalState.securityDomain = domain;
	}

	public OpenIDMetadata getOpenIDMetadata() {
		return globalState.openIDMetadata;
	}

	public void setOpenIDMetadata(OpenIDMetadata openIDMetadata) {
		this.globalState.openIDMetadata = openIDMetadata;
	}

	public String getUserRoleType() {
		return globalState.userRoleType;
	}

	public void setUserRoleType(String userRoleType) {
		this.globalState.userRoleType = userRoleType;
	}

	@SuppressWarnings("nls")
	public String toString() {
    	StringBuilder str = new StringBuilder();
    	str.append("session: sessionid=").append(globalState.sessionId);
    	str.append("; userName=").append(userName);
		str.append("; userRoleType=").append(globalState.userRoleType);
    	str.append("; vdbName=").append(globalState.vdbName);
    	str.append("; vdbVersion=").append(globalState.vdbVersion);
    	str.append("; createdTime=").append(new Date(globalState.createdTime));
    	str.append("; applicationName=").append(globalState.applicationName);
    	str.append("; clientHostName=").append(globalState.clientHostName);
    	str.append("; clientHardwareAddress=").append(globalState.clientHardwareAddress);
    	str.append("; IPAddress=").append(globalState.ipAddress);
    	str.append("; securityDomain=").append(globalState.securityDomain);
    	str.append("; lastPingTime=").append(new Date(globalState.lastPingTime));
    	return str.toString();
    }

	public VDBMetaData getVdb() {
		return globalState.vdb;
	}

	public void setVdb(VDBMetaData vdb) {
		this.globalState.vdb = vdb;
	}

	public SessionToken getSessionToken() {
		return globalState.sessionToken;
	}

	public void setSessionToken(SessionToken sessionToken) {
		this.globalState.sessionToken = sessionToken;
	}

	public void setSubject(SecurityIdentity subject) {
		this.subject = subject;
	}

	public SecurityIdentity getSubject() {
		return this.subject;
	}
	
	public void setEmbedded(boolean embedded) {
		this.globalState.embedded = embedded;
	}

	public boolean isEmbedded() {
		return globalState.embedded;
	}
	
	@Override
	public String getClientHardwareAddress() {
		return this.globalState.clientHardwareAddress;
	}
	
	public void setClientHardwareAddress(String clientHardwareAddress) {
		this.globalState.clientHardwareAddress = clientHardwareAddress;
	}

	public Map<String, Object> getSessionVariables() {
		return globalState.sessionVariables;
	}

	public void setClosed() {
		this.globalState.closed = true;
	}

	public boolean isClosed() {
		return globalState.closed;
	}

	public long getBytesUsed() {
		return globalState.bytesUsed.get();
	}

	public long addAndGetBytesUsed(long bytes) {
		return this.globalState.bytesUsed.addAndGet(bytes);
	}

	public boolean isActive() {
		return globalState.active;
	}

	public void setActive(boolean active) {
		this.globalState.active = active;
	}

	@Override
	public SessionMetadata clone() {
		try {
			return (SessionMetadata) super.clone();
		} catch (CloneNotSupportedException e) {
			throw new AssertionError();
		}
	}
}
