<%
        include("util/constants.jag");

        var user = session.get('user');
        var log = new Log();
        var authzFailure = request.getParameter("authz_failure");
        var identityServerURL = Packages.org.wso2.carbon.identity.core.util.IdentityUtil;

        if (user == null) {
            if (authzFailure != null) {
                response.sendRedirect("login.jag?authz_failure=" + authzFailure);
            } else {
                response.sendRedirect("login.jag");
            }
        }

        var httpsCoreUrl = identityServerURL.getServerURL("", true, true);

        var proxyContextPath = application.get(PROXY_CONTEXT_PATH);
        if (proxyContextPath === 'null' || proxyContextPath == null || proxyContextPath.length <= 0) {
            proxyContextPath = '';
        }

        %>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>My Dashboard | Identity Server</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- styles -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/font-awesome.min.css" rel="stylesheet">
    <link href="css/font-awesome-ie7.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/navigation.css"/>
    <link rel="stylesheet" type="text/css" href="css/portal-dashboard-designer.css"/>
    <link rel="stylesheet" type="text/css" href="css/styles.css">
    <link rel="stylesheet" type="text/css" href="css/dialog_bx.css"/>
    <script>
        var isServerURL = "<%=httpsCoreUrl%>";
        var isProxyContextPath = "<%=proxyContextPath%>";
    </script>

    <script src="js/jquery-3.4.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="<%=proxyContextPath%>/portal/themes/portal/js/shindig.js"></script>
    <script src="js/UESContainer.js"></script>
    <script src="js/portal-dashboard-designer.js"></script>


    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <script src="js/respond.min.js"></script>
    <![endif]-->

    <!-- Fav and touch icons -->
    <link rel="shortcut icon" href="<%=proxyContextPath%>/portal/themes/portal/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144"
          href="img/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114"
          href="img/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72"
          href="img/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="img/apple-touch-icon-57-precomposed.png">


</head>

<body>
<nav class="navbar navbar-inverse navbar-fixed-top ">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand brand" href="#"></a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <li>
                    <a id="user-name" value=<%=session.get(LOGGED_IN_USER) %> style="display:none;" />
                    <a class="dropdown-toggle"
                    data-toggle="dropdown" href="#"><i
                    class="icon-user"></i>
                        <%=session.get(LOGGED_IN_USER) %> <b class="caret"></b></a>
                    <ul class="dropdown-menu dropdown-account" role="menu"
                        aria-labelledby="dLabel">
                        <li>
                            <a href="<%=proxyContextPath%>/dashboard/samlsso.jag?logout=true"><i class="icon-signout"></i> Sign
                                out</a>
                        </li>                        <%
                        if (session.get("connected-accounts") != null) {
                        var connectedAccounts = session.get("connected-accounts");
                        if (connectedAccounts.length > 0) { %>
                        <div class="dropdown_separator"><span class="switch_to_div">Switch To :
                                        </span></div>
                        <%
                        for (var i in connectedAccounts) {
                        var connectedAccount = connectedAccounts[i].username;
                        if('PRIMARY' != connectedAccounts[i].domain) {
                        connectedAccount =  connectedAccounts[i].domain + "/" + connectedAccount;
                        }
                        if('carbon.super' != connectedAccounts[i].tenantDomain) {
                        connectedAccount =  connectedAccount + '@' + connectedAccounts[i].tenantDomain;
                        }
                        %>
                        <li class="associated_accounts">
                            <a href="javascript:void(0)" onclick="switchAccount('<%= connectedAccount %>');">
                                <i class="icon-user pdR2p"></i><%= connectedAccount %>
                            </a>
                        </li>
                        <%
                        }
                        }
                        } else {
                        log.debug("No connections");
                        }
                        %>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>


<div class="container-fluid margin-top-50">
    <input type="hidden" value="design" id="inp-view-mode"/>
    <input type="hidden" value="dashboard" id="inp-dashboard"/>

    <div id="dashboardName"><i class="icon-th-large"></i> <span></span></div>

    <div class="layouts_grid" id="layouts_grid"></div>

</div>

<script>


    function switchAccount(user) {
        $.ajax({
            url: "userAssociationHandler.jag",
            type: "POST",
            data: "&user=" + user,
            success: function (data) {
                var resp = $.parseJSON(data);
                if (resp.success == true) {
                    window.location = ('index.jag');
                } else {
                    if (typeof resp.reLogin != 'undefined' && resp.reLogin == true) {
                        window.location = 'logout.jag';
                    } else {
                        message({content: 'Error occurred while switching user account.', type: 'error',
                            cbk: function () {}});
                    }
                }
            },
            error: function (e) {
                message({content: 'Error occurred while switching user account.', type: 'error', cbk: function () {
                }});
            }
        });
    }

    $(function () {

        UESContainer.inlineClient.subscribe('org.wso2.is.dashboard', function(topic, data, subscriberData) {
            var element = "#"+data.id;

            checkAuthentication(element);
            //$(element).click();

        });
    });





    function checkAuthentication(element) {

        var LoggedUserName = document.getElementById("user-name").getAttribute("value");
        var str = "authenticationHandler.jag?currentLoggedUser="+ LoggedUserName;

        $.ajax({
            type:"GET",
            url:str

        })
            .done(function (data) {
                $(element).click();
            })
            .fail(function () {
                window.location=('index.jag');

            })
            .always(function () {

            });

    }
</script>



</body>
</html>
