<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="My Profile">
    <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/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 type="text/javascript" src="js/jquery.min.js"></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/backup_otp/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: "backup_otp  .expand-widget"
                });
            });
        });
    </script>

    <div class='icon-unlock-alt icon-unlock-alt-dashboard icon-marketing-styles'></div>
    Download and Generate List of SMS OTP Backup Codes.
    <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" type="text/javascript"></script>
    <script type="text/javascript" src="js/main.js" type="text/javascript"></script>
    <script type="text/javascript" src="js/modal.js" type="text/javascript"></script>
    <script type="text/javascript" src="js/gadget.js" type="text/javascript"></script>
    <script type="text/javascript" src="js/u2f-api.js" type="text/javascript"></script>
    <script type="text/javascript" src="serverinfo.jag"></script>

    <script type="text/javascript" src="../wso2-dev/js/jquery.numberformatter-1.2.3.js" type="text/javascript"></script>
    <script type="text/javascript" src="../wso2-dev/js/jquery.dependClass-0.1.js" type="text/javascript"></script>
    <script type="text/javascript" src="../wso2-dev/js/jquery.slider.js" type="text/javascript"></script>
    <script type="text/javascript" src="../../themes/portal/js/UESContainer.js"></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/backup_otp/js/ui/font-awesome/css/font-awesome.min.css';
        headID.appendChild(cssNode);
    </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");
            };
            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/backup_otp/index.jag";

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

                    })
                            .done(function (data) {
                                json = $.parseJSON(data);
                                drawPage();
                            })
                            .fail(function () {
                                console.log('error');

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

        function generate() {

            var str = PROXY_CONTEXT_PATH + "/portal/gadgets/backup_otp/controllers/backup_otp/update-otp.jag";
            $.ajax({
                url: str,
                type: "POST",
                data: $('#gadgetForm').serialize() + "&profileConfiguration=default" + "&cookie=" + cookie + "&user=" + userName
            })
                    .done(function (data) {

				    	url = 'wss://' + serverUrl + '/dashboard/session_manager.jag';
			            ws = new WebSocket(url);

			            ws.onopen = function () {
			                console.log("web Socket onopen. ");
			                ws.send("First Message open");
			            };
			            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/backup_otp/index.jag";

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

			                    })
			                            .done(function (data) {
			                                json = $.parseJSON(data);			                                
			                                drawPage();
			                                download();
			                            })
			                            .fail(function () {
			                                console.log('error');
			                            })
			                            .always(function () {
			                                console.log('completed');
			                            });
			                }
			            };
			            ws.onclose = function () {
			                console.log("web Socket onclose. ");
			            };
                    })
                    .fail(function () {
                        message({content: 'Error while updating Backup OTP Codes', type: 'error', cbk: function () {
                        } });

                    })
                    .always(function () {
                        console.log('completed');
                    });

        }

    </script>
    
    <form id="gadgetForm" class="form-horizontal">
        <div id="gadgetBody" style="text-align: center;width: 40%;margin: auto;"></div>
        <div id="message"></div>
    </form>

    ]]>
</Content>
</Module>
