<?php
  // Some useful variables
  $v  = rand();// Used to avoid abusive caching by the browser 
  require_once("config.php");// Use "config-sample.php" to create your own
?>
<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Happy Dev App</title>

    <?php 
    // Prod/Dev setup
    if ($dn == 'http://app.happy-dev.fr') {
      require_once("src/html/dependencies-prod.php");
    } else {
      require_once("src/html/dependencies-dev.php");
    }
    ?>
  </head>

  <body>
    <?php require_once('src/html/menu.html'); ?>

    <main id="mainContainer" class="container-fluid">
      <?php require_once('src/html/dashboard.html'); ?>
      <?php require_once('src/html/members.html'); ?>
      <?php require_once('src/html/member.html'); ?>
      <?php require_once('src/html/edit-profile.html'); ?>
      <?php require_once('src/html/projects.html'); ?>
      <?php require_once('src/html/project.html'); ?>
      <?php require_once('src/html/channels.html'); ?>
      <?php require_once('src/html/channel.html'); ?>
      <?php require_once('src/html/search.html'); ?>
    </main>

    <!-- This is a hack : https://git.happy-dev.fr/happy-dev/xmpp-chat-component/issues/24 -->
    <hd-chat-window
        id="chat-singleton"
        data-authentication="anonymous"
        data-auto-login="true"
        data-bosh-service-url="https://conversejs.org/http-bind/"
        data-debug="false"
        data-jid="nomnom.im"
        data-locales-url="<?php echo $cdn; ?>/node_modules/converse.js/locale/{{{locale}}}/LC_MESSAGES/converse.json",
        data-room-jid="anonymous@conference.nomnom.im">
    </hd-chat-window>
    <script>
      // Store url on load
      var currentRoute  = getCurrentRoute();
      var chatSingleton = document.querySelector("#chat-singleton");
      var body          = document.querySelector("body");

      function getCurrentRoute() {
        var pathnameSegments = window.location.pathname.split("/");

        return pathnameSegments[1];
      }

      // Listen for changes
      //setInterval(function() {
          //if (currentRoute != window.location.href) {
            //currentRoute = getCurrentRoute();

            //insertChatIfNeeded();
          //}
      //}, 250);

      // Inserts a chat in the current view if needed
      function insertChatIfNeeded() {
        if (["project", "project-chat", "channel", "channel-chat", "member", "member-chat"].indexOf(currentRoute) > -1) {
          if (currentRoute.indexOf("chat") == -1) {
            currentRoute += "-chat";
          }
          var currentView = document.querySelector("#" + currentRoute);
          if (currentView.querySelector("hd-chat-window") == null) {
            currentView.appendChild(chatSingleton);
            currentView.style.display = 'block';
          }
        }
        else {
          body.appendChild(chatSingleton);
        }
      }
    </script>
  </body>
</html>