<?xml version="1.0" encoding="UTF-8" ?>
<!--
  ~ Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
  ~
  ~  WSO2 Inc. licenses this file to you under the Apache License,
  ~  Version 2.0 (the "License"); you may not use this file except
  ~  in compliance with the License.
  ~  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~  Unless required by applicable law or agreed to in writing,
  ~  software distributed under the License is distributed on an
  ~  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~  KIND, either express or implied.  See the License for the
  ~  specific language governing permissions and limitations
  ~  under the License.
  -->
<Module>
    <ModulePrefs title="Account Recovery" >
        <Require feature="pubsub-2"/>
    </ModulePrefs>
    <Content type="html" view="default">
        <![CDATA[
        <link rel="stylesheet" type="text/css" href="js/ui/css/main.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/smoothness/jquery-ui-1.12.1.custom.min.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/smoothness/jqueryui-themeroller.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/dialog.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/bootstrap.min.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/bootstrap-theme.css">

	    <script src="js/jquery.min.js" type="text/javascript"></script>
	    <script type="text/javascript" src="serverinfo.jag"></script>
        <script>
            var headID = document.getElementsByTagName("head")[0];
            var cssNode = document.createElement('link');
            cssNode.type = 'text/css';
            cssNode.rel = 'stylesheet';
            cssNode.href = PROXY_CONTEXT_PATH + '/portal/gadgets/my-login-sessions/js/ui/font-awesome/css/font-awesome.min.css';
            headID.appendChild(cssNode);
        </script>
	<script>
		$(function() {
			$('.max_view').click(function() {
				 gadgets.Hub.publish('org.wso2.is.dashboard', {
                            msg : 'A message from My profile',
                            id: "my_login_sessions .expand-widget"
                        });
			});
		});


	</script>
        <div class='icon-th-list icon-th-list-user icon-marketing-styles'></div>
    Manage login sessions.
    <p><a class='btn btn-default max_view' href=''>View details</a></p>
        ]]>
    </Content>

    <Content type="html" view="home">
        <![CDATA[
        <script type="text/javascript" src="/portal/csrf.js"></script>
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript" src="js/main.js"></script>
        <script type="text/javascript" src="js/modal.js"></script>
        <script type="text/javascript" src="js/gadget.js"></script>
        <script type="text/javascript" src="serverinfo.jag"></script>

        <link rel="stylesheet" type="text/css" href="js/ui/css/main.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/bootstrap.min.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/bootstrap-theme.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/bootstrap-missing.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/bootstrap-theme.min.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/dialog.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/smoothness/jquery-ui-1.12.1.custom.min.css">
        <link rel="stylesheet" type="text/css" href="js/ui/css/smoothness/jqueryui-themeroller.css">

        <script>
            var headID = document.getElementsByTagName("head")[0];
            var cssNode = document.createElement('link');
            cssNode.type = 'text/css';
            cssNode.rel = 'stylesheet';
            cssNode.href = PROXY_CONTEXT_PATH + '/portal/gadgets/my-login-sessions/js/ui/font-awesome/css/font-awesome.min.css';
            headID.appendChild(cssNode);

            function terminateSession(sessionId) {

                var confirmationDialog = "<div class=\"modal fade\" id=\"messageModal\">\n" +
		        "  <div class=\"modal-dialog\">\n" +
		        "    <div class=\"modal-content\">\n" +
		        "      <div class=\"modal-header\">\n" +
		        "        <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\">&times;</button>\n" +
		        "        <h3 class=\"modal-title\">Modal title</h4>\n" +
		        "      </div>\n" +
		        "      <div class=\"modal-body\">\n" +
		        "        <p>One fine body&hellip;</p>\n" +
		        "      </div>\n" +
		        "      <div class=\"modal-footer\">\n" +
		        "      </div>\n" +
		        "    </div>\n" +
		        "  </div>\n" +
		        "</div>";

                $("#message").html(confirmationDialog);
                    message({
                        title: "Confirm Session Termination.",
                        content: 'Are you sure you want to terminate this session? This operation is not reversible.',
                        type: 'confirm',
                        okCallback: function () {
                            terminate(sessionId);
                        }
                });
            }

            function terminate(sessionId) {
                $.ajax({
                    type:"POST",
                    url:PROXY_CONTEXT_PATH + "/portal/gadgets/my-login-sessions/server-new.jag",
                    data: { sessionId : sessionId }

                }).done(function (data) {
                    var options = {
                        "title" : "Terminate Session.",
                        "type" : "info",
                        "content": "Successfully terminated the session.",
                        "cbk": function() {
                                            $('#messageModal').modal('hide');
                                            location.reload();
                                        }
                      };
                    validate(options);
                    return data;
                }).fail(function () {
                    console.log('error');
                    var options = {
                            "title" : "Terminate Session.",
                            "content": "Error while terminating the requested session.",
                            "type" : "error",
                            "cbk": function() {
                                            $('#messageModal').modal('hide');
                                            location.reload();
                                        }
                            };
                    validate(options);
                }).always(function () {
                    console.log('completed');
                });
            }

            function validate(params) {

	            var element = "<div class=\"modal fade\" id=\"messageModal\">\n" +
		        "  <div class=\"modal-dialog\">\n" +
		        "    <div class=\"modal-content\">\n" +
		        "      <div class=\"modal-header\">\n" +
		        "        <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\">&times;</button>\n" +
		        "        <h3 class=\"modal-title\">Modal title</h4>\n" +
		        "      </div>\n" +
		        "      <div class=\"modal-body\">\n" +
		        "        <p>One fine body&hellip;</p>\n" +
		        "      </div>\n" +
		        "      <div class=\"modal-footer\">\n" +
		        "      </div>\n" +
		        "    </div>\n" +
		        "  </div>\n" +
		        "</div>";
	    	    $("#message").append(element);
                message(params);

	        }

        </script>

        <script type="text/javascript">
            var cookie = null;
            var json = null;
            var userName = null;
            var serverUrl = window.location.host + PROXY_CONTEXT_PATH;

            $(function WindowLoad(event) {
                url = 'wss://' + serverUrl + '/dashboard/session_manager.jag';
                ws = new WebSocket(url);

                ws.onopen = function () {
                    console.log("web Socket onopen. ");
                    ws.send("First Message open");
                };
                
                //event handler for the message event in the case of text frames
                ws.onmessage = function (event) {
                    var obj = $.parseJSON(event.data);
                    cookie = obj.cookie;
                    userName = obj.user;
                    if (cookie != null) {
                        var str = PROXY_CONTEXT_PATH + "/portal/gadgets/my-login-sessions/index-new.jag";

                        $.ajax({
                            type:"POST",
                            url:str,
                            data: {cookie : cookie, user : userName}

                        }).done(function (data) {
                            drawPage(data);

                        }).fail(function () {
                            console.log('error');

                        }).always(function () {
                            console.log('completed');
                        });
                    }
                };
                ws.onclose = function () {
                    console.log("web Socket onclose. ");
                };
            });

            function drawPage(data) {
                var top =
            "    <div class=\"col-lg-12 content-section\">\n" +
            "        <table class=\"table table-bordered\">\n" +
            "            <thead>\n" +
            "                <tr>\n" +
            "                    <th class='txtAlnCen width30p'>IP Address</th>\n" +
            "                    <th class='txtAlnCen width30p'>User Agent</th>\n" +
            "                    <th class='txtAlnCen width30p'>Started Time</th>\n" +
            "                    <th class='txtAlnCen'>Actions</th>\n" +
            "                </tr>\n" +
            "            </thead>\n" +
            "            <tbody>\n";


            var jsonData = JSON.parse(data);
            var middle = "";
            for (var i = 0; i < jsonData.length; i++){
                middle = middle + "<tr>\n" +
                         "<td>" + jsonData[i].IP + "</td>\n" +
                         "<td>" + jsonData[i].userAgent + "</td>\n" +
                         "<td>" + jsonData[i].startTimestamp + "</td>\n" +
                         "<td>" + ' <input class="btn btn-primary" type="button" value="Terminate" onclick="terminateSession(\''+ jsonData[i].sessionId +'\')"> '  + "</td>\n" +
                         "</tr>";
            }
                $("#gadgetBody").empty();
                $("#gadgetBody").append(top + middle);
            }

        function cancel(){
           gadgets.Hub.publish('org.wso2.is.dashboard', {
                        msg:'A message from User profile',
                        id:"my_login_sessions  .shrink-widget"
           });

        }


        </script>


        <form id="gadgetForm" class="form-horizontal">
            <div id="gadgetBody"></div>
	    <div id="message"></div>
        </form>

        ]]>
    </Content>
</Module>
