package com.cloudera.server.web.cmf.wizard.service;

import com.cloudera.cmf.model.DbCluster;
import com.cloudera.cmf.model.DbService;
import com.cloudera.enterprise.JsonUtil2;
import com.cloudera.server.web.cmf.WizardBaseImpl;
import com.cloudera.server.web.cmf.include.EntityLinkHelper;
import com.cloudera.server.web.cmf.wizard.CommandDetailsStep;
import com.cloudera.server.web.cmf.wizard.WizardButtons;
import com.cloudera.server.web.cmf.wizard.WizardHiddenStepBase;
import com.cloudera.server.web.cmf.wizard.common.GenericConfigStep;
import com.cloudera.server.web.cmf.wizard.common.SimpleTextStep;
import com.cloudera.server.web.common.I18n;
import com.cloudera.server.web.common.Link;
import com.cloudera.server.web.common.Util;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import org.jamon.TemplateManager;
import org.jamon.emit.StandardEmitter;
import org.jamon.escaping.Escaping;

public class EnableRMWizardPageImpl extends WizardBaseImpl
  implements EnableRMWizardPage.Intf
{
  private final DbService service;
  private final String masterRoleType;
  private final String returnUrl;
  private final HAWizardOptions jsOptions;
  private final boolean needToEnableCGroups;

  protected static EnableRMWizardPage.ImplData __jamon_setOptionalArguments(EnableRMWizardPage.ImplData p_implData)
  {
    if (!p_implData.getNeedToEnableCGroups__IsNotDefault())
    {
      p_implData.setNeedToEnableCGroups(true);
    }
    WizardBaseImpl.__jamon_setOptionalArguments(p_implData);
    return p_implData;
  }

  public EnableRMWizardPageImpl(TemplateManager p_templateManager, EnableRMWizardPage.ImplData p_implData) {
    super(p_templateManager, __jamon_setOptionalArguments(p_implData));
    this.service = p_implData.getService();
    this.masterRoleType = p_implData.getMasterRoleType();
    this.returnUrl = p_implData.getReturnUrl();
    this.jsOptions = p_implData.getJsOptions();
    this.needToEnableCGroups = p_implData.getNeedToEnableCGroups();
  }

  protected void child_render_3(Writer jamonWriter)
    throws IOException
  {
    List stepIds = Lists.newArrayList();

    stepIds.add("gettingStartedStep");
    if (this.needToEnableCGroups) {
      stepIds.add("cgroupsStep");
    }
    stepIds.add("roleAssignmentStep");
    stepIds.add("checkReviewStep");
    stepIds.add("reviewStep");
    stepIds.add("commandStep");
    stepIds.add("commandDetailsStep");
    stepIds.add("cleanupStep");

    if (this.needToEnableCGroups) {
      stepIds.add("confirmClusterRestart");
      stepIds.add("clusterRestartCommandStep");
      stepIds.add("clusterRestartCommandDetailsStep");
    }

    Iterator stepIdIterator = stepIds.iterator();

    jamonWriter.write("<div class=\"Wizard container\">\n    <h1>");

    Escaping.HTML.write(StandardEmitter.valueOf(I18n.t("message.enableIntegratedRM.pageTitle", new String[] { this.service.getDisplayName() })), jamonWriter);

    jamonWriter.write("</h1>\n\n    <div id=\"");

    Escaping.STRICT_HTML.write(StandardEmitter.valueOf((String)stepIdIterator.next()), jamonWriter);

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

    String firstPara = I18n.t("message.enableIntegratedRM.getstarted.desc1", new String[] { "http://tiny.cloudera.com/cm5-rm", EntityLinkHelper.getResourceManagementStatusLink(this.service.getCluster()).getUrl() });

    String secondPara = this.needToEnableCGroups ? I18n.t("message.enableIntegratedRM.getstarted.cgroupsYes") : I18n.t("message.enableIntegratedRM.getstarted.cgroupsNo");

    String fullDescription = firstPara.concat(secondPara);

    SimpleTextStep __jamon__var_269 = new SimpleTextStep(getTemplateManager());
    __jamon__var_269.renderNoFlush(jamonWriter, I18n.t("label.gettingStarted"), fullDescription);

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

    if (this.needToEnableCGroups)
    {
      jamonWriter.write("\n    <div id=\"");

      Escaping.STRICT_HTML.write(StandardEmitter.valueOf((String)stepIdIterator.next()), jamonWriter);

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

      GenericConfigStep __jamon__var_270 = new GenericConfigStep(getTemplateManager());
      __jamon__var_270.setTitle(I18n.t("message.enableIntegratedRM.cgroups.title"));
      __jamon__var_270.renderNoFlush(jamonWriter);

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

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

    EnableHAWizardSteps __jamon__var_271 = new EnableHAWizardSteps(getTemplateManager());
    __jamon__var_271.setRoleAssignmentStepDescription(I18n.t("message.enableIntegratedRM.roleAssignment.desc"));
    __jamon__var_271.setRoleAssignmentStepNotes(null);
    __jamon__var_271.setRoleAssignmentStepTitle(I18n.t("label.assignRoles"));
    __jamon__var_271.renderNoFlush(jamonWriter, stepIdIterator, this.service.getServiceType());

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

    if (this.needToEnableCGroups)
    {
      jamonWriter.write("\n    <div id=\"");

      Escaping.STRICT_HTML.write(StandardEmitter.valueOf((String)stepIdIterator.next()), jamonWriter);

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

      SimpleTextStep __jamon__var_272 = new SimpleTextStep(getTemplateManager());
      __jamon__var_272.renderNoFlush(jamonWriter, I18n.t("label.restartCluster"), I18n.t("message.enableIntegratedRM.restartCluster.help", new String[] { this.returnUrl }));

      jamonWriter.write("\n    </div>\n\n    <div id=\"");

      Escaping.STRICT_HTML.write(StandardEmitter.valueOf((String)stepIdIterator.next()), jamonWriter);

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

      WizardHiddenStepBase __jamon__var_273 = new WizardHiddenStepBase(getTemplateManager());
      __jamon__var_273.renderNoFlush(jamonWriter);

      jamonWriter.write("\n    </div>\n\n    <div id=\"");

      Escaping.STRICT_HTML.write(StandardEmitter.valueOf((String)stepIdIterator.next()), jamonWriter);

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

      CommandDetailsStep __jamon__var_274 = new CommandDetailsStep(getTemplateManager());
      __jamon__var_274.renderNoFlush(jamonWriter);

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

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

    WizardButtons __jamon__var_275 = new WizardButtons(getTemplateManager());
    __jamon__var_275.renderNoFlush(jamonWriter);

    jamonWriter.write("\n\n<script type=\"text/javascript\">\nrequire([\n    \"cloudera/cmf/wizard/service/EnableRMWizard\"\n], function(EnableRMWizard) {\n    jQuery(function($) {\n      var jsOptions = ");

    Escaping.NONE.write(StandardEmitter.valueOf(JsonUtil2.valueAsString(this.jsOptions)), jamonWriter);

    jamonWriter.write(";\n      var options = $.extend(jsOptions, {\n        container: \"#bottomButtons\",\n        stepIds: ");

    Escaping.NONE.write(StandardEmitter.valueOf(JsonUtil2.valueAsString(stepIds)), jamonWriter);

    jamonWriter.write(",\n        needToEnableCGroups: ");

    Escaping.HTML.write(StandardEmitter.valueOf(this.needToEnableCGroups), jamonWriter);

    jamonWriter.write(",\n        clusterName: \"");

    Escaping.JAVASCRIPT.write(StandardEmitter.valueOf(this.service.getCluster().getName()), jamonWriter);

    jamonWriter.write("\",\n        exitUrl: \"");

    Escaping.JAVASCRIPT.write(StandardEmitter.valueOf(Util.escapeForJS(this.returnUrl)), jamonWriter);

    jamonWriter.write("\"\n      });\n      var page = new EnableRMWizard(options);\n    });\n});\n</script>\n");
  }
}