package com.cloudera.server.web.cmf;

import com.cloudera.cmf.model.DbService;
import com.cloudera.reports.HdfsCannedQueryReportSpec;
import com.cloudera.server.web.common.CurrentUser;
import com.cloudera.server.web.common.I18n;
import com.cloudera.server.web.reports.ReportsController;
import java.io.IOException;
import java.io.Writer;
import org.jamon.AbstractTemplateImpl;
import org.jamon.TemplateManager;
import org.jamon.emit.StandardEmitter;
import org.jamon.escaping.Escaping;

public class HdfsBrowseEntryDetailsImpl extends AbstractTemplateImpl
  implements HdfsBrowseEntryDetails.Intf
{
  private final DbService service;

  protected static HdfsBrowseEntryDetails.ImplData __jamon_setOptionalArguments(HdfsBrowseEntryDetails.ImplData p_implData)
  {
    return p_implData;
  }

  public HdfsBrowseEntryDetailsImpl(TemplateManager p_templateManager, HdfsBrowseEntryDetails.ImplData p_implData) {
    super(p_templateManager, __jamon_setOptionalArguments(p_implData));
    this.service = p_implData.getService();
  }

  public void renderNoFlush(Writer jamonWriter)
    throws IOException
  {
    jamonWriter.write("<div>\n  <h2 class=\"inlineBlock\"><i class=\"glyphicon\" data-bind=\"css: { 'folder_open': data.dir, 'file': !data.dir }\"></i>\n    <span class=\"path\" data-bind=\"text: data.absolutePath\"></span>\n  </h2>\n  ");

    if (CurrentUser.hasAuthority("ROLE_ADMIN"))
    {
      jamonWriter.write("\n    <div class=\"btn-group inlineBlock valignTop\" data-bind=\"if: snapshotsSupported\">\n      <a href=\"#\" class=\"btn btn-mini dropdown-toggle\" data-toggle=\"dropdown\">\n        <b class=\"caret\"></b>\n      </a>\n      <ul class=\"dropdown-menu pull-right\">\n        <li data-bind=\"visible: $parent.isCurrentPathSnapshottable() && snapshotsLoaded()\">\n          <a href=\"#\" data-bind=\"click: openDisableSnapshotDialog\">\n            ");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.disableSnapshots")), jamonWriter);

      jamonWriter.write("\n          </a>\n        </li>\n        <li data-bind=\"visible: !$parent.isCurrentPathSnapshottable() && !$parent.snapshottableRoot() && data.dir\">\n          <a href=\"#\" data-bind=\"click: openEnableSnapshotDialog\">\n            ");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.enableSnapshots")), jamonWriter);

      jamonWriter.write("\n          </a>\n        </li>\n        <li class=\"disabled\" data-bind=\"visible: !data.dir || (!$parent.isCurrentPathSnapshottable() && $parent.snapshottableRoot())\">\n          <a tabindex=\"-1\">\n            ");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.enableSnapshots")), jamonWriter);

      jamonWriter.write("\n          </a>\n        </li>\n        <li data-bind=\"visible: $parent.isCurrentPathSnapshottable()\">\n          <a href=\"#\" data-bind=\"click: openTakeSnapshotDialog\">");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.takeSnapshot")), jamonWriter);

      jamonWriter.write("</a>\n        </li>\n        <!-- ko if: snapshotsLoaded() && snapshots().length -->\n          <li class=\"divider\"></li>\n          <li>\n            <a href=\"#\" data-bind=\"click: openRestoreSnapshotDialog\">\n              <span data-bind=\"visible: data.dir\">");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.restoreDirectory")), jamonWriter);

      jamonWriter.write("</span>\n              <span data-bind=\"visible: !data.dir\">");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.restoreFile")), jamonWriter);

      jamonWriter.write("</span>\n            </a>\n          </li>\n          <li>\n            <a href=\"#\" data-bind=\"click: openRestoreAsSnapshotDialog\">\n              <span data-bind=\"visible: data.dir\">");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.restoreDirectoryAs")), jamonWriter);

      jamonWriter.write("</span>\n              <span data-bind=\"visible: !data.dir\">");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.restoreFileAs")), jamonWriter);

      jamonWriter.write("</span>\n            </a>\n          </li>\n        <!-- /ko -->\n      </ul>\n    </div>\n  ");
    }

    jamonWriter.write("\n</div>\n<table class=\"table\">\n  <tbody>\n    <tr data-bind=\"visible: data.parent\">\n      <td class=\"field-label\">");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.fileSearch.parent")), jamonWriter);

    jamonWriter.write("</td>\n      <td class=\"field-value path\" data-bind=\"text: data.parent\"></td>\n    </tr>\n    <tr data-bind=\"visible: !data.dir\">\n      <td class=\"field-label\">");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.size")), jamonWriter);

    jamonWriter.write("</label>\n      <td class=\"field-value\" data-bind=\"formattedBytes: data.length\"></p>\n    </tr>\n    <tr data-bind=\"visible: !data.dir\">\n      <td class=\"field-label\">");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.blockSize")), jamonWriter);

    jamonWriter.write("</label>\n      <td class=\"field-value\" data-bind=\"formattedBytes: data.blockSize\"></p>\n    </tr>\n    <tr data-bind=\"visible: !data.dir\">\n      <td class=\"field-label\">");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.fileSearch.replication")), jamonWriter);

    jamonWriter.write("</label>\n      <td class=\"field-value\" data-bind=\"text: data.replication\"></p>\n    </tr>\n    <tr>\n      <td class=\"field-label\">");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.fileSearch.owner")), jamonWriter);

    jamonWriter.write("</label>\n      <td class=\"field-value\" data-bind=\"text: data.owner\"></p>\n    </tr>\n    <tr>\n      <td class=\"field-label\">");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.fileSearch.group")), jamonWriter);

    jamonWriter.write("</label>\n      <td class=\"field-value\" data-bind=\"text: data.group\"></p>\n    </tr>\n    <tr>\n      <td class=\"field-label\">");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.fileSearch.mode")), jamonWriter);

    jamonWriter.write("</label>\n      <td class=\"field-value\" data-bind=\"text: formattedPermission\"></p>\n    </tr>\n    <tr data-bind=\"if: !data.dir\">\n      <td class=\"field-label\">");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.fileSearch.atime")), jamonWriter);

    jamonWriter.write("</label>\n      <td class=\"field-value\" data-bind=\"formattedDate: data.accessTime\"></p>\n    </tr>\n    <tr>\n      <td class=\"field-label\">");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.fileSearch.mtime")), jamonWriter);

    jamonWriter.write("</label>\n      <td class=\"field-value\" data-bind=\"formattedDate: data.modificationTime\"></p>\n    </tr>\n  </tbody>\n</table>\n\n<section class=\"quota-management\" data-bind=\"visible: data.dir, with: quota\">\n  <h3>");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.quotaManagement")), jamonWriter);

    jamonWriter.write("</h3>\n  <table class=\"table\">\n    <tbody>\n      <tr data-bind=\"if: disk\">\n        <td class=\"field-label\">");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.fileSearch.quota.disk")), jamonWriter);

    jamonWriter.write("</td>\n        <td class=\"field-value quota-bar\" data-bind=\"safeHTML: disk\"></td>\n      </tr>\n      <tr data-bind=\"if: filecount\">\n        <td class=\"field-label\">");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.fileSearch.quota.filecount")), jamonWriter);

    jamonWriter.write("</td>\n        <td class=\"field-value quota-bar\" data-bind=\"safeHTML: filecount\"></td>\n      </tr>\n    </tbody>\n  </table>\n  ");

    if (CurrentUser.hasAuthority("ROLE_ADMIN"))
    {
      jamonWriter.write("\n  <p class=\"vertSpace\">\n    <button class=\"btn\" data-bind=\"enable: fileSearchResultData, click: $root.editQuota\">");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.editQuota")), jamonWriter);

      jamonWriter.write("</button>\n  </p>\n  ");
    }

    jamonWriter.write("\n</section>\n\n<section id=\"snapshots\" data-bind=\"if: snapshotsSupported\">\n  <div>\n    <strong>");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.snapshots")), jamonWriter);

    jamonWriter.write("</strong>\n    <span data-bind=\"spinner: {spin: !snapshotsLoaded()}\"></span>\n    <a href=\"#\" data-bind=\"click: showAllSnapshots, visible: sortedSnapshots().length && $parent.isCurrentPathSnapshottable()\">");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.showAll")), jamonWriter);

    jamonWriter.write("</a>\n  </div>\n  <!-- ko template: {name: 'template-hdfs-browse-error'} --><!-- /ko -->\n\n  <!-- ko if: !error() && snapshotsLoaded() && $parent.snapshottableRootLoaded() -->\n  <div class=\"snapshots-message\" data-bind=\"visible: !$parent.isCurrentPathSnapshottable() && !$parent.snapshottableRoot() && !sortedSnapshots().length\" style=\"display: none\">\n    <!-- ko if: !data.dir -->\n      <p>");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("message.browse.hdfs.file")), jamonWriter);

    jamonWriter.write("</p>\n    <!-- /ko -->\n    <!-- ko if: data.dir -->\n      <p>");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("message.browse.snapshots.enable")), jamonWriter);

    jamonWriter.write("</p>\n      ");

    if (CurrentUser.hasAuthority("ROLE_ADMIN"))
    {
      jamonWriter.write("\n        <button class=\"btn\" data-bind=\"click: openEnableSnapshotDialog\">");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.enableSnapshots")), jamonWriter);

      jamonWriter.write("</button>\n      ");
    }

    jamonWriter.write("\n    <!-- /ko -->\n  </div>\n\n  <div class=\"snapshots-message\" data-bind=\"visible: $parent.isCurrentPathSnapshottable() && !snapshots().length\" style=\"display: none\">\n    <p>");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("message.browse.hdfs.noSnapshots")), jamonWriter);

    jamonWriter.write("</p>\n    ");

    if (CurrentUser.hasAuthority("ROLE_ADMIN"))
    {
      jamonWriter.write("\n        <button class=\"btn\" data-bind=\"click: openTakeSnapshotDialog\">");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.takeSnapshot")), jamonWriter);

      jamonWriter.write("</button>\n    ");
    }

    jamonWriter.write("\n  </div>\n\n  <div class=\"snapshots-message\" data-bind=\"visible: !$parent.isCurrentPathSnapshottable() && $parent.snapshottableRoot()\" style=\"display: none\">\n    <p data-bind=\"safeHTML: $parent.snapshottableParentMessage\"></p>\n  </div>\n\n  <ul class=\"entity-list snapshot-list\" data-bind=\"\n      visible: snapshotsLoaded() &&\n        summarizedSnapshots().length &&\n        $parent.isCurrentPathSnapshottable(),\n      template: {\n        name: 'snapshot-list-item-template',\n        foreach: summarizedSnapshots\n      }\" style=\"display: none\"></ul>\n  <!-- /ko -->\n</section>\n\n<section>\n  <h3>");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.reports")), jamonWriter);

    jamonWriter.write("</h3>\n  <ul class=\"unstyled\">\n  ");

    for (HdfsCannedQueryReportSpec spec : ReportsController.getAllHdfsCannedQueryReportSpecs())
    {
      jamonWriter.write("\n    <li><a data-bind=\"href: '");

      Escaping.STRICT_HTML.write(StandardEmitter.valueOf(spec.getUrl(this.service, null)), jamonWriter);

      jamonWriter.write("' + '#path=' + encodeURIComponent(data.absolutePath)\">");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t(spec.getTitleResourceId())), jamonWriter);

      jamonWriter.write("</a></li>\n  ");
    }

    jamonWriter.write("\n  </ul>\n  <div data-bind=\"with: watchDirModel\">\n    <label class=\"watched-dir-flag\" title=\"");

    Escaping.STRICT_HTML.write(StandardEmitter.valueOf(I18n.t("message.browse.hdfs.watchDirHelp")), jamonWriter);

    jamonWriter.write("\">\n      ");

    if (CurrentUser.hasAuthority("ROLE_ADMIN"))
    {
      jamonWriter.write("\n      <input type=\"checkbox\" data-bind=\"checked: isWatched, enable: enabled\" />\n      ");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.filebrowser.watchDir")), jamonWriter);

      jamonWriter.write("\n      ");
    }
    else
    {
      jamonWriter.write("\n      <input type=\"checkbox\" data-bind=\"checked: isWatched\" disabled />\n      ");

      Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("label.filebrowser.watchDir")), jamonWriter);

      jamonWriter.write("\n      ");
    }

    jamonWriter.write("\n    </label>\n  </div>\n</section>\n");
  }
}