package com.openhackathon.guacamole;

import org.glyptodon.guacamole.GuacamoleException;
import org.glyptodon.guacamole.net.auth.Credentials;
import org.glyptodon.guacamole.net.auth.UserContext;
import org.glyptodon.guacamole.net.auth.simple.SimpleAuthenticationProvider;
import org.glyptodon.guacamole.net.auth.simple.SimpleConnection;
import org.glyptodon.guacamole.net.auth.simple.SimpleConnectionDirectory;
import org.glyptodon.guacamole.properties.StringGuacamoleProperty;
import org.glyptodon.guacamole.properties.GuacamoleProperties;
import org.glyptodon.guacamole.protocol.GuacamoleConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import java.util.*;


public class OpenHackathonAuthenticationProvider extends SimpleAuthenticationProvider {
	
	private Logger logger = LoggerFactory.getLogger(OpenHackathonAuthenticationProvider.class.getClass());
	
    private static final StringGuacamoleProperty AUTH_REQUEST_URL = new StringGuacamoleProperty() {

        @Override
        public String getName() { return "auth-request-url"; }
    };

    /*constructed functions*/
    
    public OpenHackathonAuthenticationProvider() {
    	logger.info("==============================gucamole authentication jar log init =============================================");
    }

    @Override
    public Map<String, GuacamoleConfiguration> getAuthorizedConfigurations(Credentials credentials) throws GuacamoleException {

        GuacamoleConfiguration config = getGuacamoleConfiguration(credentials.getRequest());

        if (config == null) {
            return null;
        }

        Map<String, GuacamoleConfiguration> configs = new HashMap<String, GuacamoleConfiguration>();
        configs.put(config.getConnectionID(), config);
        return configs;
    }

    @Override
    public UserContext updateUserContext(UserContext context, Credentials credentials) throws GuacamoleException {
        HttpServletRequest request = credentials.getRequest();
        GuacamoleConfiguration config = getGuacamoleConfiguration(request);
      
        if (config == null) {
            return null;
        }
        String id = config.getConnectionID();
        String name = config.getParameter("name");
        SimpleConnectionDirectory connections = (SimpleConnectionDirectory) context.getRootConnectionGroup().getConnectionDirectory();
        SimpleConnection connection = new SimpleConnection(name, id, config);
        connections.putConnection(connection);
        return context;
    }

    private GuacamoleConfiguration getGuacamoleConfiguration(HttpServletRequest request) throws GuacamoleException {
    	
    	GuacamoleConfiguration config ;
    	String jsonString = null;
        
        String tokenString = request.getParameter("token");
        String connectionID = request.getParameter("id");
        logger.info("tokenString is : |" + tokenString);
               
        /*check user valid or not*/
		try {
			
			String authRequestURL = GuacamoleProperties.getProperty(AUTH_REQUEST_URL);
			logger.info("OpenHackathon guacd Auth request URL is : " + authRequestURL);
			
			Connect2OpenHackathon conn = new Connect2OpenHackathon(authRequestURL);					
			jsonString = conn.getGuacamoleJSONString(connectionID,tokenString);
			logger.info("get guacamole config json String :" + jsonString);
			logger.info("before transformation jsobString is :" +jsonString.substring(1, jsonString.length()-1));
			
			Trans2GuacdConfiguration trans = new Trans2GuacdConfiguration(jsonString.substring(1, jsonString.length()-1));
			config = trans.getConfiguration();
			
			return config ;			
		} catch (Exception e) {
			logger.error("Exception when connect with open-hackathon to check User login");
			e.printStackTrace();
			return null;
		}

    }
}


@@ -1714,26 +1714,24 @@
 %0A        %7D%0D%0A
-%0D%0A
         Map%3C
@@ -1868,32 +1868,112 @@
 ID(), config);%0D%0A
+        logger.info(%22put configuration into The getAuthorizedConfigurations%22);%0D%0A
         return c
@@ -2569,24 +2569,108 @@
 rectory();%0D%0A
+        logger.info(%22get info from GuacamoleConfiguration,id:%22+id+%22,name:%22+ name);%0D%0A
         Simp
@@ -3661,61 +3661,34 @@
 %0A%09%09%09
-logger.info(%22before transformation jsobString is :%22 +
+%0D%0A%09%09%09String finalString = 
 json
@@ -3729,23 +3729,35 @@
 gth()-1)
-);%0D%0A%09%09%09
+.replace(%22%5C%5C%22, %22%22);
 %0D%0A%09%09%09Tra
@@ -3819,54 +3819,19 @@
 ion(
-jsonString.substring(1, jsonString.length()-1)
+finalString
 );%0D%0A

debug connection;