fcf.module({
  name: "fcf:NRender/NDetails/TemplateRender.js",
  dependencies: ["fcf:NRender/NDetails/Helper.js"],
  lasy:         ["fcf:NClient/mainClientInclude.js"],
  module: function(helper) {
    var NDetails = fcf.prepareObject(fcf, "NRender.NDetails");
    var innerStorage = {};

    class HeaderMarker {
    };

    /*
    * @class fcf::NRender::NDetails::ArgOrderDetector
    * @brief Класс анализатора порядка элементов
    * @param object a_initializeOptions
    *         - object loader
    *         - object args
    *         - object state
    *         - string template
    *         - object rawTemplate
    **/
    NDetails.TemplateRender = function(a_initializeOptions) {
      var self = this;
      this.buffer = [];
      this.templates = {};
      this.markerPrefix = "<marker c9193820-251d-11ea-bc52-d7f06800af03 ";
      this.markerSuffix = "></marker>";
      this.markerHeader = this.markerPrefix + "header" + this.markerSuffix;;
      this._callMap     = {};

      this.getTheme = function(){
        return fcf.application.getTheme();
      }

      this.build = async function(){
        let result = "";
        let ctxt = fcf.getContext();
        var currentTemplateInfo = fcf.getContext().currentTemplate;
        let headerIndex = undefined;

        for(let i = 0; i < this.buffer.length; ++i){
          let itm = this.buffer[i];
          if (itm instanceof Promise || itm instanceof fcf.Actions) {
            this.buffer[i] = await itm;
            fcf.setContext(ctxt);
            fcf.getContext().currentTemplate = currentTemplateInfo;
          } else if (itm instanceof HeaderMarker) {
            headerIndex = i;
          }
        }

        if (headerIndex !== undefined){
          this.buffer[headerIndex] = await this._header();
        }

        for(let i = 0; i < this.buffer.length; ++i){
          result += fcf.str(this.buffer[i]);
        }

        return result;
      }

      this.write = function(a_content) {
        this.buffer.push(a_content);
      }

      this.template = function(a_options, a_args, a_callPosition){
        let currentTemplate = fcf.getContext().currentTemplate;
        let template    = undefined;
        let args        = undefined;
        let inheritArgs = {};

        if (typeof a_options === "string"){
          template = a_options;
          if (typeof a_args === "number"){
            args = {};
            a_callPosition = a_args;
          } else {
            args = a_args;
          }
        } else {
          template = a_options.template;
          args = a_options.args;
          if (typeof a_args === "number")
            a_callPosition = a_args;
        }
        if (typeof args !== "object")
          args = {};

        let fcfCP = this._getCallPosition(a_callPosition);
        args = fcf.append({}, args, { fcfCP: fcfCP, fcfParent: a_initializeOptions.args.fcfWrapper ? a_initializeOptions.args.fcfId : undefined});

        helper.appendChildInfo(inheritArgs, args, a_initializeOptions.args.fcfChildsArgs);
        fcf.append(args, inheritArgs);

        if (template.charAt(0) == "+")
          template = a_initializeOptions.template.split("+")[0] + template;

        return fcf.application.getRender().getLoader().loadInfo(template)
        .then((a_renderedTemplate)=>{
          if (!a_initializeOptions.rawTemplate.hooks.hookRender)
            return;
          fcf.getContext().currentTemplate = currentTemplate;
          return a_initializeOptions.rawTemplate.hooks.hookRender({
            args:            a_initializeOptions.args,
            renderedArgs:    args,
            renderedOptions: a_renderedTemplate.options,
            renderedPath:    template,
          });
        })
        .then(()=>{
          return fcf.application.getRender().render({
            template:   template,
            route:      a_initializeOptions.route,
            state:      a_initializeOptions.state,
            context:    fcf.getContext(),
            args:       args,
            reqursion:  true
          });
        })
        .then((a_template)=>{
          return a_template.content;
        })
      }

      this.view = function(a_options, a_callPosition) {
        let currentTemplate = fcf.getContext().currentTemplate;
        let fcfCP = this._getCallPosition(a_callPosition);
        let args = fcf.append({}, a_options.args, { fcfCP: fcfCP, fcfParent: a_initializeOptions.args.fcfWrapper ? a_initializeOptions.args.fcfId : undefined});
        let inheritArgs = {};
        helper.appendChildInfo(inheritArgs, args, a_initializeOptions.args.fcfChildsArgs);

        let mode = a_options.mode ?                         a_options.mode :
                   a_options.view && a_options.view.mode ?  a_options.view.mode :
                                                            "read";
        let view = fcf.buildView(a_options.view, mode);

        for(let key in view)
          if (key != "args" && key != "template")
            args[key] = view[key];

        fcf.append(args, view.args);
        fcf.append(args, a_options.args);
        fcf.append(args, inheritArgs);

        if (view.alias)
          args.fcfAlias = view.alias;

        if ("value" in a_options) {
          args.value = a_options.value;
        } else if ("value" in a_options.view) {
          args.value = a_options.view.value;
        } else if (a_options.args && "value" in a_options.args) {
          args.value = a_options.args.value;
        } else if (!fcf.isArg(a_options.record) && args.fcfAlias in a_options.record) {
          args.value = a_options.record[args.fcfAlias];
        } else if (fcf.isArg(a_options.record) && a_options.record.type == "reference" && args.fcfAlias) {
          args.value = fcf.clone(a_options.record);
          args.value.arg += `[${JSON.stringify(args.fcfAlias)}]`;
        }



        if (!args.fcfId)
          args.fcfId = fcf.genId();

        if (mode.split(".")[0] == "add" && view.default !== undefined) {
          if (args.value == undefined){
            args.value = view.default;
            args.defaultFlag = true;
          } else if (fcf.isArg(args.value) && args.value.type == "reference"){
            let id    = args.value.object.id;
            let arg   = args.value.arg;
            let ptr   = fcf.resolveEx(a_initializeOptions.state.args[id], arg, true);
            if (ptr.object[ptr.key] === undefined){
              ptr.object[ptr.key] = view.default;
              ptr.object[ptr.key].defaultFlag = true;
              if (!a_initializeOptions.state.resets[args.fcfId])
                a_initializeOptions.state.resets[args.fcfId] = {};
              a_initializeOptions.state.resets[args.fcfId].value = 1;
            }
          }
        }

        let template  = a_initializeOptions.state.theme.getTemplate(mode, a_options.view).template;
        if (!args.mode)
          args.mode = mode;

        for(let key in args){
          if (fcf.isArg(args[key]) && args[key].type == "recordreference"){
            if (fcf.isArg(a_options.record) && a_options.record.type == "reference") {
              args[key].type   = "reference";
              args[key].object = a_options.record.object;
              args[key].arg    = a_options.record.arg + `[${JSON.stringify(args[key].arg)}]`;
            }
          }
        }

        return fcf.application.getRender().getLoader().loadInfo(template)
        .then((a_renderedTemplate)=>{
          fcf.getContext().currentTemplate = currentTemplate;

          if (!a_renderedTemplate)
            return a_renderedTemplate;
          let block = template.split("+")[1];
          if (!block)
            block = "";
          if (!a_renderedTemplate.templates[block] || !a_renderedTemplate.templates[block].hooks.hookBeforeView)
            return a_renderedTemplate;
          let hookOptions = {
            record: a_options.record,
            view:   a_options.view,
            mode:   mode,
            args:   args,
          }
          if (fcf.isArg(hookOptions.record)){
            if (hookOptions.record.type === "reference") {
              hookOptions.record = fcf.resolve(a_initializeOptions.state.args[hookOptions.record.object.id], hookOptions.record.arg);
            } else if (hookOptions.record.type === "value") {
              hookOptions.record = fcf.tokenizeObject(hookOptions.record.value, {}, true);
            }
          }
          return a_renderedTemplate.templates[block].hooks.hookBeforeView(hookOptions)
          .then(()=>{
            return a_renderedTemplate;
          })
        })
        .then((a_renderedTemplate)=>{
          fcf.getContext().currentTemplate = currentTemplate;

          if (!a_initializeOptions.rawTemplate.hooks.hookRender)
            return;

          return a_initializeOptions.rawTemplate.hooks.hookRender({
            args:            a_initializeOptions.args,
            renderedArgs:    args,
            renderedOptions: a_renderedTemplate.options,
            renderedPath:    template,
          });
        })
        .then(()=>{
          return fcf.application.getRender().render({
            template:   template,
            route:      a_initializeOptions.route,
            state:      a_initializeOptions.state,
            context:    fcf.getContext(),
            args:       args,
            reqursion: true,
          });
        })
        .then((a_template)=>{
          return a_template.content;
        })
      }

      this.header = function() {
        return new HeaderMarker();
      }

      this._getCallPosition = function(a_callPosition){
        var cp = "render:" + a_callPosition;
        if (!(cp in this._callMap))
          this._callMap[cp] = 0;
        cp += "-" + (++this._callMap[cp]);
        return cp;
      }

      this._header = async function() {
        var theme = fcf.application.getTheme(a_initializeOptions.args.fcfTheme, true);
        var result = "";
        var themeInfo = theme.getInfo();

        if (!fcf.empty(a_initializeOptions.state.page.header.title))
          result += "    <title>" + a_initializeOptions.state.page.header.title + "</title>\n";

        if (!fcf.empty(a_initializeOptions.state.page.header.keywords))
          result += `    <meta name="Keywords" content=${JSON.stringify(a_initializeOptions.state.page.header.keywords.join(" "))}>\n`;

        if (!fcf.empty(a_initializeOptions.state.page.header.description))
          result += `    <meta name="description" content=${JSON.stringify(a_initializeOptions.state.page.header.description)}/>\n`;

        result += '    <style> .fcfwrapper { display: inline; }</style>\n';
        
        if (fcf.getContext().needBabel){
          result += '    <script src="' + fcf.getPath("fcf:NClient/regeneratorRuntime.js", false) + '"></script>\n';
          result += '    <script src="' + fcf.getPath("fcf:NClient/es6-promise.auto.js", false) + '"></script>\n';
        }

        result += '    <script src="' + fcf.getPath("fcf:fcf.js", false) + '"></script>\n';
        if (!fcf.getContext().get("debug")) {
          result += '    <script src="/fcfpackages/fcf/include.js"></script>\n';
        } else {
          for(var i = 0; i < fcf.NClient.mainClientInclude.length; ++i){
            let filePath = fcf.getPath(fcf.NClient.mainClientInclude[i], false);
            result += '    <script src="' + filePath + '"></script>\n';
          }
        }
        result += "    <script>\n";
          var defaultTheme = a_initializeOptions.state.defaultTheme ? a_initializeOptions.state.defaultTheme.getName() : undefined;
          if (!defaultTheme)
            defaultTheme = fcf.application.getConfiguration().defaultTheme;

          let context      = fcf.getContext();
          let sendContext  = {};
          for(var k in context) {
            if (k != "safeEnv" && k != "currentTemplate")
              sendContext[k] = context[k];
          }
          var options = {
            context:             sendContext,
            clientRenderingMode: "server",
            defaultTheme:        defaultTheme,
            fileСaching:         fcf.application.getConfiguration().fileСaching,
            renderStorage:       a_initializeOptions.state.renderStorage,
            originSourcesKeys:   a_initializeOptions.state.originSourcesKeys,
            resets:              a_initializeOptions.state.resets,
          };

          if (a_initializeOptions.state.root) {
            options.root =  {
                              id: a_initializeOptions.state.root.id,
                              template: fcf.getPath(a_initializeOptions.state.root.template, false)
                            };
          }

          result += "      fcf.application.setSettings('" + fcf.base64Encode(JSON.stringify(options)) + "');";

          let inlineRenderRestore =  "{renderRestore:";
              inlineRenderRestore += JSON.stringify(a_initializeOptions.state.sources);
              inlineRenderRestore += "}";
          result += "      fcf.application.setSettings('" + fcf.base64Encode(inlineRenderRestore) + "');\n";
        result += "    </script>\n";

        result += '    <script src="/fcfpackages/fcf/settings.js"></script>\n';


        result += "    <script>fcf.application.initialize()</script>\n";

        let avalibleMirrors = fcf.application.getConfiguration().avalibleMirrors;


        await fcf.application.getEventChannel().send("page_after", {route: a_initializeOptions.route, page: a_initializeOptions.state.page});

        var includeFilesMap = {};
        var includeFiles    = [];
        fcf.each(fcf.application.getConfiguration().html.include, function(a_key, a_path){
          var path = fcf.getPath(a_path, false)
          if (!includeFilesMap[path]){
            includeFilesMap[path] = true;
            includeFiles.push(path);
          }
        });
        fcf.each(themeInfo.html.include, function(a_key, a_path){
          var path = fcf.getPath(a_path, false)
          if (!includeFilesMap[path]){
            includeFilesMap[path] = true;
            includeFiles.push(path);
          }
        });
        for(var path in a_initializeOptions.state.include){
          path = fcf.getPath(path, false);
          if (!includeFilesMap[path]){
            includeFilesMap[path] = true;
            includeFiles.push(path);
          }
        }
        for(let i = 0; i < a_initializeOptions.state.page.header.include.length; ++i){
          let path = a_initializeOptions.state.page.header.include[i];
          path = fcf.getPath(path, false);
          if (!includeFilesMap[path]){
            includeFilesMap[path] = true;
            includeFiles.push(path);
          }
        }

        fcf.each(includeFiles, function(a_key, a_path){
          var ext = fcf.getExtension(a_path);
          if (ext == "css") {
            result += '    <link rel="stylesheet" type="text/css" href="' + a_path + '">\n';
          } else if (ext == "js") {
            result += '    <script src="' + a_path + '"></script>\n';
          }
        })

        if (!fcf.empty(a_initializeOptions.state.page.header.header)){
          fcf.each(a_initializeOptions.state.page.header.header, (k, v)=>{
            result += "    ";
            result += v;
            result += "\n";
          })
        }

        if (avalibleMirrors !== true && avalibleMirrors !== undefined){
          let strValue = Array.isArray(avalibleMirrors)
                                ? JSON.stringify(avalibleMirrors)
                                : "undefined";
          result += `    <script>`
          result += `       let currentServer = fcf.parseUrl(window.location.href).server;`
          result += `       let avalibleServers = fcf.append([], ${strValue}, [fcf.parseUrl("${fcf.application.getConfiguration().site}").server]);`
          result += `       var check = ${JSON.stringify(fcf.uuid())};`;
          result += `       let found = false;`
          result += `       fcf.each(avalibleServers, function (a_key, a_server){`
          result += `         let pos = currentServer.lastIndexOf(a_server);`;
          result += `         if (pos == -1)`;
          result += `           return;`;
          result += `         if (pos != currentServer.length - a_server.length)`;
          result += `           return;`;
          result += `         if (pos != 0 && currentServer[pos-1] != ".")`;
          result += `           return;`;
          result += `         found = true;`;
          result += `         var check = ${JSON.stringify(fcf.uuid())};`;
          result += `         return false;`;
          result += `       });`;
          result += `    if (!found) { `;
          result += `      var check = ${JSON.stringify(fcf.uuid())};`;
          result += `      if(window.stop)`;
          result += `        window.stop();`;
          result += `      else if (document.execCommand)`;
          result += `        document.execCommand("Stop", false);`;
          result += `    }`;
          result += `    </script>\n`;
        }

        result += fcf.str(fcf.application.getSystemVariable("fcf:htmlHeader"));
        result += "\n";


        result += '<script>';
        result += 'window.addEventListener("load", function(){';
        result += 'fcf.liven().finally(function(){ fcf.application._isRun = true;});';
        result += '});'
        result += '</script>\n';

        return result;
      }
    }

    return NDetails.TemplateRender;
  }
});
