package com.cloudera.server.web.cmf;

import com.cloudera.cmf.model.ConfigStalenessStatus;
import com.cloudera.cmf.model.DbRole;
import com.cloudera.cmf.model.DbService;
import com.cloudera.cmf.service.ValidationCollection;
import com.cloudera.server.web.cmf.include.ConfigForm;
import com.cloudera.server.web.cmf.include.ConfigFormBase.Fragment_alertBlock;
import com.cloudera.server.web.cmf.include.SwitchToServiceConfigDialog;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import java.util.Set;
import org.jamon.AbstractRenderer;
import org.jamon.AbstractTemplateImpl;
import org.jamon.Renderer;
import org.jamon.TemplateManager;
import org.jamon.emit.StandardEmitter;
import org.jamon.escaping.Escaping;

public class RoleInstanceConfigImpl extends RoleInstanceBaseImpl
  implements RoleInstanceConfig.Intf
{
  private final CmfPath.Type selectedPathType;
  private final DbRole role;
  private final Map<ConfigContext, ValidationCollection> contextToVC;
  private final String message;
  private final ConfigWidgetManager configWidgetManager;
  private final String groupParent;
  private final String searchQuery;
  private final String groupKey;
  private final boolean peerless;
  private final Set<String> requiredAuthorities;

  protected static RoleInstanceConfig.ImplData __jamon_setOptionalArguments(RoleInstanceConfig.ImplData p_implData)
  {
    if (!p_implData.getSelectedPathType__IsNotDefault())
    {
      p_implData.setSelectedPathType(CmfPath.Type.CONFIG);
    }
    if (!p_implData.getGroupParent__IsNotDefault())
    {
      p_implData.setGroupParent("");
    }
    if (!p_implData.getSearchQuery__IsNotDefault())
    {
      p_implData.setSearchQuery("");
    }
    if (!p_implData.getGroupKey__IsNotDefault())
    {
      p_implData.setGroupKey("");
    }
    if (!p_implData.getPeerless__IsNotDefault())
    {
      p_implData.setPeerless(false);
    }
    if (!p_implData.getRequiredAuthorities__IsNotDefault())
    {
      p_implData.setRequiredAuthorities(null);
    }
    RoleInstanceBaseImpl.__jamon_setOptionalArguments(p_implData);
    return p_implData;
  }

  public RoleInstanceConfigImpl(TemplateManager p_templateManager, RoleInstanceConfig.ImplData p_implData) {
    super(p_templateManager, __jamon_setOptionalArguments(p_implData));
    this.selectedPathType = p_implData.getSelectedPathType();
    this.role = p_implData.getRole();
    this.contextToVC = p_implData.getContextToVC();
    this.message = p_implData.getMessage();
    this.configWidgetManager = p_implData.getConfigWidgetManager();
    this.groupParent = p_implData.getGroupParent();
    this.searchQuery = p_implData.getSearchQuery();
    this.groupKey = p_implData.getGroupKey();
    this.peerless = p_implData.getPeerless();
    this.requiredAuthorities = p_implData.getRequiredAuthorities();
  }

  protected void child_render_4(Writer jamonWriter)
    throws IOException
  {
    ConfigForm __jamon__var_999 = new ConfigForm(getTemplateManager());
    __jamon__var_999.setGroupParent(this.groupParent);
    __jamon__var_999.setSearchQuery(this.searchQuery);
    __jamon__var_999.setGroupKey(this.groupKey);
    __jamon__var_999.setService(this.role.getService());
    __jamon__var_999.renderNoFlush(jamonWriter, this.message, CmfPath.to(CmfPath.Type.CONFIG, this.role), this.configWidgetManager, this.contextToVC, this.requiredAuthorities, new AbstractTemplateImpl(getTemplateManager())
    {
      public Renderer makeRenderer()
      {
        return new AbstractRenderer()
        {
          public void renderTo(Writer jamonWriter)
            throws IOException
          {
            RoleInstanceConfigImpl.1__jamon__instanceOf__0__Fragment_alertBlock.this.renderNoFlush(jamonWriter);
          }
        };
      }

      public void renderNoFlush(Writer jamonWriter) throws IOException {
        jamonWriter.write("\n");

        if (RoleInstanceConfigImpl.this.message != null)
        {
          jamonWriter.write("\n    <div class=\"alert alert-block alert-info\">\n        ");

          if (RoleInstanceConfigImpl.this.role.getService().getConfigStalenessStatus() != ConfigStalenessStatus.FRESH)
          {
            jamonWriter.write("\n            <i class=\"tiny glyphicon warning_sign warn\"></i>\n        ");
          }

          jamonWriter.write("\n        ");

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

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

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

    if (this.peerless)
    {
      jamonWriter.write("\n    ");

      SwitchToServiceConfigDialog __jamon__var_1000 = new SwitchToServiceConfigDialog(getTemplateManager());
      __jamon__var_1000.renderNoFlush(jamonWriter, this.role, this.searchQuery);

      jamonWriter.write("\n");
    }

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