
const templates = require('./_templates.js'),
        config = require('../../config.js');

module.exports.needLogin = function(urlObj) {
    return !urlObj.queryTokens["local"];
};

module.exports.exec = function(req, res) {
    if (req.account && !req.account.cguReadAndAccepted) {
        if (config.allowNewAccounts)
            return {
                status: 200,
                body: templates.header("Mimou - CGU", ["style.css"])
                    + `<section class="maci-wrapper fixed-width"><div class="maci-content">
                    <header id="cguHeader">Merci d'accepter les Conditions Generales d'Utilisation suivantes:</header>
                    <textarea id="cguContent" class="full-width"></textarea>
                    <footer class="button-container"><button id="cguButton">J'accepte les Conditions Generales d'Utilisation</button></footer>
                    </div></section>`
                    +templates.footer(["cgu.min.js"])
            };
        else
            return require('./_403.js').exec(req, res);
    }
    var serviceProviderOptionList = "";
    if (req.account)
        for (var serviceName in config.services)
            serviceProviderOptionList += `<option value="${config.services[serviceName].connectLink}">${serviceName}</option>`;
    return {
        status: 200
        ,body: templates.header("Mimouchat", ["style.css", "styleResponsive.css", "_customsheet"], ['<link href="favicon_err.png" type="image/png" rel="icon" id="linkFavicon" />'])
        +`<aside class="chat-context" id="chatCtx">
              <div class="chat-context-rooms" id="chatList">
                  <div class="chan-search-wrapper"><input type="search" id="chanSearch" class="chan-search-input" /></div>
                  <ul class="chat-context-channellist" id="chanList"></ul>
              </div>
              <nav class="chat-context-menu">
                  <a id="ctxMenuSettings" href="javascript:void(0);"></a>
                  <a id="ctxMenuLogout" href="javascript:void(0);"></a>
              </nav>
          </aside>
          <div class="chat-context-menuButton" id="chatCtxButton"></div>
          <section id="settings" class="maci-setting hidden">
              <header class="settingHead">
                  <h3 id="settingTitle"></h3>
                  <div id="settingDiscardClose"></div>
              </header>
              <div class="settingBody">
                  <aside class="settingNav">
                      <ul id="settingMenuItems">
                          <li id="setting-menu-services" data-target="services"></li>
                          <li id="setting-menu-display" data-target="display"></li>
                          <li id="setting-menu-privacy" data-target="privacy"></li>
                      </ul>
                  </aside>
                  <div class="settingContent">
                      <section class="settings-services">
                          <h4 id="settings-services-title"></h4>
                          <div class="button-container"><button id="settings-serviceAddButton"></button></div>
                          <div class="hidden" id="settings-serviceAddSection">
                              <select id="settings-serviceAddServiceList">`
                              +serviceProviderOptionList
                              +`</select>
                              <div class="button-container"><button id="settings-serviceAddConfirm"></button></div>
                          </div>
                          <ul class="settings-service-list" id="settings-serviceList"></ul>
                          <div class="settings-service-list-empty error-block" id="settings-serviceListEmpty"></div>
                      </section>
                      <section class="settings-display">
                          <h4 id="settings-display-title"></h4>
                          <label><span id="settings-displayEmojiProviderLbl"></span><select id="settings-displayEmojiProvider"></select></label>
                          <label><span id="settings-displayDisplayAvatarLbl"></span><input type="checkbox" value="1" id="settings-displayDisplayAvatar"></label>
                          <label><span id="settings-displayColorfulNamesLbl"></span><input type="checkbox" value="1" id="settings-displayColorfulNames"></label>
                          <label><span id="settings-displayScrollAvatarsLbl"></span><input type="checkbox" value="1" id="settings-displayScrollAvatars"></label>
                          <label><span id="settings-displayAttachmentContentLbl"></span><select id="settings-displayAttachmentContent"><option id="settings-displayAttachmentContent-never" value="0"/><option id="settings-displayAttachmentContent-always" value="1"/><option id="settings-displayAttachmentContent-notimg" value="2"/></select></label>
                      </section>
                      <section class="settings-privacy">
                          <h4 id="settings-privacy-title"></h4>
                      </section>
                  </div>
              </div>
              <footer class="settingFooter">
                  <button id="settingCommit"></button>
              </footer>
          </section>
          <div class="chatsystem-container no-room-selected" id="chatSystemContainer">
              <header class="chatsystem-header" id="chatsystemHeader">
                  <span class="chatsystem-title" id="currentRoomTitle"></span><span id="currentRoomStar" class="chatsystem-header-star"></span>
              </header>
              <div class="chatsystem-content" id="chatWindow"></div>
              <ul class="chatsystem-whoistyping" id="whoistyping"></ul>
              <div class="chatsystem-control">
                  <div id="replyToContainer" class="replyto-container"></div>
                  <ul id="slashList" class="chat-command-list"></ul>
                  <form id="msgForm" class="msgform">
                      <textarea id="msgInput" class="msgform-input" autocomplete="off" rows=1></textarea>
                      <a id="emojiButton" class="button msgform-oneline"/></a>
                      <a id="attachFile" href="#!" class="button msgform-oneline"><img src="paperclip.svg" alt="Send file" class="attach-file-icon" /></a>
                      <a id="msgFormSubmit" class="button msgform-oneline">&gt;</a>
                  </form>
              </div>
          </div>
          <div class="hidden file-upload-container" id="fileUploadContainer"><form id="fileUploadForm" enctype="multipart/form-data">
              <input type="file" id="fileUploadInput" />
              <div id="fileUploadError" class="file-upload-error hidden"></div>
              <a id="fileUploadCancel" class="button"/></a>
              <input type="submit" class="button"/>
          </form></div>
          <div class="error" id="neterror"></div><script>var IS_LOCAL=` +!!req.urlObj.queryTokens['local'] +`;</script>`
        +templates.footer(["mimouchat.min.js"])
    };
};

