<!DOCTYPE HTML>
<html>
<head>
    <title>Sharable Knowledge Objects Core Services</title>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.0.0/socket.io.min.js"></script>
    <script type="text/javascript" src="js/SuperGLU/Util/emacs5-compatibility-patches.js"></script>
	<script type="text/javascript" src="js/SuperGLU/Util/uuid.js"></script>
	<script type="text/javascript" src="js/SuperGLU/Util/zet.js"></script>
	<script type="text/javascript" src="js/SuperGLU/Util/serialization.js"></script>
	<script type="text/javascript" src="js/SuperGLU/Core/messaging.js"></script>
	<script type="text/javascript" src="js/SuperGLU/Core/messaging-gateway.js"></script>
	<script type="text/javascript" src="js/SuperGLU/Services/StorageService/Storage_Service_Client.js"></script>
	<script type="text/javascript" src="js/SuperGLU/Services/StorageService/Local_Storage_Service.js"></script>
	<script type="text/javascript" src="js/SuperGLU/Services/default-client-gateway.js"></script>
    <script type="text/javascript" charset="utf-8">
    var GATEWAY, STORAGE_SERVICE, TEST_SERVICE;
    
    $(window).ready(function(){
        // Use prod.x-in-y.com when released
        //socket.on('message', function(msg) {
            //$('#log').append('<br>Received # : ' + msg.data);
            // HTML: 
            // 	<h2>Receive:</h2>
            // 	<div id="log"></div>
        //});
        STORAGE_SERVICE = Storage_Service_Client.StorageServiceInterface(null, "ONR", "BatchStorageServiceClient");
        TEST_SERVICE = Client_Messaging_Gateway.TestService("TestService");
        GATEWAY = Client_Messaging_Gateway.HTTPMessagingGateway(null, 
                                                                [TEST_SERVICE,
                                                                 STORAGE_SERVICE],
                                                                window.location.protocol + document.domain + ':' + location.port);
    });
    
    // Current problem, assumed to be a global variable.
    var SESSION_ID = UUID.genV4().toString();

    function getUserName(){
        return document.getElementById("UserNameText").value;
    }
    function getComments() {
		return document.getElementById("commentsText").value;
	}
	function reportButtonFunction() {
		var msg;
        msg = Messaging.Message("Index Page", "Bug Reported From", document.referrer, getComments());
        msg.setContextValue(Messaging.AUTHORIZATION_KEY, getUserName());
        msg.setContextValue(Messaging.SESSION_ID_KEY, SESSION_ID);
        msg.updateTimestamp();
		// Get the current problem: global variable!
		//alert("Time: " + time + "\nProblem: " + PROBLEM_NAME + "\nText: "	+ text + "\nComments: " + comments);
		TEST_SERVICE.sendMessage(msg);
        document.getElementById("commentsText").value = ""; // Clear comments.
	}
    function writeAllPageData(){
        writeAllData(HTML_LIST, processPageData);
    }
    function writeAllDialogData(){
        writeAllData(INPUT_DATA, null);
    }
    function writeAllDemoData(){
        writeAllData(PAL3_DEMO_SCRIPTS, processRawPack);
    }
    function writeAllData(dataSource, dataProcessor){
        var i, obj, dataSource, funct;
        if (dataSource == null){ dataSource = INPUT_DATA; }
        if (dataProcessor == null){ dataProcessor = function(val){ return val; }; }
        i = 0;
        funct = function(val){
            console.log(val);
            if (i<dataSource.length){
                writeData(funct, dataProcessor(dataSource[i]));
            }
            i += 1;
        };
        funct(null);
        //STORAGE_SERVICE.getDataKeys(function(val){console.log(val);});
    }
    function processPageData(data){
        var totalData = {};
        totalData.name = data[0];
        totalData.value = start_regx(data[1], false);
        totalData.tags = ['TutoringPageView'];
        return totalData;
    }
    function processRawPack(data){
        var totalData = {};
        console.log(data);
        totalData.value = data.value;
        totalData.name = null;
        totalData.tags = ['ASATXmlPack'];
        return totalData;
    }
    function writeData(callback, obj){
        var name, tags, value, funct;
        name = obj.name;
        tags = obj.tags;
        value = Serialization.nativizeObject(obj.value);
        if (name == null){
            name = value.getName();
        }
        console.log(value);
        if (value.setName != null){
            value.setName(name);
        }
        console.log(name);
        value.updateId();
        funct = function (key){
            console.log(key);
            console.log(value);
            value.updateId(key);
            STORAGE_SERVICE.setDataValue(callback, key, value, tags, null, null, name);
        };
        STORAGE_SERVICE.getDataIdByName(funct, name);
    }
    function showStorageContents(){
        var funct = function(keys){
            for (var i=0; i<keys.length; i++){
                STORAGE_SERVICE.getDataName(function(val){console.log(val);}, keys[i]);
            }
        };
        STORAGE_SERVICE.getDataKeys(funct);
    }
    </script>
</head>
<body>
    <h1>Welcome to the Sharable Knowledge Objects (SKO) Production Server!</h1>
    <p>If you are seeing this page, you are probably doing development, 
       or you are a user who somehow got lost!  If you are the latter, please
       report this bug using the "Report Bug" panel below.</p>
	
    <h2>Report Bug</h2>
    <p>Reporter Name: <input type="text" id="UserNameText" size="100" placeholder="Optionally, provide your name and/or email so we can follow up on this bug"></input></p>
    
    <!-- Comment: should allow writing a text comment -->
    <textarea id="commentsText" rows="2" cols="100" placeholder="Please give any details about the bug here."></textarea>
    <!-- Report: run a JS function that prints the current time, selected character name,
    the selected text, the current problem (assume this is a JS global variable), and
    the contents of the comment box.  After hitting "Report" the "Comment" box should be cleared. -->
    <br>
    <button id="reportButton" type="button" onClick="reportButtonFunction()">Report</button>

</body>
</html>
